Java serversocket parameters in a detailed

Source: Internet
Author: User
Tags ftp protocol

ServerSocket has the following 3 options.

So_timeout: Represents the time-out period waiting for a client connection.
SO_REUSEADDR: Indicates whether to allow reuse of addresses that are bound by the server.
SO_RCVBUF: Represents the size of the buffer that receives the data.

So_timeout: Indicates the wait time-out period when data is received. The unit is in milliseconds, and the default value is 0, which means waiting forever.

Set this option: public void setsotimeout (int timeout) throws SocketException
Read this option: public int getsotimeout () throws IOException;

So_timeout represents the time-out, in milliseconds, that ServerSocket's accept () method waits for a client connection. If the value of So_timeout is 0, it means that it will never time out, which is the default value for So_timeout.

When the server executes the accept () method of ServerSocket, if the connection request queue is empty, the server waits until the client connection is received and is returned from the Accept () method. If the time-out is set, then when the server waits longer than the time-out, it throws Sockettimeoutexception, which is a subclass of Interruptedexception.

These two methods will throw socketexception exceptions when the underlying socket implementation does not support the So_timeout option. You cannot set timeout to a negative number, otherwise the Setsotimeout method throws an IllegalArgumentException exception.

Examples See: http://blog.csdn.net/woshisap/article/details/6597413

SO_RESUSEADDR: Indicates whether the local address that the socket is bound to is allowed to be reused

Set this option: public void Setreuseaddress (Boolean on) throws SocketException
Read this option: public boolean getreuseaddress () throws SocketException

This option is used to determine if there is still data on the network that transmits data to the old ServerSocket, whether the new ServerSocket is allowed to bind to the same port as the old ServerSocket, the default value of the option is related to the operating system, and in some operating systems, the port is allowed to be reused , and the reuse of ports is not allowed in some systems.

When ServerSocket is turned off, if there is data on the network that is sent to the ServerSocket, the ServerSocket does not immediately release the local port, but waits for some time to ensure that the deferred data sent over the network is received before the port is released.

It is important to note that the public void Setreuseaddress (Boolean on) throws SocketException must be used before ServerSocket is bound to a local port, otherwise execution of the method is not valid. In addition, two processes that are common to the same port must call the Serversocket.setreuseaddress (True) method to enable a process to close serversocket. The serversocket of another process can also immediately reuse the same port.

SO_RCVBUF: Buffer size for receiving data

Set this option: public void setreceivebuffersize (int size) throws SocketException
Read this option: public int getreceivebuffersize () throws SocketException

SO_RCVBUF represents the size of the Socket's buffer for entering data. In general, transmitting large contiguous blocks of data (based on HTTP or FTP protocol communication) can use a larger buffer, which reduces the number of times the data is transmitted and improves the efficiency of data transfer. In the case of communication (Telnet and online games) with frequent interactions and smaller data volumes, small buffers should be used to ensure that small batches of data are sent to each other in a timely manner. This principle of setting the buffer size also applies to the Socket's so_sndbuf option.

If the underlying Socket does not support the SO_RCVBUF option, the Setreceivebuffersize () method throws SocketException.

Backlog: The maximum queue length for the input connection indication (Request for connection) is set to the backlog parameter. If the queue receives a connection indication when it is full, the connection is rejected.

1. The backlog parameter must be a positive value greater than 0. If the value passed is equal to or less than 0, the default value is assumed.
2. After testing this queue is in accordance with the FIFO (first-in-one-out) principle.
3. If you put the Accept function in a loop body, the backlog parameter will not have any effect. Or simply say that the thread that is running the ServerSocket will block when it is blocked in the accept or read, the backlog parameter will not take effect.

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

Java serversocket parameters in a detailed

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.