Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Address already in use while binding socket with address but the port number is shown free by `netstat`

Tags:

c

linux

sockets

I tried to bind my socket(server socket) at port number 8000. It worked and did the job for me. At the end of the code I close the socket as well. The very next instant I run my code again and it shows me that the address is already in use. I have printed the meaning of error values strerror(errno); to see if my code working properly at each point. To check if the port is free I checked it using netstat but it shows that port number 8000 is free. It has happened with me a lot of times. Every time I then wait for a few more secs and then it starts working again. I am using c language. So what is he reason for this behavior by my OS.

After a few more secs I run the code and then it works.

anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./a.out  Socket Creation: Success File open: Success Socket Bind: Address already in use Socket Listen: Address already in use ^C anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1348/lighttpd    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      984/sshd         tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1131/cupsd       tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1211/mysqld      tcp6       0      0 :::22                   :::*                    LISTEN      984/sshd         tcp6       0      0 ::1:631                 :::*                    LISTEN      1131/cupsd       anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./a.out  Socket Creation: Success File open: Success Socket Bind: Address already in use Socket Listen: Address already in use ^C anirudh@anirudh-Aspire-5920:~/Desktop/testing$  
like image 443
Durin Avatar asked Feb 24 '11 15:02

Durin


People also ask

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).

Why does socket bind fail?

If you're seeing a "TCP/UDP: Socket bind failed on local address" error message in the OpenVPN log, it means your VPN connection is configured to bind to a fixed local address and/or port number, and that this address/port number is unavailable.

Could not bind unix socket address already in use?

You will get this error when file exists during the bind . Either you should ensure to unlink/remove the file before exiting the application or you could always unlink it before bind. Check man page of bind. Also, note the example given in the man page at the end.


2 Answers

I've run into that same issue as well. It's because you're closing your connection to the socket, but not the socket itself. The socket can enter a TIME_WAIT state (to ensure all data has been transmitted, TCP guarantees delivery if possible) and take up to 4 minutes to release.

or, for a REALLY detailed/technical explanation, check this link

It's certainly annoying, but it's not a bug. See the comment from @Vereb on this answer below on the use of SO_REUSEADDR.

like image 144
icfantv Avatar answered Sep 20 '22 06:09

icfantv


I know its been a while since the question was asked but I was able to find a solution:

int sockfd; int option = 1; sockfd = socket(AF_INET, SOCK_STREAM, 0); setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); 

This set the socket able to be reused immediately.

I apologize if this is "wrong". I'm not very experienced with sockets

like image 32
Supamee Avatar answered Sep 18 '22 06:09

Supamee