提升 unix 上的 UDP 套接字问题 - 绑定:地址已在使用中
Boost UDP socket issue on unix - bind: address already in use
首先,我知道同一主题还有其他几个线程,但我无法在这些线程中找到任何可以帮助我的东西,所以我会尝试非常具体地说明我的情况。
我已经设置了一个简单的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。如果这不起作用,请尝试将端口号更改为其他任意端口,并检查问题是否仍然存在。
相关文章:
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 在基于范围的for循环中使用结构化绑定声明
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 无法将套接字绑定到地址
- 如何使用 Poco::Net::HTTPSClientSession 绑定特定的源 IP 地址
- 绑定错误:地址已在使用中
- C/Python绑定:指针地址修改
- 如何使用OpenLDAP API选择LDAP客户端绑定到哪个地址
- 提升 unix 上的 UDP 套接字问题 - 绑定:地址已在使用中
- 绑定成员函数的地址
- 是否有可能绑定和监听一个IP地址与TCP/IP套接字?(Linux / C)
- 直接从数组读取时超出了绑定地址
- UDP绑定:地址已被使用
- 确定未绑定套接字的地址族
- WinSock连接前绑定导致WSAEADDRNOTAVAIL-请求的地址在其上下文中无效
- boost asio TCP 服务器必须绑定到 IP 地址
- ZeroMQ:重新绑定套接字时使用地址错误
- Windows套接字无法绑定VPN IP地址