Bind error: Address already in use
Although the process was forced to end with Ctrl+C, the error still exists. We can still see the process “forced to end” with Ctrl+C with netstat -an |grep 5120 and ps aux |grep 5120. The port is still in use, so we have to kill the process with kill every time, which is very troublesome. Last night, I happened to browse an article titled “5 Hidden Dangers in Linux Socket Programming” on THE IBM website. I suddenly realized that I had a try today and solved the problem as expected. I hereby express my thanks and hope that more coder can read this article and avoid making mistakes.
The main codes are:
int on;
on = 1;
ret = setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, & on, sizeof(on) );
now every time I force the process to end with Ctrl+C, I can still see the port in use with netstat and ps, but the running program will not have the error of “Address already in use”, so the port is reused.
Here is the third pitfall in this article — incorrect address usage
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 your server Settings to limit the interfaces that may have a connection coming. You can also use this function in client Settings to limit the interfaces that should be used for outgoing connections. The most common use of BIND is to associate a port number with a server and use a wildcard address (INADDR_ANY), which allows any interface to be used for incoming connections.
A common problem with BIND is trying to bind to a port that is already in use. The trap is that no active socket may exist, but the binding port is still disabled (bind returns EADDRINUSE), which is caused by the TCP socket state TIME_WAIT. This state remains for about 2 to 4 minutes after the socket is closed. After the TIME_WAIT state exits, the socket is deleted and the address can be rebound without problems.
Waiting for TIME_WAIT to end can be annoying, especially if you’re developing a socket server and need to stop the server to make some changes, and then restart. Fortunately, there is a way to get around the TIME_WAIT state. 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 the address, I call setsockopt with the SO_REUSEADDR option. To allow address reuse, I set the integer parameter (on) to 1 (otherwise, I can set it to 0 to prohibit address reuse).
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;
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 reuse of the address.
Five pitfalls in Linux socket programming
http://www.ibm.com/developerworks/cn/linux/l-sockpit/
Read More:
- [Windows] Socket Server Failed to bind, error 10048
- Creating Server TCP listening socket 127.0.0.1:6379: bind: No error。。。 Solutions to problems encountered in startup
- nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
- Solution to the problem that listen TCP 0.0.0.0:3306: bind: address already in use port is occupied in Linux centos7
- Python TCP socket programming: send returns broken pipe error?
- [exception] address already in use: bind
- Error: transport error 202: bind failed: address already in use
- Eclipse port occupied( java.net.BindException : address already in use: bind) solution
- Creating Server TCP listening socket 127.0.0.1:6379: bind: No error
- A solution to the error of asynchronous socket error 10053 in C + + Builder
- Failed to set up listener: socketexception: address already in use
- Windows redis startup error reported: Creating Server TCP listening socket 127.0.0.1:6379: bind: No error
- The socket connection was aborted in WCF
- How to solve oserror: [errno 98] address already in use and kill the python3 process
- JMeter: java.net.bindexception: address already in use: connect solution
- There will be row spacing problems in algorithm use in latex. Please use the ‘setstretch {1.35}
- filezilla Failed to create listen socket on port 21 for IPv4 solution
- Error: listen eadrinuse: address already in use
- Developing in Android java.net.SocketException : socket failed: eacces (permission denied) reported an error
- Error running ‘Tomcat 9‘: Address localhost:1099 is already in use