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$
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).
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.
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.
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With