简体   繁体   中英

Multithread Server Java

I'm trying to make a simple ECHO server that can manage more client.

Server Class:

public class EchoServer {

   protected int          port  ;
   protected ServerSocket socket;
   private   Socket       acceptedSocket;

   public EchoServer(int port) throws IOException {
      this.port = port;
      socket = new ServerSocket(port);
   }

   public void start() throws AcceptingClientException {
      while(!socket.isClosed()) {
         try {
            acceptedSocket = socket.accept();
         }
         catch (IOException e){
            throw new AcceptingClientException();
         }
         ClientHandler ch = new ClientHandler(acceptedSocket);
         ch.run();
      }
   }
}

Runnable client handler:

public class ClientHandler implements Runnable {

   Socket socket;

   public ClientHandler(Socket socket) {
      this.socket = socket;
   }

   @Override
   public void run() {
      PrintWriter    From_Server = null;
      BufferedReader To_Server   = null;
      String to_server_string    = null;
      try {
         From_Server = new PrintWriter(socket.getOutputStream());
         To_Server   =
            new BufferedReader(
               new InputStreamReader( socket.getInputStream()));
         System.out.println("Stream opened.\n");
         while(true) {
            if(To_Server.ready()){
               System.out.println("Reading input line.\n");
               to_server_string = To_Server.readLine();
               if(to_server_string.equalsIgnoreCase("quit")) {
                  System.out.println("Connection closed on user request.\n");
                  From_Server.print("Bye :)\n");
                  From_Server.close();
                  To_Server.close();
                  socket.close();               
               }
               else {
                  System.out.println(
                     "String '" +
                     to_server_string+"' is not 'quit', echoing.\n");
                  From_Server.print("ECHO: "+to_server_string+"\n");
                  System.out.println("String written on stream, flushing.\n");
                  From_Server.flush();
               }
            }
         }
      }
      catch (IOException e) {
         System.out.println("Stream error (connection closed?).\n");
      }
   }
}

Main Class

public static void main(String[] args) {
   try {
      EchoServer server= new EchoServer(9999);
      server.start();
   }
   catch (IOException ex) {
      System.out.println("Unable to start server (port is busy?)\n");
      Logger.getLogger(SimpleServer.class.getName()).log(Level.SEVERE, null, ex);
   }
   catch (AcceptingClientException e){
      System.out.println("Unable to accept client\n");
   }
}

More than one client is able to connect to the server, but the ECHO will works only with one client at the time (if I close the connection with one client the server will start to handle another one automatically), but I can't understand why: when a client connects to the server, the associated socked created with server.accept() is passed to a new instance of a runnable client handler which is started with handler.run() and the server should go back on waiting in server.accept() (unless the ServerSocket is closed). I'm assuming the issue should be with this method of the server class:

public void start() throws AcceptingClientException {
   while(!socket.isClosed()) {
      try {
         acceptedSocket=socket.accept();
      }
      catch (IOException e){
         throw new AcceptingClientException();
      }
      ClientHandler ch = new ClientHandler(acceptedSocket);
      ch.run();
   }
}

But I can't figure out what is wrong with it...what am I missing?

Your code:

ClientHandler ch = new ClientHandler(acceptedSocket);
ch.run();

doesn't start a new thread, it delegates to ClientHandler.run() in the same thread.

To start a thread, use new Thread( ch ).start(); since ch is of class ClientHandler which implements Runnable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM