Search code examples
clinuxsockets

Bind failed: Address already in use


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?


Solution

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