Port reuse C ++ (address reuse)

Source: Internet
Author: User

In my previous articles, I have referenced a port reuse and port exclusive program design. However, I found a bit unclear about it. I added a bit to the author in good faith. the fourth parameter in setsockopt () indicates whether to reuse the port. If it is set to 1, it can be reused. Otherwise, reuse is prohibited. Therefore, the above article should add: cflag = 1;


Hidden Danger 3. Address usage error (EADDRINUSE)

You can use the bind API function to bind an address (an interface and a port) to a socket endpoint. You can use this function in server settings to restrict the interfaces that may be connected. You can also use this function in client settings to restrict the interfaces that should be used for outgoing connections. The most common usage of bind is to associate the port number with the server and use the wildcard address (INADDR_ANY), which allows any interface to be used for the incoming connection.

The common problem with bind is to try to bind a port that is already in use. This risk is caused by the TCP socket status TIME_WAIT. A socket may not be active, but the binding port is still prohibited (bind returns EADDRINUSE. This status is retained for about 2 to 4 minutes after the socket is disabled. After the TIME_WAIT status exits, the socket is deleted so that the address can be rebound.

It may be annoying to wait for TIME_WAIT to end. In particular, if you are developing a socket server, you need to stop the server to make some changes and restart it. Fortunately, there are ways to avoid the TIME_WAIT status. You can apply the SO_REUSEADDR socket option to the socket so that the port can be reused immediately.

Consider the example in listing 3. Before binding an address, I used the SO_REUSEADDR option to call setsockopt. To allow address reuse, I set the integer parameter (on) to 1 (otherwise, it can be set to 0 to prohibit address reuse ).


Listing 3. Use the SO_REUSEADDR socket option to avoid address usage errors

Int sock, ret, on;
Struct sockaddr_in servaddr;

/* Create a new stream (TCP) socket */
Sock = socket (AF_INET, SOCK_STREAM, 0 ):

/* Enable address reuse */
On = 1;
Ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, & on, sizeof (on ));

/* Allow connections to port 8080 from any available interface */
Memset (& servaddr, 0, sizeof (servaddr ));
Servaddr. sin_family = AF_INET; www.2cto.com
Servaddr. sin_addr.s_addr = htonl (INADDR_ANY );
Servaddr. sin_port = htons (45000 );

/* Bind to the address (interface/port )*/
Ret = bind (sock, (struct sockaddr *) & servaddr, sizeof (servaddr ));


After the SO_REUSEADDR option is applied, the bind API function allows immediate address reuse.

 

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.