A simple socket example

Source: Internet
Author: User

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

}

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.