简体   繁体   English

地址已经与boost asio接受器一起使用

[英]Address already in use with boost asio acceptor

I wrote a server that is listening for incomming TCP connections and clients connecting to it. 我写了一个服务器,它正在监听TCP连接和连接它的客户端。 When I shut down the server and restart it on the same port, I sometimes get the error message EADDRINUSE when calling bind(...) (error code: 98 on Linux). 当我关闭服务器并在同一端口重新启动它时,我有时会在调用bind(...)时收到错误消息EADDRINUSE(Linux上的错误代码:98)。 This happens even though I am setting the option to reuse the socket. 即使我设置了重用套接字的选项,也会发生这种情况。

The error does not happen all the time, but it seems that it happens more often when clients are connected to the server and sending data while it shuts down. 错误不会一直发生,但似乎当客户端连接到服务器并在关闭时发送数据时,它会更频繁地发生。 I guess the problem is that there are still pending connections while the server is shut down (related topic: https://stackoverflow.com/questions/41602/how-to-forcibly-close-a-socket-in-time-wait ). 我想问题是服务器关闭时仍有未决连接(相关主题: https//stackoverflow.com/questions/41602/how-to-forcibly-close-a-socket-in-time-wait )。

On the server side, I am using boost::asio::ip::tcp::acceptor. 在服务器端,我使用boost :: asio :: ip :: tcp :: acceptor。 I initialize it with the option "reuse_address" (see http://beta.boost.org/doc/libs/1_38_0/doc/html/boost_asio/reference/basic_socket_acceptor.html ). 我使用选项“reuse_address”对其进行初始化(请参阅http://beta.boost.org/doc/libs/1_38_0/doc/html/boost_asio/reference/basic_socket_acceptor.html )。 Here is the code snippet: 这是代码片段:

using boost::asio::ip::tcp;
acceptor acceptor::acceptor(io_service);
endpoint ep(ip::tcp::v4(), port);
acceptor.open(ep.protocol());
acceptor.set_option(acceptor::reuse_address(true));
acceptor.bind(ep);
acceptor.listen();

The acceptor is closed with: 接受者关闭时:

acceptor.close();

I also tried using acceptor.cancel() before that, but it had the same effect. 我之前也尝试过使用acceptor.cancel(),但它具有相同的效果。 When this error occurred, I cannot restart the server on the same port for quite some time. 发生此错误时,我无法在同一端口上重启服务器很长一段时间。 Restarting the network helps, but is not a permanent solution. 重新启动网络有帮助,但不是永久解决方案。

What am I missing? 我错过了什么?

Any help would be greatly appreciated! 任何帮助将不胜感激! :) :)

These were originally a comment to the question. 这些最初是对这个问题的评论。


does your server fork child processes? 你的服务器分叉子进程? Also, are you sure the socket is in TIME_WAIT state? 此外,您确定套接字处于TIME_WAIT状态吗? You might want to grab the netstat -ap output when this happens 当发生这种情况时,您可能想要获取netstat -ap输出

When you solve these problems "by force", it seems you are calling problems on your head, do not you? 当你“强行”解决这些问题时,你的脑子里似乎有问题,不是吗?

There is a reason the default behavior requires you to wait, otherwise the network could for example confuse the ACK from the previous connection to be ACK for the new connection. 有一个原因是默认行为要求您等待,否则网络可能会将来自先前连接的ACK混淆为新连接的A​​CK。

I would not allow this "solution" to be included in release builds in my team. 我不允许将这个“解决方案”包含在我的团队的发布版本中。

Remember, when the probability of error is very low, testing is extremely difficult! 请记住,当错误概率非常低时,测试非常困难!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM