|
Boost Users : |
Subject: Re: [Boost-users] [asio] Second connection fails
From: Florian Lindner (mailinglists_at_[hidden])
Date: 2018-06-21 07:01:55
Am 21.06.2018 um 05:01 schrieb William Jagels:
> Can you re-run your test code with handler tracking enabled and post the output? How to enable:
> https://www.boost.org/doc/libs/1_67_0/doc/html/boost_asio/overview/core/handler_tracking.html
Hey,
I already did, but it shows very little information for non-asnyc operations. Anyways, I managed to fix it yesterday and
it was my own stupid mistake, not related to boost asion.
Thanks a lot for your input!
Florian
>
> On Tue, Jun 19, 2018, 07:16 Florian Lindner via Boost-users <boost-users_at_[hidden]
> <mailto:boost-users_at_[hidden]>> wrote:
>
> 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 <http://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 mailing list
> Boost-users_at_[hidden] <mailto:Boost-users_at_[hidden]>
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
>
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