I'm creating multithreading application, that will work as "router" but on application layer. The software will run on multiple hosts with multiple network interfaces.
In my application in one thread runs server, that accepts every connection on every interface and pass it to another worker-thread, which decide if the message will be forwarded and which interface schould be used.
I decided to use boost-asio.
So summarizing: incoming messages only in server (only here the read function), outgoing depending on typ through different gateway (only send function). The gateway should always connect to one interface, therefore I try to use the bind method.
But I get an exception: "bind: Bad file descriptor"
Here is the code snippet:
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();
}
I tried different settings for local and remote endpoint. The server is already running in another thread, and if I not perform the binding operation, it is getting the sent message from send function (not included here).