Linux 绑定失败:地址已被使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15198834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bind failed: Address already in use
提问by TamiL
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?
为什么会出现这个错误?
采纳答案by Techmonk
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.
该错误通常意味着您尝试打开的端口已被另一个应用程序使用。尝试使用 netstat 查看打开的端口,然后使用可用端口。
Also check if you are binding to the right ip address (I am assuming it would be localhost)
还要检查您是否绑定到正确的 IP 地址(我假设它是 localhost)
回答by Ed Heal
You have a process that is already using that port. netstat -tulpn
will enable one to find the process ID of that is using a particular port.
您有一个已经在使用该端口的进程。netstat -tulpn
将使人们能够找到使用特定端口的进程 ID。
回答by Joe
Everyone is correct. However, if you're also busy testing your code your ownapplication might still "own" the socket if it starts and stops relatively quickly. Try SO_REUSEADDRas a socket option:
每个人都是正确的。但是,如果您还忙于测试您的代码,您自己的应用程序可能仍然“拥有”套接字,如果它启动和停止的速度相对较快。尝试SO_REUSEADDR作为套接字选项:
What exactly does SO_REUSEADDR do?
This socket option tells the kernel that even if this port is busy (in the TIME_WAIT state), go ahead and reuse it anyway. If it is busy, but with another state, you will still get an address already in use error. It is useful if your server has been shut down, and then restarted right away while sockets are still active on its port. You should be aware that if any unexpected data comes in, it may confuse your server, but while this is possible, it is not likely.
It has been pointed out that "A socket is a 5 tuple (proto, local addr, local port, remote addr, remote port). SO_REUSEADDR just says that you can reuse local addresses. The 5 tuple still must be unique!" by Michael Hunter ([email protected]). This is true, and this is why it is very unlikely that unexpected data will ever be seen by your server. The danger is that such a 5 tuple is still floating around on the net, and while it is bouncing around, a new connection from the same client, on the same system, happens to get the same remote port. This is explained by Richard Stevens in ``2.7 Please explain the TIME_WAIT state.''.
SO_REUSEADDR 究竟是做什么的?
这个套接字选项告诉内核,即使这个端口很忙(在 TIME_WAIT 状态),仍然继续并重用它。如果它很忙,但处于其他状态,您仍然会收到地址已在使用错误。如果您的服务器已关闭,然后在其端口上的套接字仍处于活动状态时立即重新启动,这将很有用。您应该知道,如果出现任何意外数据,可能会混淆您的服务器,虽然这是可能的,但不太可能。
有人指出“一个套接字是一个 5 元组(proto、local addr、local port、remote addr、remote port)。SO_REUSEADDR 只是说你可以重用本地地址。5 元组仍然必须是唯一的!” 作者:迈克尔·亨特 ([email protected])。这是真的,这就是为什么您的服务器不太可能看到意外数据的原因。危险在于,这样的 5 元组仍然在网上漂浮,并且在它四处跳跃时,来自同一客户端、同一系统上的新连接碰巧获得了相同的远程端口。Richard Stevens 在“2.7 请解释 TIME_WAIT 状态”中对此进行了解释。
回答by Pradheep
As mentioned above the port is in use already. This could be due to several reasons
如上所述,该端口已在使用中。这可能是由于几个原因
- some other application is already using it.
- The port is in
close_wait
state when your program is waiting for the other end to close the program.refer (https://unix.stackexchange.com/questions/10106/orphaned-connections-in-close-wait-state). - The program might be in
time_wait
state. you can wait or use socket optionSO_REUSEADDR
as mentioned in another post.
- 其他一些应用程序已经在使用它。
close_wait
当您的程序正在等待另一端关闭程序时,端口处于状态。refer ( https://unix.stackexchange.com/questions/10106/orphaned-connections-in-close-wait-state)。- 程序可能处于
time_wait
状态。您可以等待或使用SO_REUSEADDR
另一篇文章中提到的套接字选项。
Do netstat -a | grep <portno>
to check the port state.
执行netstat -a | grep <portno>
检查端口状态。
回答by AyanG_WAP7
I was also facing that problem, but I resolved it. Make sure that both the programs for client-side and server-side are on different projectsin your IDE,in my case NetBeans. Then assuming you're using localhost, I recommend you to implement both the programs as two different projects.
我也遇到了这个问题,不过我解决了。 确保客户端和服务器端的程序都在IDE 中的不同项目中,在我的例子中是 NetBeans。然后假设您使用的是 localhost,我建议您将这两个程序作为两个不同的项目实施。
回答by prime
Address already in use
means that the port
you are trying to allocate for your current execution is already occupied/allocated to some other process.
Address already in use
意味着port
您尝试为当前执行分配的已被占用/分配给其他某个进程。
If you are a developer and if you are working on an application which require lots of testing, you might have an instance of your same application running in background (may be you forgot to stop it properly)
如果您是一名开发人员,并且您正在开发一个需要大量测试的应用程序,那么您可能会在后台运行相同应用程序的一个实例(可能是您忘记正确停止它)
So if you encounter this error, just see which application/process is using the port.
因此,如果您遇到此错误,只需查看哪个应用程序/进程正在使用该端口。
In linux try using netstat -tulpn
. This command will list down a process list with all running processes.
在 linux 中尝试使用netstat -tulpn
. 此命令将列出所有正在运行的进程的进程列表。
Check if an application is using your port.If that application or process is another important one then you might want to use another port which is not used by any process/application.
检查应用程序是否正在使用您的端口。如果该应用程序或进程是另一个重要的端口,那么您可能希望使用另一个未被任何进程/应用程序使用的端口。
Anyway you can stop the process which uses your port and let your application take it.
无论如何,您可以停止使用您的端口的进程并让您的应用程序使用它。
If you are in linux environment try,
如果你在linux环境下试试,
- Use
netstat -tulpn
to display the processes kill <pid>
This will terminate the process
- 使用
netstat -tulpn
显示进程 kill <pid>
这将终止进程
If you are using windows,
如果您使用的是窗户,
- Use
netstat -a -o -n
to check for the port usages - Use
taskkill /F /PID <pid>
to kill that process
- 使用
netstat -a -o -n
检查端口用途 - 使用
taskkill /F /PID <pid>
杀死进程
回答by AbdolHosein
It also happens when you have not give enough permissions(read and write) to your sock file!
当您没有为 sock 文件提供足够的权限(读取和写入)时,也会发生这种情况!
Just add expected permission to your sock contained folder and your sock file:
只需向包含 sock 的文件夹和 sock 文件添加预期权限:
chmod ug+rw /path/to/your/
chmod ug+rw /path/to/your/file.sock
Then have fun!
然后玩得开心!