469,963 Members | 2,054 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

Periodic "forcibly closed" error messages on Aynschronous server

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)
{
lock(this)
{
MsgState msgState=null;
try
{

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)
{
SocketClosed(msgState.socket);
return;
}

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

Nov 16 '05 #1
0 1266

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.