The significance of the so_reuseaddr of parameters in setsockopt (RPM)

Source: Internet
Author: User

The significance of the so_reuseaddr of parameters in the setsockopt of http://www.cnblogs.com/qq78292959/archive/2013/01/18/2865926.html (Turn)

1, generally speaking, a port after the release will wait two minutes before being used again, SO_REUSEADDR is to let the port released immediately after it can be reused.

SO_REUSEADDR is used for sockets that are in the TIME_WAIT state of a TCP socket and can be used repeatedly for binding. The server program should always set the SO_REUSEADDR socket option before calling bind (). TCP, a party that calls close () first will enter the TIME_WAIT state

2, SO_REUSEADDR and So_reuseport

SO_REUSEADDR offers the following four functions:

SO_REUSEADDR allows you to start a listening server and bundle its well-known ports, even if a previously established connection to use this port as their local port still exists. This is usually the case when restarting the listening server, and if you do not set this option, bind will have an error.

SO_REUSEADDR allows multiple instances of the same server to be started on the same port, as long as each instance bundles a different local IP address. For TCP, we simply cannot start multiple servers that bundle the same IP address and the same port number.

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

SO_REUSEADDR allows for a fully duplicated bundle: When an IP address and port are bound to a set of interfaces, this IP address and port are also allowed to be bundled onto another set of interfaces. In general, this feature is only available on systems that support multicasting, and only for UDP sockets (TCP does not support multicasting).

The So_reuseport option has the following semantics:

This option allows for a full duplicate bundle, but only if this set of interface options are specified for the set of interfaces that want to bundle the same IP address and port.

If the bundled IP address is a multicast address, then SO_REUSEADDR and So_reuseport are equivalent.

Recommendations for using these two sets of interface options:

In all TCP servers, set the SO_REUSEADDR socket 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 bundle the multicast address of this group as the local IP address.

if (setsockopt (FD, Sol_socket, SO_REUSEADDR,

(const void *) &noptval, sizeof (int)) < 0) ...

Report

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

A: This socket option notifies the kernel that if the port is busy but the TCP state is in Time_wait, the port can be reused. If the port is busy and the TCP state is in a different state, reusing the port still gets an error message indicating that the address is already in use. The SO_REUSEADDR option is useful when your service program stops and wants to restart immediately, while the new socket still uses the same port. It is important to realize that any non-expected data arrival at this time could lead to confusion in the service program, but it is only possible, in fact, impossible.

A socket consists of a related five-tuple, protocol, local address, local port, remote address, remote port. SO_REUSEADDR only indicates that local local addresses, local ports can be reused, and the entire related five-tuple is the only certainty. Therefore, after restarting the service program may receive non-expected 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 answers the question:

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

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

The significance of the so_reuseaddr of parameters in setsockopt (RPM)

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.