Example of the SO_REUSEADDR Option

Source: Internet
Author: User

Quoted from

Http://www.docs.hp.com/en/B2355-90136/ch03s01.html

 

This option is af_inet socket-specific.

So_reuseaddr
Enables you to restart a daemon which was killed or terminated.

This option modifies the rules used by bind to validate local addresses, but it does not violate the uniqueness requirements of an association. so_reuseaddr modifies the BIND rules only when a wildcard Internet Protocol (IP) address is used in combination with a participating protocol port. the host still checks at connection time To be sure any other sockets with the same local address and local port do not have the same remote address and remote port. connect
Fails if the uniqueness requirement is violated.

Example of the so_reuseaddr Option

A network daemon server is listening on a specific port: port 2000. If you executedNetstat
, Part of the output wowould resemble:


Active connections (including   servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp 0 0 *.2000 *.* LISTEN

Network daemon server listening at port 2000

When the network daemon accepts a connection request, the accepted socket will bind
To port 2000 and to the address where the daemon is running (e.g.192.6.250.100
). If you executedNetstat
, The output wocould resemble:


Active connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp 0 0 192.6.250.100.2000 192.6.250.101.4000 ESTABLISHED
tcp 0 0 *.2000 *.* LISTEN

New Connection established, daemon server still listening

Here the network daemon has established a connection to the client (192.6.250.101.4000
) With a new server socket. The original network daemon server continues to listen for more connection requests.

If the listening network daemon process is killed, attempts to restart the daemon fail if SO_REUSEADDR is not set. the restart fails because the daemon attempts to bind to port 2000 and a wildcard IP address (e.g.*. 2000.
). The wildcard address matches the address of the established connection (192.6.250.100
), SoBind
Aborts to avoid duplicate socket naming.

When SO_REUSEADDR
Is set,Bind
Ignores the wildcard match, so the network daemon can be restarted. An example usage of this option is:


int optval = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval));
bind (s, &sin, sizeof(sin));

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.