Sorry for keeping posting questions about ASIO TCP socket reconnection regarding to network interface changes, I am very much stuck at the moment. As I described in other emails, when one network interface WiFi failed, the network interface Ethernet takes over, the client application using boost ASIO TCP socket cannot detect the broken of WiFi, it continually sent messages to the remote server via the broken WiFi interface without triggering errors but obviously the server could not receive messages any more. So my solution is prior to send message, it ping to 8.8.8.8 via the network interface WiFi, if it failed, it called the SetConnection again to close the socket binding to WiFi interface and to start reconnection to bind to Ethernet interface: void SetConnection(void) { if (this->mSocket.lowest_layer().is_open()) { std::cout << "Set connection close socket" << std::endl; this->mSocket.lowest_layer().close(); } boost::asio::ip::tcp::resolver resolver(this->mIoService); boost::asio::ip::tcp::resolver::query query(this->mHost, this->mPort); this->mIterator = resolver.resolve(query); this->mContext.load_verify_file("ssl/ca.pem"); this->mSocket.set_verify_mode(boost::asio::ssl::verify_peer); this->mSocket.set_verify_callback(boost::bind(&client::verify_certificate, this, _1, _2)); boost::asio::async_connect(mSocket.lowest_layer(), this->mIterator, boost::bind(&client::handle_connect, this, boost::asio::placeholders::error)); } But it did not work, the error: handle_connect connect failed: Network is unreachable What I am missing here? How can I make the TCP socket reconnection? Thank you. Kind regards, - jupiter
On 29/06/2019 12:35, JH wrote:
As I described in other emails, when one network interface WiFi failed, the network interface Ethernet takes over, the client application using boost ASIO TCP socket cannot detect the broken of WiFi, it continually sent messages to the remote server via the broken WiFi interface without triggering errors but obviously the server could not receive messages any more.
Receiving on a broken socket reports no errors. Trying to send on a broken socket, however, should normally return an error code. Are you sure you're checking in the right places?
So my solution is prior to send message, it ping to 8.8.8.8 via the network interface WiFi, if it failed, it called the SetConnection again to close the socket binding to WiFi interface and to start reconnection to bind to Ethernet interface: [...] But it did not work, the error: handle_connect connect failed: Network is unreachable
What I am missing here? How can I make the TCP socket reconnection?
Transition from one interface to another is not immediate. Whatever is handling the transition has to first notice that the original interface failed (typically with some timeout) and then perform the transition, which itself might take a while. (Ethernet link negotiation takes ~2 seconds typically for example, although that might not be a factor here depending on exactly what is happening.) When a connection fails, you need to keep retrying until it succeeds -- usually with a second or two delay between each retry (or exponential backoff) to avoid flooding the system with connection retry requests.
Mere moments ago, quoth I:
On 29/06/2019 12:35, JH wrote:
As I described in other emails, when one network interface WiFi failed, the network interface Ethernet takes over, the client application using boost ASIO TCP socket cannot detect the broken of WiFi, it continually sent messages to the remote server via the broken WiFi interface without triggering errors but obviously the server could not receive messages any more.
Receiving on a broken socket reports no errors. Trying to send on a broken socket, however, should normally return an error code. Are you sure you're checking in the right places?
Also note that depending on the type of break, it may take a long time (>=30 seconds in some cases) to get an error when sending.
Hi Gavin,
Also note that depending on the type of break, it may take a long time (>=30 seconds in some cases) to get an error when sending.
I have spent weeks to debug this problem, I waited many minutes, the program kept sending to broken connection, but no error occurred. That is real behavior of the boost socket, isn't? My connection class is able to keep connection peioridcally until it is connected, but that feature only works in first time connection, once it was connected, to close the socket, to re-open the socket, to start connection again, it never works. Here is what I test: - Connect to Ethernet cable, start asio socket application to initialize connection, it handle failure unit it is connected, then it can send data to server without any problems. - During the application is running, pull Ethernet cable out, a function to check network interface to find failure of Ethernet but WiFi was good, closed the socket, start reconnection, the socket was opened again, the connection was successful, but sending message to socket got an error of "stream truncated", in error handler, it kept calling the reconnection again, it was no longer to work. Appreciate any advice how to get that work. Thank you. Kind regards, - jh
participants (2)
-
Gavin Lambert
-
JH