all groups > dotnet distributed apps > october 2004 >
You're in the

dotnet distributed apps

group:

Error when client disconnects (Asynch Sockets)



Error when client disconnects (Asynch Sockets) polyfractal NO[at]SPAM gmail.com
10/26/2004 6:49:11 PM
dotnet distributed apps: Hi,

I'm using asynchronous sockets for my server. Whenever a client
disconnects (either "legally" or suddenly), the server code
"explodes". I get the following error:

An unhandled exception of type 'System.NullReferenceException'
occurred in system.dll

Additional information: Object reference not set to an instance of an
object.


It then proceeds to tell me that there is no code to display, only
ASM. Which is entirely unhelpful. I'm completely out of ideas about
how to tackle this, let alone what is wrong. I can't seem to trap it.
I have exception handling around all the Asynch methods (BeginAccept,
BeginRecieve, BeginSend, and all their EndX counterparts). Nothing is
ever thrown. Checking for mSocket.Connected = False does nothing as
well. I am quite out of ideas. So, let me explain how I am currently
doing things:

For accepting, I have a While True loop which begins BeginAccept, and
a ManualResetEvent to control when the loop should continue
(controlled from the callback delegate of BeginAccept)

For Recieving, I do the following. I call the BeginRecieve method and
wait. When I get # of bytes recieved by calling EndRecieve on the
IAsynch Object passed in. I then proceed to parse and process data,
fill buffers, etc. At the end of the sub, I call the function that
calls BeginRecieve again, so I can get the rest of the data (or new
commands).

I also use BeginSend in similar fashion, but it is usually called from
the callback delegate of BeginRecieve (ie. it gets data, then proceeds
to send data back).

RE: Error when client disconnects (Asynch Sockets) Ted
10/26/2004 10:51:04 PM
Hi Zach,

You need to check in the callback of BeginReceive when you do the
"EndReceive" if you have received 0 bytes of data, this means that the client
has disconnected, when this happens it's also possible that you might get a
NullReference exception in the callback of the BeginSend when you do the
EndSend since the client is no longer connected, just catch it and ignore it,
but remember to check the EndReceive returning a datalength of 0 bytes, and
you should of course then not do any further BeginReceive as they also will
generate NullReference exceptions in the callback.

Cheers,
-Ted

Re: Error when client disconnects (Asynch Sockets) polyfractal NO[at]SPAM gmail.com
10/27/2004 3:34:21 AM
Hi, Thanks for the help. I went in and added handling to those areas
(so now every asynch area has handling), as well as checking for 0
length in the buffer. Still has not seemed to fix it. None of the
exceptions are ever called (tried both the generic Exception and also
the System.NullReferenceException). Here is my code, if you wish to
see it:

Main Form (calls BeginRecieve and such):
http://t0bban.mine.nu/Pastebin/Uploads/paste_18538129809.htm

Netclient class (handles all the socket work):
http://t0bban.mine.nu/Pastebin/Uploads/paste_02126134086.htm

Thanks, I appreciate the help. Also, sorry for the double post,
Re: Error when client disconnects (Asynch Sockets) Ted
10/27/2004 4:41:02 AM
Hi Zach,

I noted just some small things. You're doing the following in your
BeginRead callback:
numByteRecieved = mSocket.EndReceive(ar) - 1

why? remove the "-1", otherwise your check for 0 should actually be for -1.

Second, in your callbacks, catch the following exceptions
"ObjectDisposed","SocketException" also, and finally at the end of all
catches for the fun of it just catch Exception as it will catch any
exceptions not caught earlier.

But, again, remove the "-1", in your BeginReceive callback, I think that's
the bug, but remember to always add a catch for the general
"System.Exception" at the end of a catch sequence.

Try it out, or post back if you still have problems.

Cheers,
-Ted

[quoted text, click to view]
Re: Error when client disconnects (Asynch Sockets) polyfractal NO[at]SPAM gmail.com
10/27/2004 1:21:13 PM
Well, I found the problem, which was a combination of things (isn't it
always). First was the -1 issue on the socket. I remember adding
that code so I wouldn't have to mess with subtracting in loops (since
it's zero based). I didn't even think about the consequences of
checking for a 0 byte array. Doh. I was no longer getting the error
from "proper" shutdown on the client end.

Unfortunately, I was still getting the error when the client would
disconnect suddenly. After some digging around, I found this article:

http://groups.google.com/groups?hl=en&lr=lang_en&safe=off&selm=%23FjgHuY5DHA.1816%40TK2MSFTNGP12.phx.gbl

Which, although the error is different, was the answer. I have Nod32
installed as my AntiVirus, and apparently (I've heard this before too)
hooks itself into the low-level socket structure of windows. When
suddenly disconnecting, either Nod32 wasn't returning the error, or
Windows was not handling it correctly. Either way, I was getting the
sourceless, untrappable error.

With Nod32 removed, I now can trap the error as normal, and see which
object it is, etc. All is good :D

AddThis Social Bookmark Button