Generally blocking IO server communication, there is usually a separate acceptor thread responsible for monitoring client contact, which receives the client for each request connected after the client is assigned to process a new thread after processing. Returns the reply to the client. Line friend destroyed.
Take a look at the code that plugs the Ioserver:
Server Startup class
Package Com.bio.demo.server;import Java.io.ioexception;import Java.net.serversocket;import java.net.Socket;import com.bio.demo.server.handler.timerserverhandler;/** * @author Zhouxuejun * * @date October 20, 2014 PM 7:08:58 */public Class Ti Meserver {public static serversocket server=null;/** * @param args */public static void main (string[] args) {//TODO Auto-generated method Stubtry {server=new serversocket (8080); Socket Socket=null;while (True) {socket=server.accept (); New Thread (New Timerserverhandler (socket)). Start ();}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Processing Thread Classes:
Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.printwriter;import java.net.socket;/** * @author Zhouxuejun * * @date October 20, 2014 afternoon 7:17:28 */public class Timerse Rverhandler implements Runnable {private socket socket; public timerserverhandler (socket socket) {//TODO auto-generated constructor stub This.socket=socket;} /* (non-javadoc) * @see java.lang.runnable#run () */@Overridepublic void Run () {//TODO auto-generated method stub Buf Feredreader In=null; PrintWriter Out=null; try {in=new BufferedReader (new InputStreamReader (This.socket.getInputStream ())); Out=new PrintWriter ( This.socket.getOutputStream ()); String Body=null; String Tag=null;while (True) {body=in.readline (); if (null==body) break; Out.print (body+ "_return"); }} catch (IOException e) {//TODO auto-generated catch Blockif (null!=in) {try {in.close ();} catch (IOException E1) {//TODO Auto-generated catch Blocke1.printstacktrace ();}} if (null!=out) {Out.close(); out=null;} if (Null!=this.socket) {try {this.socket.close ()} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} This.socket=null;} E.printstacktrace ();}}}
Can be seen through the code above. Whenever a new client request comes in, the server needs to create a new thread to handle the new incoming client request, and a thread can only process a client request.
In high-performance server applications, concurrent access is often required for thousands of clients. Blocking IO clearly does not meet high performance, highly concurrent field access.
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Block IO comprehension