The basic model of network programming is the Client/server model, which is the communication between two processes, where the server provides location information (
Bind IP address and listening port), the client sends a connection request through the connection operation to the address that the server listens to, establishes the connection through three handshakes,
If the connection is successful, both parties can communicate through the socket.
In the development of the traditional synchronous blocking model, ServerSocket is responsible for binding the IP address and starting the listening port: The socket is responsible for initiating the connection request
Operation. After the operation is successfully connected, both sides communicate synchronously through the input and output streams.
Here is the classic time server code that analyzes the work process:
Timeserver Code:
Package Com.panther.dong.netty.bio.synchronousblockio;import Java.net.serversocket;import java.net.Socket;/** * Server thread (corresponding to any client thread) * Created by Panther on 15-8-11. */public class Timeserver {public static void main (string[] args) {int port = 8080; if (args! = null && args.length > 0) {port = integer.valueof (Args[0]); } ServerSocket server = null; try {server = new ServerSocket (port); System.out.println ("The time server is start in port:" + port); Socket socket = NULL; while (true) {socket = server.accept (); New Thread (new Timeserverhandler (socket)). Start (); }} catch (Exception e) {e.printstacktrace (); } finally {if (server! = null) {System.out.println ("The time server close"); try {server.close (); } CATCH (Exception e) {} server = null; } } }}
Timerserver the port to be monitored based on the incoming parameters, and if there is no entry, use the default 8080 port to create the ServerSocket through the constructor
, if the port is valid and not in use, the server listener is successful. Program through a loop to listen to the client's access, if there is no client's
Access, the thread blocks on the ServerSocket accept operation, starts the Timeserver, waits for the client's access
Timeserverhandler's Code:
Package Com.panther.dong.netty.bio.synchronousblockio;import Java.io.bufferedreader;import java.io.IOException; Import Java.io.inputstreamreader;import java.io.printwriter;import java.net.socket;import java.util.Date;/** * Listener Client Socket * Created by panther on 15-8-11. */public class Timeserverhandler implements Runnable {private socket socket; Public Timeserverhandler (socket socket) {this.socket = socket; } @Override public void Run () {bufferedreader in = null; PrintWriter out = null; try {in = new BufferedReader (New InputStreamReader (This.socket.getInputStream ())); out = new PrintWriter (This.socket.getOutputStream (), true); String current = null; String BODY = null; while (true) {BODY = In.readline (); if (BODY = = null) {break; } System.out.println ("The time server receive order:" + body); Current = "QUERY time ORDER". Equalsignorecase (body)? New Date (System.currenttimemillis ()). ToString (): "Bad ORDER"; OUT.PRINTLN (current); }} 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; } } }}
Client code timeclient:
Package Com.panther.dong.netty.bio.synchronousblockio;import Java.io.bufferedreader;import java.io.IOException; Import Java.io.inputstreamreader;import java.io.printwriter;import java.net.socket;/** * Client thread (one thread) * Created by Panther on 15-8-13. */public class Timeclient {public static void main (string[] args) {int port = 8080; if (args! = null && args.length > 0) {try {port = integer.valueof (Args[0]); } catch (Exception e) {}} socket socket = NULL; BufferedReader in = null; PrintWriter out = null; try {socket = new socket ("127.0.0.1", port); in = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); out = new PrintWriter (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 (IOException e) {} finally {if (out! = null) {out.close (); out = null; } if (in = null) {try {in.close (); } catch (IOException e) {} in = null; } if (socket! = NULL) {try {socket.close (); } catch (IOException e) {} socket = null; } } }}
Operation Result:
Run Timeserver first to get the result:
When you run timeclient, you get the following results:
As can be known from the above figure, Timeserver and timeclient establish a connection!!!!
The drawbacks of Bio:
Whenever a new client access request is made, the server must create a new thread to process the new access link, and a thread can handle only one
The connection to the client. In the application domain of high-performance servers, it is often necessary to have thousands of concurrent connections between clients, this model can not meet high performance
, high concurrent access scenarios!!!
Bio Introduction Complete ~~~~~~~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Netty Learning (ii)--traditional bio programming