Boost logo

Boost Users :

Subject: [Boost-users] [asio] Second connection fails
From: Florian Lindner (mailinglists_at_[hidden])
Date: 2018-06-19 11:08:04


Hello,

I am using boost.asio to connect a self written host. The function below is called with acceptorRanks = {0, 1}. The
first connection went well, but the second one fails (to 1). It is always the second with, e.g. when called with {1, 0},
connecting to 0 fails.

I took the code out of context, changed some lines for clarity, I hope you can help me!

The sockets seems to connect, i.e. there is no error, but the asio::write fails with "Bad file descriptor"

Wireshark only shows traffic for the first connections, nothing at all the second (when filtered by port name). The
connection is always on localhost.

The host to wich I connect always takes on connection per process, that is way I think the problem is with the requester.

I would be really thankful for any help what the problem could be.

Best Thanks!
Florian

Same code: https://gist.github.com/floli/d418405ca50d722f1abf8bf5971466e2

void requestConnection(std::string const &nameAcceptor,
                       std::string const &nameRequester,
                       std::set<int> const &acceptorRanks,
                       int requesterRank)

{
  boost::asio::ip::tcp::resolver resolver(*_ioService);

  for (auto const & acceptorRank : acceptorRanks) {
    try {
      address = readAddress(nameAcceptor, nameRequester, acceptorRank);
      DEBUG("Requesting connection to " << address);

      std::string ipAddress = address.substr(0, address.find(":"));
      std::string portNumber = address.substr(ipAddress.length()+1, address.length() - ipAddress.length()-1);

      _portNumber = static_cast<unsigned short>(std::stoi(portNumber));

      PtrSocket socket(new Socket(*_ioService)); // shared_ptr to socket

      using asio::ip::tcp;

      tcp::resolver::query query(tcp::v4(), ipAddress, portNumber);

      while (not isConnected()) {
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        boost::system::error_code error = asio::error::host_not_found;
        boost::asio::connect(*socket, endpoint_iterator, error);

        _isConnected = not error;

        if (not isConnected()) {
          // Wait a little, since after a couple of ten-thousand trials the system
          // seems to get confused and the requester connects wrongly to itself.
          boost::asio::deadline_timer timer(*_ioService, boost::posix_time::milliseconds(1));
          timer.wait();
        }
      }

      DEBUG("Requested connection to " << address << ", rank = " << acceptorRank);
      _sockets[acceptorRank] = socket;
      WARN("CHECKPOINT 1 FOR RANK " << acceptorRank);
      asio::write(*_sockets.at(acceptorRank), asio::buffer(&requesterRank, sizeof(int)));
      WARN("CHECKPOINT 2 FOR RANK " << acceptorRank); // reached only for acceptorRank=0
    } catch (std::exception &e) {
      ERROR("Requesting connection to " << address << " failed: " << e.what());
    }
  }
  // NOTE:
  // Keep IO service running so that it fires asynchronous handlers from another thread.
  _work = PtrWork(new asio::io_service::work(*_ioService));
  _thread = std::thread([this]() { _ioService->run(); });
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net