已地址在使用中:绑定 - 升压UNIX上的UDP套接字问题 [英] Boost UDP socket issue on unix - bind: address already in use
问题描述
首先,我知道有同一主题的几个其他线程,但我无法找到任何东西的那些可以帮助我,所以我会尝试是非常具体与我的情况。
First of all, I know there are several other threads on the same theme, but I was unable to find anything in those that could help me so I'll try to be very specific with my situation.
我已经建立了一个简单的UDP客户端/服务器的UDP对,负责几个平行模拟之间发送数据。即,模拟器的每个实例是在一个单独的线程运行,并且在UDP套接字发送数据。在主线程服务器正在运行和路线模拟之间的消息。
I have set up a simple UDP Client / UDP Server pair that is responsible to send data between several parallel simulations. That is, every instance of the simulator is running in a separate thread and send data on a UDP socket. In the master thread the server is running and routes the messages between the simulations.
服务器code的(这个问题)重要的部分是这样的:
The (for this problem) important parts of the server code looks like this:
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工作站上。
This works fine on my windows workstation.
问题是;我希望能够在Linux集群,这就是为什么我编译它,并试图在群集节点上运行上运行此。在code编译顺利,但是当我尝试运行它,我得到的错误
The thing is; I want to be able to run this on a linux cluster, which is why I compiled it and tried to run it on a cluster node. The code compiled without a hitch, but when I try to run it I get the error
bind: address already in use
我使用1024以上的端口号,并且已经验证,它不是由另一程序使用。正如上面看到的,我还设置了 reuse_address
选项,所以我真的不知道还有什么可能是错的。
I use a port number above 1024, and have verified that it is not in use by another program. And as is seen above, I also set the reuse_address
option, so I really don't know what else could be wrong.
推荐答案
要<一个href=\"http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t\">portably使用 SO_REUSEADDR
你需要的插座绑定到通配符地址之前的选项设置:
To portably use SO_REUSEADDR
you need to set the option before binding the socket to the wildcard address:
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();
}
在原来的code,这需要一个终端
构造,将打开,并在一行结合插座的构造 - 它简洁,但不是很灵活。在这里,我们正在建设,并在构造函数调用打开插座,然后再结合之后,我们设置的选项。
In your original code, the constructor that takes an endpoint
constructs, opens and binds the socket in a single line - it's concise but not very flexible. Here we're constructing and opening the socket in the constructor call, and then binding it later after we set the option.
顺便说一句,没有太多的点初始化 m_endpoint
如果你只是打算用它作为的out参数 async_receive_from
反正。
As an aside, there's not much point initialising m_endpoint
if you're just going to use it as the out argument of async_receive_from
anyway.
这篇关于已地址在使用中:绑定 - 升压UNIX上的UDP套接字问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!