Evolution of Java Socket Server (I)

Source: Internet
Author: User

Recently I have been reading some network server designs. This article will introduce the ideas about how modern network servers process concurrent connections from the perspective of their origins. For example, I will use the APIS provided by java. 1. the synchronous blocking server and the operating system API of a single thread are the simplest socket servers. The multi-connection is not considered at all. The main thread processes only one connection at a time, other connections are maintained by the operating system, using the ServerSocket of the java socket package, the backlog supported by The constructor is The TCP connection waiting queue copy Code * The maximum queue length for incoming connection indications (a * request to connect) is set to the <code> backlog </code> parameter. if * a connection indication arrives when the queue is full, the * connection is refused. public ServerSocket (int port, int backl Og) throws IOException {this (port, backlog, null);} copy the sample code of the Code server for test purposes only, regardless of the Elegance: copy the public class SocketServer {Logger log = getLogger ("SocketServer"); ServerSocket server = null; public SocketServer () throws IOException {server = new ServerSocket (8080,50); System. out. println ("Server start... listen on: 8080 "+ server. getInetAddress (). toString ();} public void service () throws InterruptedExce Ption {while (true) {try {log.info ("wait connection... "); Socket socket = server. accept (); log.info (socket. toString (); InputStream is = socket. getInputStream (); shard scan = new shard (is); byte [] buffer = new byte [1024]; while (scan. hasNextLine () {System. out. println ("start read. "); String str = scan. nextLine (); System. out. println (str);} socket. getOutputStream (). write (new String ("HTTP-Vers Ion Status-Code Reason-Phrase CRLF \ r \ nHTTP/1.1 200 OK \ r \ n "). getBytes (); Thread. sleep (1000); log.info ("awake. "); socket. close ();} catch (IOException e) {log. severe (e. getMessage (); // To change body of catch statement use File | Settings | File Templates .}}} public static void main (String [] args) {try {SocketServer ss = new SocketServer (); ss. service ();} catch (Exception e) {e. printStackTrace () ;}} Copy the code because the socketserver's backlog waits for the queue to be set to 50, so the following test shows how the blocked server processes the connection through the telnet client connection. Telnet1 first connects to the test Server IP address 1.132, and sends a response to the console of the tt3: image_thumb6 server. The following data is displayed: image_thumb8 Telnet2 connects to the test Server IP address 1.132 after telnet1: the image_thumb15 server does not display the output, but the socket is connected and the server thread is blocked on telnet1, which has not been processed yet. Therefore, although the operating system has accepted the telnet2 connection, but the application cannot process it. In windows, you can use netstat to check the connection status to see that the two 1.121 connections have been ESTABLISHED and are in the ESTABLISHED status. Image_thumb4 stop telnet1 at this time, and disconnect the socket as follows: image_thumb11 then we can see that the tt4 input before telnet2 has a response on the server side, it indicates that telnet2 input is buffered in the buffer zone of the operating system. The actual processing flow of image_thumb13 is like this. incoming connections are queued one by socketServer in while (true. one-by-one processing of the accept method: image_thumb21, a server program that can only process one connection, is obviously very bad, and no server will handle it like this. So why does the ServerSocket constructor support this backlog Buffer Queue? Next, let's take a look at what Backlog attributes are encapsulated in the JVM code. How does JVM combine this queue with the local operating system API, first look at part of the code for constructing ServerSocket: copy the code public void bind (SocketAddress endpoint, int backlog) throws IOException {... if (backlog <1) backlog = 50; try {SecurityManager security = System. getSecurityManager (); if (security! = Null) security. checkListen (epoint. getPort (); getImpl (). bind (epoint. getAddress (), epoint. getPort (); getImpl (). listen (backlog); bound = true ;}...}

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.