// Serveonesocket. Java Server Source Code
Import java. Io .*;
Import java.net .*;
Public class serveonesocket extends thread {
Private Socket socket;
Private bufferedreader in;
Private printwriter out;
Private string content;
/**
* Constructor
*/
Public serveonesocket (socket S, string c) throws ioexception {
Socket = s;
Content = C;
In = new bufferedreader (
New inputstreamreader (
Socket. getinputstream ()));
// Enable auto-flush
Out = new printwriter (
New bufferedwriter (
New outputstreamwriter (
Socket. getoutputstream (), true );
Start (); // callrun ()
}
Public void run (){
Try {
While (true ){
String STR = in. Readline ();
If (Str. Equals ("end") break;
System. Out. println ("echoing:" + Str );
Out. println (STR );
Out. println (content );
}
System. Out. println ("closing ...");
} Catch (ioexception e ){
} Finally {
Try {
Socket. Close ();
} Catch (ioexception e ){}
}
}
}
// Socketclientthread. Java client source code
Import java.net .*;
Import java. Io .*;
Class socketclientthread extends thread {
Private Socket socket;
Private bufferedreader in;
Private printwriter out;
Private Static int counter = 0;
Private int id = counter ++;
Private Static int threadcount = 0;
Final int Port = 8110;
Public static int threadcount (){
Return threadcount;
}
Public socketclientthread (inetaddress ADDR ){
System. Out. println ("Making client:" + id );
Threadcount ++;
Try {
Socket = new socket (ADDR, Port );
} Catch (ioexception e ){
}
Try {
In = new bufferedreader (
New inputstreamreader (
Socket. getinputstream ()));
Out = new printwriter (
New bufferedwriter (
New outputstreamwriter (
Socket. getoutputstream (), true );
Start ();
} Catch (ioexception e ){
Try {
Socket. Close ();
} Catch (ioexception E2 ){}
}
}
Public void run (){
Try {
For (INT I = 0; I <25; I ++ ){
Out. println ("client:" + ID + ":" + I );
String STR = in. Readline ();
System. Out. println (STR );
}
Out. println ("end ");
} Catch (ioexception e ){
} Finally {
Try {
Socket. Close ();
} Catch (ioexception e ){}
Threadcount --;
}
}
}
Public class multisocketclient {
Static final int max_threads = 10;
/**
* Main
* @ Param ARGs
*/
Public static void main (string [] ARGs) throws ioexception, interruptedexception {
Inetaddress ADDR = inetaddress. getbyname (null );
While (true ){
If (socketclientthread. threadcount () <max_threads)
New socketclientthread (ADDR );
Thread. currentthread (). Sleep (100 );
}
}
}