Java Reference
In-Depth Information
public
public static
static final
final int
int ECHOPORT = 7 ;
public
public static
static final
final int
int NUM_THREADS = 4 ;
/** Main method, to start the servers. */
public
public static
static void
void main ( String [] av ) {
new
new EchoServerThreaded2 ( ECHOPORT , NUM_THREADS );
}
/** Constructor */
public
public EchoServerThreaded2 ( int
int port , int
int numThreads ) {
ServerSocket servSock ;
try
try {
servSock = new
new ServerSocket ( port );
} catch
catch ( IOException e ) {
/* Crash the server if IO fails. Something bad has happened */
throw
throw new
new RuntimeException ( "Could not create ServerSocket " , e );
}
// Create a series of threads and start them.
for
for ( int
int i = 0 ; i < numThreads ; i ++) {
new
new Handler ( servSock , i ). start ();
}
}
/** A Thread subclass to handle one client conversation. */
class
class Handler
extends Thread {
ServerSocket servSock ;
int
Handler extends
int threadNumber ;
/** Construct a Handler. */
Handler ( ServerSocket s , int
int i ) {
servSock = s ;
threadNumber = i ;
setName ( "Thread " + threadNumber );
}
public
public void
void run () {
/* Wait for a connection. Synchronized on the ServerSocket
* while calling its accept() method.
*/
while
while ( true
true ) {
try
try {
System . out . println ( getName () + " waiting" );
Search WWH ::




Custom Search