1

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).

sehe
  • 374,641
  • 47
  • 450
  • 633
tookie009
  • 186
  • 1
  • 14
  • I tried to using standard network library accordingly to: https://stackoverflow.com/questions/14697963/tcp-ip-connection-on-a-specific-interface sockaddr_in localaddr = {0}; localaddr.sin_family = AF_INET; localaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); bind(this->sock, (sockaddr*)&localaddr, sizeof(localaddr)); It's working without exceptions. But I'm still interested in asio solution. – tookie009 Mar 07 '18 at 10:10
  • Edit your question instead of rambling in the comments. Also, in that alternative you "quote", you skipped the same, essential step that you missed in the Asio code (`SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);`, i.e. opening the socket) – sehe Mar 07 '18 at 12:17
  • 1
    Also, your code could be a ton simpler. Do not use `new` (you're definitely leaking the `io_service`). How many `io_service` instances are you planning on using? How are you going to manage many connections (since you're using blocking connects)? In other words, have you tried working through a [simple tutorial example](http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/examples.html) before you set about reinventing a load-balancing router? – sehe Mar 07 '18 at 12:32
  • I'm using connect, because I want to use gateway only to send data. So I will just sometimes call the send function. To asynchronous read data I will use the server thread. Anyway thanks for help – tookie009 Mar 07 '18 at 14:06

1 Answers1

3

socket::bind throws bad file descrption when socket was created but it is not open. You used constructor

basic_stream_socket(
boost::asio::io_service & io_service);

which constructs socket without opening it. You should call open method before calling bind or use one of overloaded version of socket constructor which creates and also opens socket.

rafix07
  • 20,001
  • 3
  • 20
  • 33