I'm building the c#
example here https://github.com/coolaj86/fizzbuzz/blob/master/tcp-echo-csharp/Main.cs and running it against netcat
.
When I exit netcat
with CTRL + C it closes the socket and I get
Unhandled Exception: System.Net.Sockets.SocketException: Connection reset by peer
at System.Net.Sockets.Socket.EndReceive (IAsyncResult result) [0x00000] in <filename unknown>:0
at echo.AsynchronousSocketListener.ReadCallback (IAsyncResult ar) [0x00000] in <filename unknown>:0
In other languages I've used, the FIN is represented as a read of 0 bytes (or an event called 'FIN' or 'end', etc), but I've modified the example to print out a message for a read of 0 bytes and I see no message.
How to I handle the client closing the connection?
I'm looking for an answer such as "Use System.Net.Foo.Bar.CloseHandler()
or System.Net.Foo.Bar.ErrorHandler()
".
I'm not sure, but I think there is no way to do it gracefully. Because if connection close properly, you get notification about client disconnection. But interrupting program execution by CTRL + C isn't this case. So I advise you just handle exception to workout this problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.