Bio Communication model of Netty authoritative guide

Source: Internet
Author: User

The basic model of network programming is the Client/server model, that is, two processes to communicate with each other, where the server provides location information (bound IP address and listening port), the client through the connection operation to the server to listen to the address to initiate a connection request, through three handshake to establish a connection, If the connection is successful, both parties can communicate over a network socket (socket).

The service side of the bio communication model, usually by a separate acceptor thread, is responsible for listening to the client's connection, after it receives a client connection request, creates a new thread for each client for link processing, and after processing completes, returns the reply to the customer via the output stream, which the thread destroys.

Under this model, the number of threads on the server and the number of concurrent accesses to the client are 1:1 proportional, the thread is a valuable resource of Java virtual machine, after the expansion of the number of threads, the performance of the system decreases, and the thread concurrency access continues to increase, resulting in process downtime or zombie.

Timeserver and Timeclient cases are as follows

 PackageCom.hjp.netty.bio;Importjava.io.IOException;ImportJava.net.ServerSocket;ImportJava.net.Socket;/*** Created by Jiapeng on 2017/2/1.*/ Public classTimeserver { Public Static voidMain (string[] args)throwsIOException {intPort = 8080; if(Args! =NULL&& args.length > 0) {            Try{Port=integer.valueof (port); } Catch(NumberFormatException e) {}} 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 (); NewThread (NewTimeserverhandler (socket)). Start (); }        } finally {            if(Server! =NULL) {System.out.println ("The time server close");                Server.close (); Server=NULL; }        }    }}
Timeserver
 PackageCom.hjp.netty.bio;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.Socket;Importjava.util.Date;/*** Created by Jiapeng on 2017/2/1.*/ Public classTimeserverhandlerImplementsRunnable {Privatesocket socket;  PublicTimeserverhandler (socket socket) { This. Socket =socket; } @Override Public voidrun () {BufferedReader in=NULL; PrintWriter out=NULL; Try{ in=NewBufferedReader (NewInputStreamReader ( This. Socket.getinputstream ())); out=NewPrintWriter ( This. Socket.getoutputstream (),true); String currenttime=NULL; String Body=NULL;  while(true) {Body=In.readline (); if(BODY = =NULL) {                     Break; } System.out.println ("The time server receive order:" +body); CurrentTime= "QUERY time ORDER". Equalsignorecase (body)?NewDate (System.currenttimemillis ()). ToString (): "Bad ORDER";            Out.println (currenttime); }        } Catch(Exception e) {if(In! =NULL) {                Try{in.close (); } Catch(IOException E1) {e1.printstacktrace (); }            }            if(Out! =NULL) {out.close (); out=NULL; }            if( This. Socket! =NULL) {                Try {                     This. Socket.close (); } Catch(IOException E1) {e1.printstacktrace (); }                 This. Socket =NULL; }        }    }}
Timeserverhandler
 PackageCom.hjp.netty.bio;ImportJavax.management.Query;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.Socket;/*** Created by Jiapeng on 2017/2/2.*/ Public classtimeclient { Public Static voidMain (string[] args) {intPort = 8080; if(Args! =NULL&& args.length > 0) {            Try{Port=integer.valueof (port); } Catch(NumberFormatException e) {e.printstacktrace (); }} 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"); System.out.println ("Send order 2 server succeed."); String resp=In.readline (); System.out.println ("Now is:" +resp); } Catch(Exception e) {}finally {            if(Out! =NULL) {out.close (); out=NULL; }            if(In! =NULL) {                Try{in.close (); } Catch(IOException e) {e.printstacktrace (); } in=NULL; }            if(Socket! =NULL) {                Try{socket.close (); } Catch(IOException e) {e.printstacktrace (); } Socket=NULL; }        }    }}
timeclient

Bio Communication model of Netty authoritative guide

Related Article

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.