Java network communication: synchronous blocking I/O model (BIO)

Source: Internet
Author: User

Disadvantage: A thread can handle only one client connection

Service side:

 Public classTimeserver { Public Static voidMain (string[] args)throwsIOException {intPort = 8080; ServerSocket Server=NULL; Try{Server=NewServerSocket (port); System.out.println ("The time server is a start in port:" +port); Socket Socket=NULL;  while(true) {Socket= Server.accept ();//block until a new connection is available//each monitor hears a socket connection and generates a new thread to handle                NewThread (NewTimeserverhandler (socket)). Start (); }        } finally {            if(Server! =NULL) {System.out.println ("The time server close");                Server.close (); Server=NULL; }        }    }}

Transaction thread:

 Public classTimeserverhandlerImplementsRunnable {Privatesocket socket;  PublicTimeserverhandler (socket socket) { This. Socket =socket; } @Override Public voidrun () {BufferedReader in=NULL; PrintWriter out=NULL; Try {            //get input and output streamin =NewBufferedReader (NewInputStreamReader ( This. Socket.getinputstream ())); out=NewPrintWriter ( This. Socket.getoutputstream (),true); String currenttime=NULL; String Body=NULL;  while(true) {Body= In.readline ();//block until data is read, or data is read or an exception occurs                if(BODY = =NULL)                     Break; System.out.println ("The time server receive order:" +body); CurrentTime= "QUERY time ORDER". Equalsignorecase (body)?Newjava.util.Date (System.currenttimemillis ()). ToString ():"Bad ORDER"; Out.println (currenttime);//Output            }        } Catch(Exception e) {//Handling Exceptions        }    }}

Client:

 Public classtimeclient { Public Static voidMain (string[] args) {intPort = 8080; Socket Socket=NULL; BufferedReader in=NULL; PrintWriter out=NULL; Try{Socket=NewSocket ("127.0.0.1", Port); Inch=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); out=NewPrintWriter (Socket.getoutputstream (),true); Out.println ("QUERY Time ORDER");//Send Request System.out.println ("Send order 2 server succeed."); String resp=in.readline ();//reply SYSTEM.OUT.PRINTLN ("Now is:" +resp); } Catch(Exception e) {e.printstacktrace (); } finally {            if( This. Socket! =NULL) {                Try {                     This. Socket.close (); } Catch(IOException E1) {e1.printstacktrace (); }                 This. Socket =NULL; }        }    }}

Java network communication: synchronous blocking I/O model (BIO)

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.