Java Socket parameters: backlog

Source: Internet
Author: User

The maximum queue length of the input connection indication (for connection requests) is set to the backlog parameter. If the queue is full, the connection is rejected.
Note:
1. The backlog parameter must be a positive value greater than 0. If the passed value is equal to or less than 0, it is assumed to be the default value.
2. After testing, this queue follows the FIFO principle.
3. If you place the accept function in a loop body, the backlog parameter does not work. Or simply speaking, the backlog parameter takes effect only when the thread running serversocket is blocked, whether in accept or read.
Create a serversocket instance, bind it to port 10000, and set backlog to 2

Package socket; import Java. io. *; import java.net. *; import Org. apache. log4j. logger; public class test_backlog {Private Static logger = logger. getlogger (test_backlog.class); public static void main (string [] ARGs) throws exception {bufferedreader in = NULL; printwriter out = NULL; int backlog = 2; serversocket = new serversocket (10000, backlog); While (true) {logger. debug ("Start the server ...... "); int I; Socket socket = serversocket. accept (); logger. debug ("a client is connected to the server. The client information is as follows:" + socket. getinetaddress () + ":" + socket. getport () + ". "); In = new bufferedreader (New inputstreamreader (socket. getinputstream (); out = new printwriter (socket. getoutputstream (), true); do {char [] C = new char [1024]; I = in. read (c); logger. debug ("message received by the server:" + new string (C, 0, I) ;}while (I =-1); out. close (); In. close (); socket. close (); logger. debug ("Disable the server ...... ");}}}

Server logs:

0 [main] Debug socket. test_backlog-start the server ...... 3871 [main] Debug socket. test_backlog-a client is connected to the server. The client information is as follows:/127.0.0.1: 4176.18888 [main] Debug socket. test_backlog-the server receives the message: I will be disconnected after the message is sent. 18888 [main] Debug socket. test_backlog-Disable the server ...... 18889 [main] Debug socket. test_backlog-start the server ...... 18890 [main] Debug socket. test_backlog-a client is connected to the server. The client information is as follows:/127.0.0.1: 4177.45316 [main] Debug socket. test_backlog-the server receives the message: I am the second client. After sending the message, it is also disconnected. 45316 [main] Debug socket. test_backlog-Disable the server ...... 45316 [main] Debug socket. test_backlog-start the server ...... 45317 [main] Debug socket. test_backlog-a client is connected to the server. The client information is as follows:/127.0.0.1: 4178.52501 [main] Debug socket. test_backlog-the server receives the message: the last pull. 52501 [main] Debug socket. test_backlog-Disable the server ......

Use the TCP tool to connect to this server

1. When the first client is connected, the server outputs the following information:
6629 [main] Debug socket. test_backlog-a client is connected to the server. The client information is as follows:/127.0.0.1: 4110.
2. When the second client is connected, the server does not output anything. If the client is successfully displayed, it is blocked in the connection queue.
3. When the third client is connected, the situation is the same as 2.
4. When the fourth client is connected, the connection fails and an error is returned. Because the backlog parameter is set to 2, there are only two blocking messages in the queue.
Summary:
The task for managing client connection requests is completed by the operating system. The operating system stores these connection requests in an FIFO queue. Many operating systems limit the maximum queue length, generally 50. When the connection requests in the queue reach the maximum capacity of the queue, the host where the server process is located rejects new connection requests. Only when the server process extracts connection requests from the queue through the serversocket accept () method to free up space in the queue can the queue continue to add new connection requests.
For a customer process, if the connection request sent by the customer process is added to the server Queue, the connection between the customer and the server is established successfully, and the customer process returns normally from the socket constructor. If the connection request sent by the client process is rejected by the server, the socket constructor will throw a connectionexception.

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.