[asio] Second connection fails
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(); }); }
On 19/06/2018 23:08, Florian Lindner wrote:
The host to wich I connect always takes on connection per process, that is way I think the problem is with the requester.
If you are trying to connect to the same server twice, and it only supports one active connection, it might either refuse a second connection or close its previous connection in favour of the new one.
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/hand... On Tue, Jun 19, 2018, 07:16 Florian Lindner via Boost-users < boost-users@lists.boost.org> 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(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@lists.boost.org https://lists.boost.org/mailman/listinfo.cgi/boost-users
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/hand...
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
mailto:boost-users@lists.boost.org> 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@lists.boost.org mailto:Boost-users@lists.boost.org https://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Florian Lindner
-
Gavin Lambert
-
William Jagels