Multiple client to server communication program in Java

前端 未结 3 1101
南旧
南旧 2020-12-14 21:33

I wrote a server-client communication program and it worked well.

Client module

import java.io.*;
import java.net.*;

class Client {
    public sta         


        
3条回答
  •  误落风尘
    2020-12-14 22:09

    MainServer class

    public class Server {
    
        public static void main(String[] args) throws IOException {
    
            ServerSocket serverSocket = null;
    
            boolean listeningSocket = true;
            try {
                serverSocket = new ServerSocket(2343);
            } catch (IOException e) {
                System.err.println("Could not listen on port: 2343");
            }
    
            while(listeningSocket){
                Socket clientSocket = serverSocket.accept();
                MiniServer mini = new MiniServer(clientSocket);
                mini.start();
            }
            serverSocket.close();       
        }
    
    }
    

    Helper Class

    public class MiniServer extends Thread{
    
        private Socket socket = null;
    
        public MiniServer(Socket socket) {
    
            super("MiniServer");
            this.socket = socket;
    
        }
    
        public void run(){
                //Read input and process here
        }
                //implement your methods here
    
    }
    

提交回复
热议问题