Linux socket Programming Port problem (Bind error:address already in use)
When programming Linux networks, each time you modify the source code and compile the runtime again, you often encounter the following usage errors:
Bind error:address already in use
Although using CTRL + C to force the end of the process, but the error persists, with Netstat-an |grep 5120 and PS aux |grep 5120 can also see just use CTRL + C "forced end" of the process, the port or use, had to kill each time with the end process, It's a lot of trouble. Last night accidentally browsing to an IBM Web site titled "Linux Socket Programming 5 hidden dangers" article, suddenly, today tried, really solve the problem, thank you here, but also hope that more coder see this article, to avoid errors.
The main code is:
int on;
on = 1;
ret = setsockopt (sock, Sol_socket, so_reuseaddr, &on, sizeof (on));
Now every time I use CTRL + C to force the end of the process, with Netstat and PS can also see the port in use, but the running program does not appear "Address already in using" error, the implementation of port reuse.
The following is the third hidden problem in the original--Address usage Error
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 limit the interfaces that may come with the connection. You can also use this function in client settings to limit the interfaces that should be used for connections that should be made available. The most common usage of BIND is to associate the port number and server with a wildcard address (inaddr_any), which allows any interface to be used for incoming connections.
The common problem with BIND is trying to bind a port that is already in use. The trap is that there may not be an active socket, but it is still forbidden to bind the port (bind returns Eaddrinuse), which is caused by the TCP socket state time_wait. The status is retained for approximately 2-4 minutes after the socket is closed. After the time_wait state exits, the socket is deleted and the address can be re-bound without problems.
Waiting for time_wait to end can be annoying, especially if you are developing a socket server, you need to stop the server to make some changes, and then restart. Fortunately, there are ways to avoid the time_wait state. You can apply the SO_REUSEADDR socket option to sockets so that the ports can be reused immediately.
Consider the example in Listing 3. Before binding the address, I call setsockopt with the SO_REUSEADDR option. To allow address reuse, I set the integer parameter (on) to 1 (otherwise, it can be set to zero address reuse).
Use 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;
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 will allow immediate reuse of the address.
5 hidden dangers in the programming of Linux sockets
http://www.ibm.com/developerworks/cn/linux/l-sockpit/
Linux socket Programming Port problem (Bind error:address already in use)