绑定失败:地址已在使用中

问题描述:

我正在尝试将套接字绑定到以下端口:

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");

但是它给出了:

$ ./serve   
Socket created    
bind failed. Error: Address already in use

为什么会出现此错误?

该错误通常表示您尝试打开的端口已被另一个应用程序使用.尝试使用netstat查看哪些端口已打开,然后使用可用端口.

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.

还要检查您是否绑定到正确的IP地址(我假设它是本地主机)

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