“使用中的地址"之间的区别在 Windows 和 Linux 上使用 bind() - errno=98 [英] difference between "address in use" with bind() in Windows and on Linux - errno=98

查看:64
本文介绍了“使用中的地址"之间的区别在 Windows 和 Linux 上使用 bind() - errno=98的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个侦听端口的小型 TCP 服务器.在调试时,我通常会 CTRL-C 服务器以终止进程.

I have a small TCP server that listens on a port. While debugging it's common for me to CTRL-C the server in order to kill the process.

在 Windows 上,我可以快速重新启动服务并且可以重新启动套接字.在 Linux 上,我必须等待几分钟,然后 bind() 才能成功返回

On Windows I'm able to restart the service quickly and the socket can be rebound. On Linux I have to wait a few minutes before bind() returns with success

当 bind() 失败时,它返回 errno=98, address in use.

When bind() is failing it returns errno=98, address in use.

我想更好地了解实现中的差异.Windows 肯定对开发人员更友好,但我有点怀疑 Linux 做的是错误的事情".

I'd like to better understand the differences in implementations. Windows sure is more friendly to the developer, but I kind of doubt Linux is doing the 'wrong thing'.

我最好的猜测是 Linux 正在等待,直到所有可能的客户端都检测到旧套接字已损坏,然后才允许创建新套接字.它可以做到这一点的唯一方法是等待他们超时

My best guess is Linux is waiting until all possible clients have detected the old socket is broken before allowing new sockets to be created. The only way it could do this is to wait for them to timeout

在 Linux 开发过程中,有没有办法改变这种行为?我希望复制 Windows 这样做的方式

is there a way to change this behavior during development in Linux? I'm hoping to duplicate the way Windows does this

推荐答案

您想在 Linux 上的套接字上使用 SO_REUSEADDR 选项.相关的联机帮助页是 socket(7).这是其用法的示例.这个问题解释了会发生什么.

You want to use the SO_REUSEADDR option on the socket on Linux. The relevant manpage is socket(7). Here's an example of its usage. This question explains what happens.

这是此答案的副本.

在 Linux 上,SO_REUSEADDR 允许您绑定到一个地址,除非存在活动连接.在 Windows 上,这是默认行为.在 Windows 上,SO_REUSEADDR 允许您另外将多个套接字绑定到相同的地址.请参阅此处此处了解更多信息.

On Linux, SO_REUSEADDR allows you to bind to an address unless an active connection is present. On Windows this is the default behaviour. On Windows, SO_REUSEADDR allows you to additionally bind multiple sockets to the same addresses. See here and here for more.

这篇关于“使用中的地址"之间的区别在 Windows 和 Linux 上使用 bind() - errno=98的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆