Intro_SocketServer_Thread1
 
  • Adding Thread to a Sever:
    • It is always better to have multi-threded socket server
    • A loop which will dynamically accept new connections, a new thread is created with a new connection and disposed after the connection is closed.
    • A connection should be passed to a Thread object, than handling directly.
    • Below is an example accept loop:
      while (true) {
        try {
          Socket s = ss.accept();
          ThreadedEchoServer test 
      = new ThreadedEchoServer(s);
          test.start();
        }
        catch (IOException e) {}
      }
  • Thread Pool: Create a pool of threads with incoming connection, and dispose the threads from the queue as  the operations are dismissed
  • use accept() in the thread run() method than in the main() method.