By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,391 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,391 IT Pros & Developers. It's quick & easy.

Periodic "forcibly closed" error messages on Aynschronous server

P: n/a
I have written a fairly simple c# server that uses asynchronous
sockets. I have two different clients that connect and send/receive
info from the server. Both exihibit this behaviour where everything
will work fine for a long while, but eventually the OnDataReceived
callback method throws an exception on the EndReceive method, with
socket exception "An existing connection was forcibly closed by the
remote host". I am reasonably sure the connection was not actually
closed by the remoate host, in fact the remove host also reports that
the remote host closed the connect (lol!)

Anyway, are there any known C# issues regarding this behavior? What
code mistakes could cause this?

I have so far been unable to reproduce it with the server running
within my lan,it only happens when the server is out on our hosted
machine out on the internet. Could firewalls/routers be severing
socket connections periodically? Wouldn't I see evidence of this in
other applications like remote desktop/instant messenger? Or do they
all have built in reconnect logic?

Here is my OnDataReceived code:
public void OnDataReceived(IAsyncResult asyn)
MsgState msgState=null;

msgState = (MsgState)asyn.AsyncState ;

int bytesReceived = 0 ;
bytesReceived = msgState.socket.EndReceive (asyn);
msgState.pos += bytesReceived;

//A result of 0 implies that the socket has died
if (bytesReceived == 0)

//Check the type of message, handle it, then wait for the next
chunk of data
msgState = processMessage(msgState);
catch (ObjectDisposedException ode)
listBoxTraffic.Items.Add("ODE Exception follows:");
catch(SocketException se)
listBoxTraffic.Items.Add("Socket Exception follows:");
listBoxTraffic.Items.Add("msg pos:"+msgState.pos+" msg
exp:"+msgState.expectedSize+" socket:"+msgState.socket);

Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.