The meaning of so_reuseaddr in setsockopt)

Source: Internet
Author: User

1. Generally, a port can be used again after being released for two minutes. so_reuseaddr enables the port to be used again immediately after being released.

So_reuseaddr is used to bind a TCP socket in the time_wait status. The server program should always set the so_reuseaddr socket option before calling BIND. TCP: The party that calls close () first enters the time_wait status.

2. so_reuseaddr and so_reuseport

So_reuseaddr provides the following functions:

So_reuseaddr allows you to start a listener server and bind its well-known port, even if the previously established connection to use this port as their local port still exists. This usually occurs when the listening server is restarted. If this option is not set, an error occurs during bind.

So_reuseaddr allows multiple instances of the same server to be started on the same port, as long as each instance is bound with a different local IP address. For TCP, it is impossible to start multiple servers that bind the same IP address and the same port number.

So_reuseaddr allows a single process to bind the same port to multiple sets of interfaces, as long as each bundle specifies a different local IP address. This is generally not used for TCP servers.

So_reuseaddr allows completely repeated binding: When an IP address and port are bound to a set of interfaces, the IP address and port are also allowed to be bound to another set of interfaces. Generally, this feature is available only on systems that support multicast, and only for UDP interfaces (TCP does not support multicast ).

The so_reuseport option has the following semantics:

This option allows repeated binding, but this option is only required for the interface that wants to bind the same IP address and port.

If the bound IP address is a multicast address, so_reuseaddr is equivalent to so_reuseport.

Suggestions for using these two sets of Interface Options:

In all TCP servers, set the so_reuseaddr interface option before calling bind;

When writing a multicast application that can run multiple times on the same host at the same time, set the so_reuseaddr option and bind the multicast address of this group as the local IP address.

If (setsockopt (FD, sol_socket, so_reuseaddr,

(Const void *) & noptval, sizeof (INT) <0 )...

Appendix

Q: What does so_reuseaddr mean when writing a TCP/sock_stream service program?

A: This socket option notifies the kernel. If the port is busy but the TCP status is time_wait, You can reuse the port. If the port is busy and the TCP status is in another State, an error message is still returned when the port is reused, indicating "The address is in use ". If you want to restart your service program immediately after it is stopped, and the new socket still uses the same port, the so_reuseaddr option is very useful. It must be realized that any unexpected data arrival at this time may lead to confusion in the response of the service program, but this is only a possibility, in fact, very unlikely.

A socket is composed of five groups: Protocol, local address, local port, remote address, and remote port. So_reuseaddr only indicates that the local address and port can be reused. The entire quintuple is unique. Therefore, the restarted service program may receive unexpected data. The so_reuseaddr option must be used with caution. [2]

 

[1] http://topic.csdn.net/u/20090103/16/a0414edb-b289-4c72-84da-39e155e8f4be.html

[2]

The following blog provides a Q &:

Http://blog.sina.com.cn/s/blog_53a2ecbf010095db.html

[3] http://www.sudu.cn/info/html/edu/20050101/296180.html

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.