提升 asio 绑定:错误的文件描述符
boost asio bind: bad file descriptor
我正在创建多线程应用程序,它将作为 "router" 但在应用程序层上工作。
该软件将 运行 在具有多个网络接口的多台主机上运行。
在我的一个线程 运行s 服务器中的应用程序中,它接受每个接口上的每个连接并将其传递给另一个工作线程,该工作线程决定是否转发消息以及应该使用哪个接口。
我决定使用 boost-asio。
所以总结一下:传入消息仅在服务器中(此处仅读取功能),根据类型通过不同网关传出(仅发送功能)。网关应该始终连接到一个接口,因此我尝试使用 bind 方法。
但我得到一个例外:"bind: Bad file descriptor"
这是代码片段:
try
{
MY_LOG(trace) << "Connecting on IFace" << routingConf->connections[interface].ipSource;
boost::asio::io_service *svc = new boost::asio::io_service();
this->socket = new boost::asio::ip::tcp::socket(*svc);
boost::asio::ip::tcp::endpoint localEndpoint =
ip::tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 0);
boost::asio::ip::tcp::endpoint remoteEndpoint = ip::tcp::endpoint(
boost::asio::ip::address::from_string("127.0.0.1"), port);
this->socket->bind(localEndpoint);
MY_LOG(trace) << "socket success";
this->socket->connect(remoteEndpoint);
} catch (std::exception &e)
{
MY_LOG(fatal) << e.what();
}
我为本地端点和远程端点尝试了不同的设置。服务器已经运行在另一个线程中,如果我不执行绑定操作,它正在从发送函数(此处未包含)中获取发送的消息。
socket::bind
在创建套接字但未打开时抛出 错误文件描述。您使用了构造函数
basic_stream_socket(
boost::asio::io_service & io_service);
在不打开的情况下构建套接字。
您应该在调用 bind
之前调用 open
方法,或者使用创建并打开套接字的套接字构造函数的重载版本之一。
我正在创建多线程应用程序,它将作为 "router" 但在应用程序层上工作。 该软件将 运行 在具有多个网络接口的多台主机上运行。
在我的一个线程 运行s 服务器中的应用程序中,它接受每个接口上的每个连接并将其传递给另一个工作线程,该工作线程决定是否转发消息以及应该使用哪个接口。
我决定使用 boost-asio。
所以总结一下:传入消息仅在服务器中(此处仅读取功能),根据类型通过不同网关传出(仅发送功能)。网关应该始终连接到一个接口,因此我尝试使用 bind 方法。
但我得到一个例外:"bind: Bad file descriptor"
这是代码片段:
try
{
MY_LOG(trace) << "Connecting on IFace" << routingConf->connections[interface].ipSource;
boost::asio::io_service *svc = new boost::asio::io_service();
this->socket = new boost::asio::ip::tcp::socket(*svc);
boost::asio::ip::tcp::endpoint localEndpoint =
ip::tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 0);
boost::asio::ip::tcp::endpoint remoteEndpoint = ip::tcp::endpoint(
boost::asio::ip::address::from_string("127.0.0.1"), port);
this->socket->bind(localEndpoint);
MY_LOG(trace) << "socket success";
this->socket->connect(remoteEndpoint);
} catch (std::exception &e)
{
MY_LOG(fatal) << e.what();
}
我为本地端点和远程端点尝试了不同的设置。服务器已经运行在另一个线程中,如果我不执行绑定操作,它正在从发送函数(此处未包含)中获取发送的消息。
socket::bind
在创建套接字但未打开时抛出 错误文件描述。您使用了构造函数
basic_stream_socket(
boost::asio::io_service & io_service);
在不打开的情况下构建套接字。
您应该在调用 bind
之前调用 open
方法,或者使用创建并打开套接字的套接字构造函数的重载版本之一。