我正在创建多线程应用程序,它将作为"路由器"但在应用层。 该软件将在具有多个网络接口的多个主机上运行。
在我的应用程序中,在一个线程中运行服务器,它接受每个接口上的每个连接并将其传递给另一个工作线程,该工作线程决定是否转发消息以及应该使用哪个接口。
我决定使用boost-asio。
总结:仅在服务器中传入消息(仅在此处为读取功能),根据不同网关的类型(仅发送功能)传出。网关应始终连接到一个接口,因此我尝试使用bind方法。
但我得到一个例外:" bind:错误的文件描述符"
以下是代码段:
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();
}
我尝试了本地和远程端点的不同设置。服务器已经在另一个线程中运行,如果我没有执行绑定操作,它将从send函数获取已发送的消息(此处未包含)。
答案 0 :(得分:2)
socket::bind
在创建套接字但未打开时抛出错误的文件描述。你使用了构造函数
basic_stream_socket(
boost::asio::io_service & io_service);
构造套接字而不打开它。
你应该在调用open
之前调用bind
方法,或者使用一个重载版本的socket构造函数来创建并打开套接字。