First, obstruction
Service-Terminal
public class BIOServer { public static void main (String [] args) throws Exception { the ServerSocket SC = new new the ServerSocket (9093 ); System.out.println ( "Server started successfully!" ); the while (! sc.isClosed ()) { Request the Socket = sc.accept (); // blocking System.out.println ( "receive new connection:" + request.toString ()); the try { the InputStream IS = request.getInputStream (); // NET + I / o Reader = the BufferedReader new new the BufferedReader ( new new the InputStreamReader (IS, "UTF-. 8" )); String MSG; the while (! (MSG = reader.readLine ()) = null ) { // No data will block IF (msg.length ( ) == 0 ) { BREAK ; } System.out.println (MSG); } System.out.println ( "data is received from the:" + request.toString ()); } the catch (Exception E) { // TODO: handle exception } finally { try { request.close(); } catch (Exception e2) { // TODO: handle exception } } } } }
sc.accept () causes the server block until the connection is created
InputStream is blocked
Client
public class BIOClient { public static void main(String[] args) throws IOException { Socket s = new Socket("localhost", 9093); OutputStream out = s.getOutputStream(); Scanner scanner = new Scanner(System.in); System.out.println("请输入"); String msg = scanner.nextLine(); out.write(msg.getBytes()); scanner.close(); s.close(); } }
OutputStream is blocked, will not return after the completion of writing
When two clients simultaneously start
Server only establishes a connection and wait for the client's input
We ended input 123 customers were waiting
Server receives 123, and the establishment of a new connection
This does not meet our needs, following the introduction of multi-threaded, server-side upgrade
Second, the introduction of multi-threading
Thread Pool
public class BIOServer { private static ExecutorService threadPool = Executors.newCachedThreadPool(); public static void main(String[] args) throws Exception { ServerSocket sc = new ServerSocket(9093); System.out.println("服务器启动成功!"); while (!sc.isClosed()) { Socket request = sc.accept(); // 阻塞 System.out.println("收到新连接:" + request.toString()); threadPool.execute(() -> { try { InputStream is = request.getInputStream(); // net+i/o BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8")); String msg; while ((msg = reader.readLine()) != null) { if (msg.length() == 0) { break; } System.out.println(msg); } System.out.println("收到数据,来自:" + request.toString()); } catch (Exception e) { // TODO: handle exception } finally { try { request.close(); } catch (Exception e2) { // TODO: handle exception } } }); } }
Started three clients, respectively input 123,456,789
You do not have to wait for other clients
Third, the service end interacting with the browser
The above simple C / S program does not meet the real scene, to interact with the server browser below.
Our address in the browser, enter the server's 127.0.0.1:9093
We found the service side received the above information, but your browser does not have access to any content
The reason: interactive use of the browser and the server is HTTP protocol
You must meet certain format:
1. The request packet
FIG no upper third portion and a fourth portion
2. The response packet
响应状态码
因此,我们按照HTTP协议的格式,在服务端编写响应浏览器的内容,浏览器就可以收到我们的响应信息
public class BIOServer { private static ExecutorService threadPool = Executors.newCachedThreadPool(); public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9096); System.out.println("tomcat 服务器启动成功"); while (!serverSocket.isClosed()) { Socket request = serverSocket.accept(); System.out.println("收到新连接:" + request.toString()); threadPool.execute(() -> { try { InputStream inputStream = request.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8")); String msg; while ((msg = reader.readLine()) != null) { if (msg.length() == 0) { break; } System.out.println(msg); } System.out.println("收到数据,来自:" + request.toString()); // 响应结果 OutputStream outputStream = request.getOutputStream(); outputStream.write("HTTP/1.1 200 ok\r\n".getBytes()); outputStream.write("Content-Length: 40\r\n\r\n".getBytes()); outputStream.write("<button type=\\\"button\\\">Click Me!</button>".getBytes()); } catch (Exception e) { // TODO: handle exception } finally { try { request.close(); } catch (Exception e) { // TODO: handle exception } } }); } serverSocket.close(); } }
浏览器访问服务器
概念总结:
BIO:blocking IO,资源不可用时,IO请求一直阻塞,直到收到反馈结果(有数据或超时)。
NIO:non-blocking IO,资源不可用时,IO请求离开返回,返回数据标识资源不可用。
同步IO:synchronous IO,应用阻塞在发送或接收数据的状态,直到数据成功传输或返回失败。
异步IO:asynchronous IO,应用发送或接收数据后立刻返回,实际处理时异步执行的。
阻塞带来的问题:阻塞导致在处理网络I/O时,一个线程只能处理一个网络连接。