<div dir="ltr">Thanks Gavin for the response, please see following embedded comments.<br><div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 24, 2017 at 1:04 PM, Gavin Lambert via Boost-users <span dir="ltr"><<a href="mailto:boost-users@lists.boost.org" target="_blank">boost-users@lists.boost.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">On 23/02/2017 23:09, jupiter via Boost-users wrote:<br> <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> I have a server socket connected by many clients using<br> boost::asio::ip::tcp::socket to set up server socket connection and to<br> use boost::asio::ip::tcp::acceptor async_accept for new client<br> connections. The client may come and go, so I need to check if the<br> connection is still valid or not before sending message to clients. How<br> can I check if a client connection is no longer valid? My server does<br> not seem get any indication when a client terminate the connection.<br> </blockquote> <br></span> There are a few platform-specific sockopts that sometimes do the trick, but generally the simplest way to test whether a connection is still alive is to try to transmit something -- if the connection is lost it will then either immediately or after a timeout fail with a "connection reset" error. You usually can't detect a disconnection if you're just waiting for a read without sending anything.<br></blockquote><div><br></div><div>That is exactly I am currently doing, if I failed to run boost::asio::async_write to a specific client, I removed the connection and wait until a new connection from the client is established. Doesn't seem there are other better detecting methods. <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <br> Also, if clients have some means of identification or authentication (using the IP is possible but not really recommended due to gatewaying or multiple separate applications), and provided you're expecting only one connection per client, then you could treat a reconnection from the same client as a disconnection of their previous connection. If clients auto-reconnect this would probably be the fastest method of identifying a lost connection, but it can be tricky to avoid causing problems if not implemented carefully.<br> <br></blockquote><div>Yes, I am also writhe the client program, each client has an unique identification and provides expecting only one connection per client. The client will auto-reconnect to the server if it detect lost connection, the question again, is writing to boost::asio::write the efficient way to detect a stalled connection or are there any better alternatives to get feedback from the boost::asio::ip::tcp::socket lowest_layer? The problem is the client is always waiting receiving from server, if the client cannot detect from the oost::asio::ip::tcp::socket while waiting on boost::asio::read, then I have to run constantly heartbeat in every second or so which I try to avoid too much unnecessary bandwidth usage as the client is replying on LTE transmission, network bandwidth cost needs be considered.<br><br></div><div>Thank you.<br></div><div><br><br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <br> ______________________________<wbr>_________________<br> Boost-users mailing list<br> <a href="mailto:Boost-users@lists.boost.org" target="_blank">Boost-users@lists.boost.org</a><br> <a href="http://lists.boost.org/mailman/listinfo.cgi/boost-users" rel="noreferrer" target="_blank">http://lists.boost.org/mailman<wbr>/listinfo.cgi/boost-users</a><br> </blockquote></div><br></div></div></div>