0
Answer

socket server crash after client disconnects and connects back

gizmokaka

gizmokaka

17y
1.9k
1
Hello everyone,

I wrote a socket server terminal in windows, that listens on a specific socket for client connection. after a client connects it calls a function "OnClientConnect" event driven, and creates a worker socket.
it passes the worker and an ASyncCallback function to onDataReceived, which takes the returned value from BeginRecieve and calls endRecieve with it.

the problem is:
if I disconnect the client from the network and then plug it back in, I recieve an exception in this line:
Code:
int n = theSockId.thisSocket.EndReceive(asyn);


here are a few lines I think may clarify things

Code:
m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null); : public void OnClientConnect(IAsyncResult asyn) { try { m_socWorker = m_socListener.EndAccept(asyn); WaitForData(m_socWorker); theNetwork.TheNetwork.gatewayType = Network.GatewayTypeE.TCP_GATEWAY; } : public void WaitForData(System.Net.Sockets.Socket soc) { try { if (pfnWorkerCallBack == null) pfnWorkerCallBack = new AsyncCallback(OnDataReceived); CSocketPacket theSocPkt = new CSocketPacket(); theSocPkt.thisSocket = soc; // now start to listen for any data... soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt); } : public void OnDataReceived(IAsyncResult asyn) { CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState; //end receive... int n = theSockId.thisSocket.EndReceive(asyn);


Any help will be most appreciated!!!

P.S
Just noticed that after reconnecting to the net, it's asyn in endRecieve is different from the one it was before disconnecting, it has no specific ip on which to communicate, but instead it's 0.0.0.0ortNO

something changes, I don't know what.
also I breakpointed the onDateRecived to see where it first falls, and it's in EndRecieve.