提升 unix 上的 UDP 套接字问题 - 绑定:地址已在使用中

Boost UDP socket issue on unix - bind: address already in use

本文关键字:地址 绑定 上的 unix UDP 套接字 问题 提升      更新时间:2023-10-16

首先,我知道同一主题还有其他几个线程,但我无法在这些线程中找到任何可以帮助我的东西,所以我会尝试非常具体地说明我的情况。

我已经设置了一个简单的UDP客户端/UDP服务器对,负责在几个并行模拟之间发送数据。也就是说,模拟器的每个实例都在单独的线程中运行,并在 UDP 套接字上发送数据。在主线程中,服务器正在运行并在模拟之间路由消息。

服务器代码的(对于此问题(重要部分如下所示:

UDPServer::UDPServer(boost::asio::io_service &m_io_service) :
   m_socket(m_io_service, udp::endpoint(udp::v4(), PORT_NUMBER)),
   m_endpoint(boost::asio::ip::address::from_string("127.0.0.1"), PORT_NUMBER)
{
   this->start_receive();
};
void UDPServer::start_receive() {
   // Set SO_REUSABLE to true
   boost::asio::socket_base::reuse_address option(true);
   this->m_socket.set_option(option);
   // Specify what happens when a message is received (it should call the handle_receive function)
   this->m_socket.async_receive_from(   boost::asio::buffer(this->recv_buffer),
                                        this->m_endpoint,
                                        boost::bind(&UDPServer::handle_receive, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
};

这在我的Windows工作站上工作正常。

问题是;我希望能够在 linux 集群上运行它,这就是我编译它并尝试在集群节点上运行它的原因。代码编译顺利,但是当我尝试运行它时,出现错误

bind: address already in use

我使用了高于 1024 的端口号,并已验证它未被其他程序使用。如上所示,我还设置了reuse_address选项,所以我真的不知道还有什么问题。

要移植使用SO_REUSEADDR您需要在将套接字绑定到通配符地址之前设置该选项:

UDPServer::UDPServer(boost::asio::io_service &m_io_service) :
   m_socket(m_io_service, udp::v4()),
   m_endpoint()
{
   boost::asio::socket_base::reuse_address option(true);
   this->m_socket.set_option(option);
   this->m_socket.bind(udp::endpoint(udp::v4(), PORT_NUMBER));
   this->start_receive();
}

在您的原始代码中,采用endpoint构造、打开并绑定套接字的构造函数在一行中 - 它简洁但不是很灵活。在这里,我们在构造函数调用中构造和打开套接字,然后在设置选项后绑定它。

顺便说一句,如果您只是将其用作async_receive_from的 out 参数,那么初始化m_endpoint就没有多大意义。

尝试在 Linux 上运行以下命令,以查看该端口是否已被其他程序使用。

netstat -antup | grep 1024

如果您得到"地址已在使用中",那么它肯定被其他程序使用。如果上述命令产生一些结果,则终止命令中报告的进程 ID。如果这不起作用,请尝试将端口号更改为其他任意端口,并检查问题是否仍然存在。