Simple server and client code instances

Source: Internet
Author: User

Simple server and client code instances

Exercise the application of socket, datainputstream, dataoutputstream, thread, and actionlistener. The function is very simple: Enter the user name in the client and press enter to check whether the user name is valid. I have defined that only jiqimiao is valid.

The server code is as follows:

Import java. AWT .*;
Import java. Io .*;
Import java.net .*;
Import java. util .*;

Import javax. Swing .*;

/**
* Server programs
* @ Author Zuo Jie
*
*/
Public class testserver extends jframe {
/**
*
*/
Private Static final long serialversionuid =-4148974450944551683l;

Private jtextarea JTA = new jtextarea ();

Public static void main (string [] ARGs ){
New testserver ();
}

Public testserver (){
This. getcontentpane (). setlayout (New borderlayout ());
This. getcontentpane (). Add (New jscrollpane (JTA), borderlayout. center );
Settitle ("server ");
Setsize (500,300 );
Setdefaclocloseoperation (jframe. exit_on_close );
Setvisible (true );
Try {
Serversocket = new serversocket (5000); // bind the server port
JTA. append ("Server start at" + new date () + "");
While (true ){
Socket socket = serversocket. Accept (); // server listener
// Start the listening thread
Handleclint thread = new handleclint (socket );
Thread. Start ();
}
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
/**
* Receiving and sending client threads
* @ Author Zuo Jie
*
*/
Class handleclint extends thread {
Private Socket socket;

Public handleclint (Socket socket ){
This. Socket = socket;
}

Public void run (){
Try {
Datainputstream inputfromclient = new datainputstream (socket
. Getinputstream ());
Dataoutputstream outputtoclient = new dataoutputstream (socket
. Getoutputstream ());
While (true ){
String ins = datainputstream. readutf (inputfromclient); // receives client information
String outs = "you are not a legal user ";
If (INS. Equals ("jiqimiao ")){
Outs = "Welcome:" + INS;
}
Outputtoclient. writeutf (outs); // sends the information to the client.
JTA. append (outs + "");
}
} Catch (exception e ){
}
}
}
}

The client code is as follows:

Import java. AWT. borderlayout;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;
Import java. Io. datainputstream;
Import java. Io. dataoutputstream;
Import java. Io. ioexception;
Import java.net. Socket;

Import javax. Swing. jframe;
Import javax. Swing. jlabel;
Import javax. Swing. jpanel;
Import javax. Swing. jscrollpane;
Import javax. Swing. jtextarea;
Import javax. Swing. jtextfield;

/**
* Client program
* @ Author Zuo Jie
*
*/
Public class testclient extends jframe implements actionlistener {
/**
*
*/
Private Static final long serialversionuid =-1751294222460334070l;
Private jtextarea JTA = new jtextarea ();
Private jtextfield JTF = new jtextfield ();
Private dataoutputstream toserver;
Private datainputstream fromserver;
Public static void main (string [] ARGs ){
New testclient ();
}
Public testclient (){
Jpanel P = new jpanel ();
P. setlayout (New borderlayout ());
P. Add (New jlabel ("Enter the User name:"), borderlayout. West );
P. Add (JTF, borderlayout. center );
JTF. sethorizontalalignment (jtextfield. Left );

This. getcontentpane (). setlayout (New borderlayout ());
This. getcontentpane (). Add (p, borderlayout. North );
This. getcontentpane (). Add (New jscrollpane (JTA), borderlayout. center );
JTF. addactionlistener (this); // Add event listening
Settitle ("client ");
Setsize (500,300 );
Setdefaclocloseoperation (jframe. exit_on_close );
Setvisible (true );
Try {
Socket socket = new socket ("localhost", 5000); // link to specify the server and Port
This. fromserver = new datainputstream (socket. getinputstream ());
This. toserver = new dataoutputstream (socket. getoutputstream ());
} Catch (ioexception e ){
// Todo auto-generated Catch Block
JTA. append (E. tostring () + "");
}

}
Public void actionreceivmed (actionevent e) {// define the listener
If (E. getsource () instanceof jtextfield ){
Try {
String TOS = JTF. gettext (). Trim ();
Toserver. writeutf (ToS );
Toserver. Flush ();
String gets = datainputstream. readutf (fromserver );;
JTA. append (gets + "");
} Catch (ioexception E1 ){
// Todo auto-generated Catch Block
System. Err. println (E1 );
}


}
}

}

Everyone learns and makes progress together.

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.