Problem
I’m trying to bind a socket to the port listed 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?
Asked by TamiL
Solution #1
Every single person 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:
Answered by Joe
Solution #2
That port is already in use by another process. netstat -tulpn can be used to identify the process ID of a process that is using a specific port.
Answered by Ed Heal
Solution #3
The error usually indicates that the port you’re attempting to open is already in use by another program. Utilize netstat to determine which ports are available and then use one of them.
Check to see if you’re using the correct IP address (I am assuming it would be localhost)
Answered by Techmonk
Solution #4
The port you’re seeking to assign for your current execution is already occupied/allocated to another process, which indicates it’s already in use.
So, if you get this problem, look to see what application or process is using the port.
Use netstat -tulpn in Linux to see what’s going on. This command displays a list of all currently running processes.
Check to see if a program is using your port. If that program or process is critical, you may wish to use a port that is not in use by any other process or application.
In any case, you can terminate the process that is using your port and allow your program to use it.
If you’re working in a Linux environment, give it a shot.
If you’re using a PC with Windows,
Answered by prime
Solution #5
As previously stated, the port is already in use. This could be attributed to a variety of factors.
Do netstat -a | grep to check the port state.
Answered by Pradheep
Post is based on https://stackoverflow.com/questions/15198834/bind-failed-address-already-in-use