Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind failed: Address already in use

Tags:

c

linux

sockets

I am attempting to bind a socket to a port below:

if( bind(socket_desc,(struct sockaddr *) &server, sizeof(server)) < 0) {     perror("bind failed. Error");     return 1; } puts("bind done"); 

But it gives:

$ ./serve    Socket created     bind failed. Error: Address already in use 

Why does this error occur?

like image 459
TamiL Avatar asked Mar 04 '13 09:03

TamiL


People also ask

How do I fix address already in use BIND?

The Error “address already in use” occurred because some process was already running on the same port. So we can resolve the issue just by killing the process. To stop the process, we need the process ID (PID), which we can fetch using the lsof command.

How do you fix could not bind socket address and port are already in use?

To do so, open the program options by going to Edit -> Options -> Browsers and change the value of the WebSockets port. The same value must then also be set in the browser add-on (within the browser itself).

How do you stop the port which is already in use in Docker?

If port is in use by another container, stop it with docker-compose stop <service-name-in-compose-file> or remove it by replacing stop with rm . Run docker ps to see list of all containers running under your host. If you find the port is in use by another container, you can stop it with docker stop <container-id> .


2 Answers

Everyone is correct. However, if you're also busy testing your code your own application might still "own" the socket if it starts and stops relatively quickly. Try SO_REUSEADDR as a socket option:

What exactly does SO_REUSEADDR do?

This socket option tells the kernel that even if this port is busy (in the TIME_WAIT state), go ahead and reuse it anyway. If it is busy, but with another state, you will still get an address already in use error. It is useful if your server has been shut down, and then restarted right away while sockets are still active on its port. You should be aware that if any unexpected data comes in, it may confuse your server, but while this is possible, it is not likely.

It has been pointed out that "A socket is a 5 tuple (proto, local addr, local port, remote addr, remote port). SO_REUSEADDR just says that you can reuse local addresses. The 5 tuple still must be unique!" by Michael Hunter ([email protected]). This is true, and this is why it is very unlikely that unexpected data will ever be seen by your server. The danger is that such a 5 tuple is still floating around on the net, and while it is bouncing around, a new connection from the same client, on the same system, happens to get the same remote port. This is explained by Richard Stevens in ``2.7 Please explain the TIME_WAIT state.''.

like image 198
Joe Avatar answered Sep 23 '22 08:09

Joe


You have a process that is already using that port. netstat -tulpn will enable one to find the process ID of that is using a particular port.

like image 27
Ed Heal Avatar answered Sep 22 '22 08:09

Ed Heal