Detailed explanation of java serversocket parameters and ocket Parameters

Source: Internet
Author: User

Detailed explanation of java serversocket parameters and ocket Parameters

ServerSocket has the following three options.

SO_TIMEOUT: the timeout time for waiting for the client to connect.
SO_REUSEADDR: Indicates whether to allow reuse of the address bound to the server.
SO_RCVBUF: indicates the size of the buffer for receiving data.

 

SO_TIMEOUT: the timeout value for receiving data. The Unit is millisecond. The default value is 0, indicating waiting forever.

Set this option: public void setSoTimeOut (int timeout) throws SocketException
Read this option: public int getSoTimeOut () throws IOException;

 

SO_TIMEOUT indicates the timeout time for the ServerSocket accept () method to wait for a client connection, in milliseconds. If the value of SO_TIMEOUT is 0, it indicates that it will never time out. This is the default value of SO_TIMEOUT.

When the server executes the ServerSocket accept () method, if the connection request queue is empty, the server waits until it receives the client connection and returns the result from the accept () method. If a timeout value is set, SocketTimeoutException is thrown when the server wait time exceeds the timeout value. It is a subclass of InterruptedException.

When the underlying Socket implementation does not support the SO_TIMEOUT option, these two methods will throw the SocketException exception. You cannot set timeout to a negative number. Otherwise, the setSoTimeout method will throw the IllegalArgumentException exception.

Instance see: http://blog.csdn.net/woshisap/article/details/6597413

 

SO_RESUSEADDR: Indicates whether to allow reuse of the local address bound to the socket

Set this option: public void setReuseAddress (boolean on) throws SocketException
Read this option: public boolean getReuseAddress () throws SocketException

 

This option is used to determine whether to allow the new ServerSocket to be bound to the same port as the old ServerSocket if data is still transmitted to the old ServerSocket on the network, the default value of this option is related to the operating system. In some operating systems, port reuse is allowed, and in some systems port reuse is not allowed.

When ServerSocket is closed, if there is still data sent to this serversocket on the network, this ServerSocket will not immediately release the local port, but will wait for a while to ensure that the delayed data sent from the network is received, then release the port.

It is worth noting that public void setReuseAddress (boolean on) throws SocketException must be used before ServerSocket is bound to a local port. Otherwise, this method is invalid. In addition, both processes that share the same port must call the serverSocket. setReuseAddress (true) method, so that after one process closes ServerSocket, The ServerSocket of the other process can immediately reuse the same port.

 

SO_RCVBUF: the buffer size of the received data

Set this option: public void setReceiveBufferSize (int size) throws SocketException
Read this option: public int getReceiveBufferSize () throws SocketException

SO_RCVBUF indicates the buffer size of the Socket for input data. generally, a large continuous data block (Communication Based on HTTP or FTP protocol) can use a large buffer, which can reduce the number of data transfers and improve the efficiency of data transmission. for communication modes with frequent interactions and a small amount of data transferred at a time (Telnet and online games), a small buffer zone should be used to ensure that small batches of data can be sent to the other party at the same time. this setting of the buffer size also applies to the Socket SO_SNDBUF option.

If the underlying Socket does not support the SO_RCVBUF option, the setReceiveBufferSize () method will throw a SocketException.

 

Backlog: 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.

 

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.

Instance reference: http://blog.csdn.net/huang_xw/article/details/7338487

 

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.