Socket server class, receiving requests
Package com. Wonder. socketexample;
Import java. Io. datainputstream;
Import java. Io. dataoutputstream;
Import java. Io. ioexception;
Import java.net. serversocket;
Import java.net. Socket;
Import java. util. date;
Public class socketserver {
Serversocket = NULL;
Socket socket = NULL;
Datainputstream instream = NULL;
Dataoutputstream outstream = NULL;
Public socketserver (){
Try {
Serversocket = new serversocket (6600 );
} Catch (ioexception e ){
E. printstacktrace ();
}
}
Public static void main (string [] ARGs ){
Socketserver = new socketserver ();
Socketserver. waitforclient ();
}
Public void waitforclient (){
Try {
// Obtain the socket and set instream and outstream
Socket = serversocket. Accept ();
Instream = new datainputstream (socket. getinputstream ());
Outstream = new dataoutputstream (socket. getoutputstream ());
Outstream. writeutf ("ready! ");
/// Socket. setsotimeout (3000 );
// Receive and send data
Acceptandsenddata ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Private void acceptandsenddata (){
Boolean flag = true;
While (FLAG ){
Try {
String acceptstr = instream. readutf ();
System. Out. println ("client input information:" + acceptstr );
If ("exit". Equals (acceptstr )){
System. Out. println ("the service ends. ");
Break;
} Else {
String sendstr = string. valueof ("server processing:" + new date ());
Outstream. writeutf (sendstr );
}
} Catch (ioexception e ){
E. printstacktrace ();
Break;
}
}
}
}
-----------------------------------------
In the socketclient example, enter the content, send it to socket, and exit.
Package com. Wonder. socketexample;
Import java. Io. bufferedreader;
Import java. Io. datainputstream;
Import java. Io. dataoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java.net. Socket;
Import java.net. unknownhostexception;
Public class socketclient {
Socket socket = NULL;
Datainputstream instream = NULL;
Dataoutputstream outstream = NULL;
Public socketclient (){
Try {
Init ();
Waitdata ();
} Catch (unknownhostexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
Private void waitdata (){
Boolean flag = true;
While (FLAG ){
Try {
String acceptstr = instream. readutf ();
System. Out. println ("Client Received:" + acceptstr );
Bufferedreader in = new bufferedreader (New inputstreamreader (system. In ));
String STR = "";
While (STR! = NULL ){
System. Out. Print ("> prompt ");
STR = in. Readline ();
If ("exit". Equals (STR )){
System. Out. println ("the system exits. ");
Outstream. writeutf (STR );
Return;
}
Outstream. writeutf (STR );
}
} Catch (exception e ){
E. printstacktrace ();
Break;
}
}
}
Private void Init () throws unknownhostexception, ioexception {
Socket = new socket ("127.0.0.1", 6600 );
Instream = new datainputstream (socket. getinputstream ());
Outstream = new dataoutputstream (socket. getoutputstream ());
/// Socket. setsotimeout (3000 );
}
Public static void main (string [] ARGs ){
Socketclient client = new socketclient ();
}
}