98

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?

gobernador
  • 5,659
  • 3
  • 32
  • 51
TamiL
  • 2,706
  • 4
  • 24
  • 44
  • 2
    Use a different port number? – Nick Mar 04 '13 at 10:01
  • 3
    Use an address that isn't already in use. – David Schwartz Mar 04 '13 at 10:02
  • 1
    I got it.. I choose different ports... Thanks for the help .. Thanks all. – TamiL Mar 04 '13 at 10:18
  • 2
    I faced the same issue when I closed the server program with client program still running. This put the socket into `TIME_WAIT` state. Here's an elaborate discussion of the problem: [How to forcibly close a socket in TIME_WAIT?](http://serverfault.com/questions/329845/how-to-forcibly-close-a-socket-in-time-wait) – narendra-choudhary Apr 05 '16 at 06:10
  • 7
    Why was this question closed? It covers a common issue for socket programming, and the the questions' answers are useful and correct. It certainly helped me. – Elliott Beach Apr 22 '17 at 17:43
  • The top-voted answer was extremely helpful to me, this seems like a relevant question to me – Jack V. Jun 29 '17 at 16:10

12 Answers12

95

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 (mphunter@qnx.com). 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.''.

Joe
  • 7,378
  • 4
  • 37
  • 54
  • 3
    https://easyengine.io/tutorials/nginx/troubleshooting/emerg-bind-failed-98-address-already-in-use/ Above link only fixed my problem... Though I used (SO_REUSEADDR | SO_REUSEPORT) both this problem occurred. – iDebD_gh Mar 04 '16 at 09:47
  • 2
    @iDebD_gh SO_REUSEPORT is only for UDP. – user207421 Apr 23 '17 at 10:44
  • 4
    Be aware SO_REUSEADDR can have negative ramifications if the port in question is behind a Load Balancer. – kmarsh May 24 '17 at 18:40
66

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.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • `netstat: n: unknown or uninstrumented protocol` – Dr.jacky Sep 07 '22 at 10:31
  • @Dr.jacky I get the same error on macOS. If you try removing the "n", you will get a more helpful message that you need to specify a protocol for that "p". `netstat -tulp TCP` will run, but ultimately it's wrong because the macOS and Linux versions of `netstat` are very different and the parameters mean completely different things. Look for the answers about `lsof` which does the same thing on macOS. – Aaron Oct 04 '22 at 02:43
64

Address already in use means that the port you are trying to allocate for your current execution is already occupied/allocated to some other process.

If you are a developer and if you are working on an application which require lots of testing, you might have an instance of your same application running in background (may be you forgot to stop it properly)

So if you encounter this error, just see which application/process is using the port.

In linux try using netstat -tulpn. This command will list down a process list with all running processes.

Check if an application is using your port. If that application or process is another important one then you might want to use another port which is not used by any process/application.

Anyway you can stop the process which uses your port and let your application take it.

If you are in linux environment try,

  • Use netstat -tulpn to display the processes
  • kill <pid> This will terminate the process

If you are using windows,

  • Use netstat -a -o -n to check for the port usages
  • Use taskkill /F /PID <pid> to kill that process
prime
  • 14,464
  • 14
  • 99
  • 131
51

The error usually means that the port you are trying to open is being already used by another application. Try using netstat to see which ports are open and then use an available port.

Also check if you are binding to the right ip address (I am assuming it would be localhost)

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
Techmonk
  • 1,459
  • 12
  • 20
  • 1
    It can also be that you are running a web debugging proxy like fiddler (http://www.telerik.com/fiddler). I was getting that same error on my machine, I closed fiddler, and was able to proceed just fine. – Farskeptic Apr 05 '14 at 13:21
  • 2
    It can also be that there are leftover ports in TIME-WAIT state. If the IP address was wrong the error would have been 'cannot assign requested address', and the default that should be assumed is not `localhost` but INADDR_ANY. – user207421 Apr 23 '17 at 10:44
  • 1
    For `python` or `python3` based apps, just close all such apps and then proceed. Use `pkill -9 python` & `pkill -9 python3` – Pe Dro Sep 08 '21 at 06:38
18

if address is already in use can you just want to kill whoso ever process is using the port, you can use

lsof -ti:PortNumberGoesHere | xargs kill -9

source and inspiration this.

PS: Could not use netstat because it not installed already.

best wishes
  • 5,789
  • 1
  • 34
  • 59
14

As mentioned above the port is in use already. This could be due to several reasons

  1. some other application is already using it.
  2. The port is in close_wait state when your program is waiting for the other end to close the program.refer (https://unix.stackexchange.com/questions/10106/orphaned-connections-in-close-wait-state).
  3. The program might be in time_wait state. you can wait or use socket option SO_REUSEADDR as mentioned in another post.

Do netstat -a | grep <portno> to check the port state.

Community
  • 1
  • 1
Pradheep
  • 3,553
  • 1
  • 27
  • 35
  • 14
    How can we manually stop this application at this port number? – ypahalajani Apr 20 '16 at 06:07
  • CLOSE-WAIT means the port is waiting for this end to close it, not the remote end. That's what your link says too. – user207421 Apr 23 '17 at 09:30
  • `CLOSE_WAIT` means your program is still running, and hasn't closed the socket (and the kernel is waiting for it to do so). `SO_REUSEADDR` is for servers and `TIME_WAIT` sockets, so doesn't apply here – helloworld Apr 07 '20 at 02:31
  • Windows: `netstat -ano | findstr ":8080"` (to find pid - last column - for port) and then `taskkill /F /PID ` to kill the process with the pid. – felvhage Aug 07 '20 at 13:41
2

It also happens when you have not give enough permissions(read and write) to your sock file!

Just add expected permission to your sock contained folder and your sock file:

 chmod ug+rw /path/to/your/
 chmod ug+rw /path/to/your/file.sock

Then have fun!

AbdolHosein
  • 528
  • 4
  • 15
2

Check for running process pid:

pidof <process-name>

Kill processes:

sudo kill -9 process_id_1 process_id_2 process_id_3
cormacncheese
  • 1,251
  • 1
  • 13
  • 27
1

I was also facing that problem, but I resolved it. Make sure that both the programs for client-side and server-side are on different projects in your IDE, in my case NetBeans. Then assuming you're using localhost, I recommend you to implement both the programs as two different projects.

thatlittlegit
  • 69
  • 1
  • 8
ayan-cs
  • 41
  • 6
1

First of check which port are listening,

netstat -tlpn

then select available port to conect,

sudo netstat -tlpn | grep ':port'

Fix it into also to your server and clients interfaces. Go Barrier tab -> change settings, -> port value type -> save/ok

enter image description here

Check both clients and server have similar port values

Then Reload.

Now it should be ok.

makt
  • 89
  • 2
  • 15
0

To terminate all node processes:

killall -9 node
MD SHAYON
  • 7,001
  • 45
  • 38
0

I was supposed to create a tunnel using ssh -L option, leave it running in a terminal, and then use ssh -p in another terminal to make an ssh connection via the tunnel.

I did a ctrl+r, which usually gets me the ssh -p. But, since I restarted my laptop the ctrl+r just gave me the ssh -L command again.

So, I was doing ssh -L twice and wondering why the second command was not working :-P

Teddy
  • 4,009
  • 2
  • 33
  • 55