At the first launch, the bandage passes successfully, when the program is restarted, it screams that the address is already in use, the address is released after a couple of minutes.

If you do not call shutdown and close, then everything will be successful every time (when the program is restarted, of course)

boost::asio::io_service _ioService; boost::asio::ip::tcp::socket _socket(_ioService); boost::system::error_code err; _socket.open(boost::asio::ip::tcp::v4(), err); _socket.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string("127.0.0.1"), 1250), err); if (err.value()) { cout << err.value() << endl; cout << err.message() << endl; } _socket.connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string("127.0.0.1"), 1500), err); //_socket.shutdown(_socket.shutdown_both); //_socket.close(err); 
  • Those. Are you running a second copy of the program? Or do you first close and restart it again and then go "scream"? - Max ZS
  • @MaxZS, first close and restart it - Kopkan
  • Connections on the other hand are closed by this moment or are they still hanging? If the other party has not closed the connection up to this point, then how would the behavior be correct and logical. On your side, the connection is closed and reconnecting may result in fragments of the message sent by the other party that are roaming the network. Therefore, bind again only after a couple of minutes. Well, if you do not close the connection, then this is a sign of a temporary loss of communication (well, the program flew out) and you, having restored the connection, continue to accept everything from the other side. - Max ZS
  • @MaxZS, why does everything work out well with a standard destructor (without calling close)? Yes, and I didn’t see such behavior with winsocks, and when I close the program, Windows cleans everything normally. - Kopkan
  • I described that if you do not explicitly close the connection, then, let's say, the network believes that the socket is still in use, you can send data to it, but the program could just temporarily fly out and the connection is about to be restored. It was with a boost, I did not come across, but here in the usual sockets (vinsok) everything is the same. Maybe you just did not pay attention, the other side is open or closed. And in ordinary sockets there is even a special option for this situation - SO_REUSEADDR . If you can use socket options in boost, this will be the solution to your question. - Max ZS

0