Boost::asio cleanly disconnecting?

I think you should probably have a call to socket. Shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec) in there before the call to socket.close().

I think you should probably have a call to socket. Shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec) in there before the call to socket.close(). The boost::asio documentation for basic_stream_socket::close states: For portable behaviour with respect to graceful closure of a connected socket, call shutdown() before closing the socket.

This should ensure that any pending operations on the socket are properly cancelled and any buffers are flushed prior to the call to socket.close.

Maybe this is what is happening: Client send disconnect packet Client shuts socket down Server read handler gets called, but there is an error associated with the shutdown packet because the socket is already closed. I see in your read handlers, if there is an error, you never check to see if your shutdown packet is there. Maybe it is.

Basically what I'm saying is maybe your client sometimes is able to send both the close and the shutdown packet before the server has a chance to process them separately.

Error "thrown" here, len always = 0 in problem cases..." so its always read 0 bytes of the header, ie it hasn't read any of the packet... And sticking a Sleep(500) or something on the client isn't a good solution, because that still may not always be enough on slower networks and is a noticeable delay. – Fire Lancer Jan 3 '10 at 7:33 Perhaps wait for an OK package from the server, or have the server disconnect instead? – Macke Jan 3 '10 at 12:49 But if the client then waits for the server to send a disconnect message (in response to the client disconnect message), I'll just end up with the opposite problem of the server disconnecting on the client.... – Fire Lancer Jan 3 '10 at 16:01 Would you be able to use the disconnect error alone or do you really need the disconnect packet for something?

– Chris H Jan 3 '10 at 22:48.

Use async_write() and put socket.close() inside of write handler. This will make sure packet is processed by boost asio and not neglected in the middle of processing (because of close() calls).

I have a very similar issue. I believe it's related to Windows recycling connections. Is the following familiar?

You get this error immediately upon starting the program but not after a connection is established? The error never happens if you wait more than 4 minutes before restarting your application? The tcp specs specify that by default it should wait four minutes for the final acknowledgment when a tcp connection is closed.

You can see these connections in FIN_WAIT state using netstat. The Windows OS detects when you try to connect to the exact same system and takes these partially closed connections and recycles them. Your second invocation of the program gets the 'closed' connection left behind by the first run.It gets the next acknowledge and then really closes.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions