I. Client GUI
In this example, the Client GUI is implemented using swing, and the server does not use the GUI.
Public class javaclient extends jframe implements actionlistener // use jframe as the base class to implement the actionlistener Interface
{
Jbutton sendbutton; // "send" button
Jtextfield inputfield; // input box
Jtextarea outputaera; // server return box
Public javaclient () // initialize the GUI in the constructor
{
Inputfield = new jtextfield ("Enter..." Here); // text box for the client to enter
Outputarea = new jtextarea ("server return"); // display the text field of the data returned by the server
Sendbutton = new jbutton ("send ");
Jpanel Panel = new jpanel (); // create a panel
Panel. setlayout (New borderlayout (); // set the Panel style to borderlayout.
Panel. Add (inputfield, borderlayout. North); // place controls
Panel. Add (outputarea, borderlayout. center );
Settitle ("Java communication client ");
Setcontentpane (panel );
}
}
Ii. Client Communication client agent
Create a custom package named clientagent, which includes the clientagent class. This class is used to perform interaction with the server. Separating the interface and communication class makes debugging and maintenance easier. The clientagent class has the following functions:
1. Establish a server connection in the construction function.
2. The sendrequest () method sends data to the server.
3. The getresponse () method obtains a response from the server.
1. member variables
The member variables of the clientagent class include:
Printstream OPS; // output stream (pointing to server)
Datainputstream IPs; // input stream (from the server)
String cltrequest; // client request
String svrresponse; // server-side response
2. the constructor clientagent (string servername, int port) contains two parameters that receive the server name and port number passed in from the main () function of javaclient.
Public clientagent (string servername, int port)
{
Try
{
Socket clientsocket = new socket (servername, Port); // create a socket based on the server name and port number
Ops = new printstream (clientsocket. getoutputstream (); // gets the output stream of the socket.
IPS = new datainputstream (clientsocket. getinputstream (); // obtain the socket input stream
}
Catch (exception E)
{
System. Out. println ("unable to connect to the server! ");
}
}
3. sendrequest () method
Public void sendrequest (string request)
{
Ops. println (request); // write a string to the socket output stream
}
4. getrespone () method
Public String getresponse ()
{
String STR = new string ();
Try
{
STR = IPs. Readline (); // read the string from the socket input stream
}
Catch (ioexception e) {}// an error must be caught
Return STR;
}
Iii. Main () Functions and event processing of the client javaclient class
1. Main () function
With the above preparations, the main () function becomes very concise
Public static void main (string [] ARGs)
{
Javaclient frame = new javaclient ();
Frame. Pack (); // note that javaclient is a derived class of jframe and calls the base class method.
Frame. setvisible (true );
CA = new clientagent ("127.0.0.1", 1001); // pass the server name and port number
}
2. Capture and process the event that you click send.
Public void actionreceivmed (actionevent E)
{
If (E. getsource () = sendbutton) // determine whether the event source control is a "send" button
{
CA. sendrequest (inputfield. gettext (); // send the text in the text box
Outputarea. append ("/N" + Ca. getresponse (); // receives the server response and writes it to the text field
}
}
Iv. Server
1. serveragent
1) member variables
Serversocket svrskt = NULL;
Socket cltskt = NULL;
Datainputstream input = NULL; // input stream, from the client
Printstream output = NULL; // The output stream pointing to the client
2) serveragent () Construction Function
Public serveragent (INT port) // The listening port number transmitted by the main () function
{
System. Out. println ("the server proxy is listening, Port:" + port );
Try
{
Svrskt = new serversocket (port); // start listening
} Catch (ioexception e) {system. Out. println ("listening port" + port + "failed ");}
Try
{
Cltskt = svrskt. Accept (); // receives connection requests
}
Catch (ioexception e) {system. Out. println ("connection failed ");}
Try
{
Input = new datainputstream (cltskt. getinputstream (); // obtain the input stream
Output = new printstream (cltskt. getoutputstream (); // get the output stream
}
Catch (ioexception e ){}
Output. println ("Welcome ......");
}
3) The getrequest () method reads the data sent by the client.
Public String getrequest ()
{
String frmclt = NULL;
Try
{
Frmclt = input. Readline ();
}
Catch (exception e ){
System. Out. println ("unable to read port .....");
System. Exit (0 );
}
Return frmclt;
}
4) The sendresponse () method sends data to the client.
Public void sendresponse (string response)
{
Try
{
Output. println (response );
}
Catch (exception e ){
System. Out. println ("failed to write the port ......");
System. Exit (0 );
}
}
2. Main () function
In the main () function, different requests sent by the client are processed. This example only introduces the principle. Therefore, the server simply sends the data sent by the client back:
Public static void main (string [] ARGs) throws ioexception
{
Serveragent SA = new serveragent (1001 );
While (true)
{
SA. sendresponse (SA. getrequest ());
}
}