995 - I / O completion port error message. The error occurs when you try to continue reading from a socket when it most likely has been closed.
Getting 0 bytes from EndRecieve means that the socket has been closed, like most exceptions that EndRecieve will EndRecieve .
You need to start dealing with these situations.
Never ignore exceptions; they are thrown for some reason.
Update
Nothing says that the server is doing something wrong. A connection can be lost for many reasons, because an idle connection is closed by a switch / router / firewall, etc. Network failures can also occur.
I want to say that you SHOULD handle disconnections. The right way to do this is to take apart the socket and try to plug in a new one at regular intervals.
As for the receive callback, the more correct way to handle it is something like this (semi pseudo code):
public void OnDataReceived(IAsyncResult asyn) { BLCommonFunctions.WriteLogger(0, "In :- OnDataReceived", ref swReceivedLogWriter, strLogPath, 0); try { SocketPacket client = (SocketPacket)asyn.AsyncState; int bytesReceived = client.thisSocket.EndReceive(asyn); //Here error is coming if (bytesReceived == 0) { HandleDisconnect(client); return; } } catch (Exception err) { HandleDisconnect(client); } try { string strHEX = BLCommonFunctions.ByteArrToHex(theSockId.dataBuffer); //do your handling here } catch (Exception err) { // Your logic threw an exception. handle it accordinhly } try { client.thisSocket.BeginRecieve(.. all parameters ..); } catch (Exception err) { HandleDisconnect(client); } }
the reason why I use three catch blocks is simply because the logic for the middle is different from the other two. Exceptions from BeginReceive / EndReceive usually indicate disconnected connectors, while exceptions from your logic should not stop socket reception.
source share