Simple Chat Room applications (client and server)
Server:
Import java. Io .*;
Import java.net .*;
Public class chatsvr
{
Public static void main (string [] Str)
{
Serversocket SOC;
Socket svrsoc;
Objectoutputstream objout = NULL;
Objectinputstream objin = NULL;
Try {
System. Out. println ("waiting for client's connecting ...");
SOC = new serversocket (6666 );
Svrsoc = Soc. Accept ();
System. Out. println ("the Client Connected, you can exit this program by type 'quit '");
Objout = new objectoutputstream (svrsoc. getoutputstream ());
Objin = new objectinputstream (svrsoc. getinputstream ());
} Catch (exception e) {system. Exit (0 );}
Sendmsgout send = new sendmsgout (objout );
Send. Start ();
Getmsgfromclient get = new getmsgfromclient (objin );
Get. Start ();
}
}
Class sendmsgout extends thread
{
Objectoutputstream objout = NULL;
Public sendmsgout (objectoutputstream out)
{
Objout = out;
}
Public void run ()
{
String strmsg = "";
While (true)
{
Try {
Strmsg = (New bufferedreader (New inputstreamreader (system. In). Readline ();
Objout. writeobject (strmsg );
If (strmsg. Equals ("quit") system. Exit (0 );
} Catch (exception e ){}
// System. Out. println (strmsg );
}
}
}
Class getmsgfromclient extends thread
{
Objectinputstream objin;
Public getmsgfromclient (objectinputstream in)
{
Objin = in;
}
Public void run ()
{
String strmsg = "";
While (true)
{
Try {
Strmsg = (string) objin. readobject ();
System. Out. println ("the client said:" + strmsg );
If (strmsg. Equals ("quit") system. Exit (0 );
} Catch (exception e ){}
}
}
}
Client:
Import java. Io .*;
Import java.net .*;
Public class chatclient
{
Public static void main (string [] Str)
{
String servername = "";
If (Str. length> = 1) servername = STR [0];
Else {
System. Out. println ("You must provide a server name you want to connect ,");
System. Out. println ("and run your program by input in DOS prompt like this :");
System. Out. println ("Java chatclient servername ");
System. Exit (0 );
}
Socket cltsoc;
Objectoutputstream objout = NULL;
Objectinputstream objin = NULL;
Try {
Inetaddress address = inetaddress. getbyname (servername );
Cltsoc = new socket (addresses, 6666 );
Objout = new objectoutputstream (cltsoc. getoutputstream ());
Objin = new objectinputstream (cltsoc. getinputstream ());
System. Out. println ("connected to the server successfully ...");
System. Out. println ("if you want to exit this program, type 'quit '! ");
} Catch (exception e) {system. Exit (0 );}
Getmsg get = new getmsg (objin );
Get. Start ();
Sendmsg send = new sendmsg (objout );
Send. Start ();
}
}
Class getmsg extends thread
{
Objectinputstream objin;
Public getmsg (objectinputstream in)
{
Objin = in;
}
Public void run ()
{
String strmsg = "";
While (true)
{
Try {
Strmsg = (string) objin. readobject ();
System. Out. println ("the server said:" + strmsg );
If (strmsg. Equals ("quit") system. Exit (0 );
} Catch (exception e ){}
}
}
}
Class sendmsg extends thread
{
Objectoutputstream objout;
Public sendmsg (objectoutputstream out)
{
Objout = out;
}
Public void run ()
{
String strmsg = "";
While (true)
{
Try {
Strmsg = (New bufferedreader (New inputstreamreader (system. In). Readline ();
Objout. writeobject (strmsg );
If (strmsg. Equals ("quit") system. Exit (0 );
} Catch (exception e ){}
// System. Out. println (strmsg );
}
}
}