Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
428
Chapter 16 Using HTML Forms
Listing 16.10 ThreadedEchoServer.java (continued)
* before the run method gets a chance to
* copy the socket reference.
*/
public void handleConnection(Socket server) {
Connection connectionThread = new Connection(this, server);
connectionThread.start();
}
public void run() {
Connection currentThread =
(Connection)Thread.currentThread();
try {
super.handleConnection(currentThread.serverSocket);
} catch(IOException ioe) {
System.out.println("IOException: " + ioe);
ioe.printStackTrace();
}
}
}
/** This is just a Thread with a field to store a
* Socket object. Used as a thread safe means to pass
* the Socket from handleConnection to run.
*/
class Connection extends Thread {
protected Socket serverSocket;
public Connection(Runnable serverObject,
Socket serverSocket) {
super(serverObject);
this.serverSocket = serverSocket;
}
}
NetworkServer
Listings 16.11 and 16.12 present some utilities classes that simplify network
ing. The
EchoServer
is built on top of them.
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved