Server code:
Import java. Io .*;
Import java.net .*;
Class serveonejabber extends thread {
Private Socket socket;
Private bufferedreader in;
Private printwriter out;
Public serveonejabber (socket S)
Throws ioexception {
Socket = s;
In =
New bufferedreader (
New inputstreamreader (
Socket. getinputstream ()));
// Enable auto-flush:
Out =
New printwriter (
New bufferedwriter (
New outputstreamwriter (
Socket. getoutputstream (), true );
// If any of the above callthrow
// Exception, the caller is responsible
// Closing the socket. Otherwise the thread
// Will close it.
Start (); // callrun ()
}
Public void run (){
Try {
While (true ){
String STR = in. Readline (); // receives client data
If (Str. Equals ("end") break;
System. Out. println ("echoing:" + Str );
Out. println (STR); // returns a message to the client.
}
System. Out. println ("closing ...");
} Catch (ioexception e ){
} Finally {
Try {
Socket. Close ();
} Catch (ioexception e ){}
}
}
}
Public class multijabberserver {
Static final int Port = 8080;
Public static void main (string [] ARGs)
Throws ioexception {
Serversocket S = new serversocket (port );
System. Out. println ("server started ");
Try {
While (true ){
// Blocks until a connection occurs:
Socket socket = S. Accept ();
Try {
New serveonejabber (socket );
} Catch (ioexception e ){
// If it fails, close the socket,
// Otherwise the thread will close it:
Socket. Close ();
}
}
} Finally {
S. Close ();
}
}
}
Client code:
Import java.net .*;
Import java. Io .*;
Class jabberclientthread 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;
Public static int threadcount (){
Return threadcount;
}
Public jabberclientthread (inetaddress ADDR ){
System. Out. println ("Making client" + id );
Threadcount ++;
Try {
Socket =
New socket (ADDR, multijabberserver. Port );
} Catch (ioexception e ){
// If the creation of the socket fails,
// Nothing needs to be cleaned up.
}
Try {
In =
New bufferedreader (
New inputstreamreader (
Socket. getinputstream ()));
// Enable auto-flush:
Out =
New printwriter (
New bufferedwriter (
New outputstreamwriter (
Socket. getoutputstream (), true );
Start ();
} Catch (ioexception e ){
// The socket shocould be closed on any
// Failures other than the socket
// Constructor:
Try {
Socket. Close ();
} Catch (ioexception E2 ){}
}
// Otherwise the socket will be closed
// The run () method of the thread.
}
Public void run (){
Try {
For (INT I = 0; I <25; I ++ ){
Out. println ("client" + ID + ":" + I); // send data to the server (or send a request to the server)
String STR = in. Readline (); // read the data sent from the server.
System. Out. println (STR );
}
Out. println ("end ");
} Catch (ioexception e ){
} Finally {
// Always close it:
Try {
Socket. Close ();
} Catch (ioexception e ){}
Threadcount --; // ending this thread
}
}
}
Public class multijabberclient {
Static final int max_threads = 4;
Public static void main (string [] ARGs)
Throws ioexception, interruptedexception {
Inetaddress ADDR =
Inetaddress. getbyname (null );
While (true ){
If (jabberclientthread. threadcount () <max_threads)
New jabberclientthread (ADDR );
Else
Break;
Thread. currentthread (). Sleep (1000 );
}
}
}