Simple Chat Room code

Source: Internet
Author: User
Tags sendmsg

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 );
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.