Order the Book

Class ThreadedEchoServer

java.lang.Object
  |
  +--NetworkServer
        |
        +--EchoServer
              |
              +--ThreadedEchoServer

public class ThreadedEchoServer
extends EchoServer
implements Runnable

A multithreaded variation of EchoServer.

Taken from Core Servlets and JavaServer Pages from Prentice Hall and Sun Microsystems Press, http://www.coreservlets.com/. © 2000 Marty Hall; may be freely used or adapted.


Fields inherited from class EchoServer
maxRequestLines, serverName
 
Constructor Summary
ThreadedEchoServer(int port, int connections)
           
 
Method Summary
 void handleConnection(Socket server)
          The new version of handleConnection starts a thread.
static void main(String[] args)
          Supply a port number as a command-line argument.
 void run()
           
 
Methods inherited from class NetworkServer
getMaxConnections, getPort, listen, setMaxConnections, setPort
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadedEchoServer

public ThreadedEchoServer(int port,
                          int connections)
Method Detail

main

public static void main(String[] args)
Description copied from class: EchoServer
Supply a port number as a command-line argument. Otherwise port 8088 will be used.

handleConnection

public void handleConnection(Socket server)
The new version of handleConnection starts a thread. This new thread will call back to the old version of handleConnection, resulting in the same server behavior in a multithreaded version. The thread stores the Socket instance since run doesn't take any arguments, and since storing the socket in an instance variable risks having it overwritten if the next thread starts before the run method gets a chance to copy the socket reference.
Overrides:
handleConnection in class EchoServer

run

public void run()
Specified by:
run in interface Runnable

Order the Book

Order
CSAJSP @ amazon.com

Return to Top-Level Source Code Archive