Untrappable error, Asynchronous sockets

excaliber

Member
Joined
Dec 7, 2003
Messages
9
Hi,

Im 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. Im completely out of ideas about how to tackle this, let alone what is wrong. I cant 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).

Any help would be great. Thanks
 
Well, I found the problem, which was a combination of things (isnt it always). First was the of not catching a 0 length return array for the buffer (which means the client is shutting down). 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=#FjgHuY5DHA.1816@TK2MSFTNGP12.phx.gbl

Which, although the error is different, was the answer. I have Nod32 installed as my AntiVirus, and apparently (Ive heard this before too) hooks itself into the low-level socket structure of windows. When suddenly disconnecting, either Nod32 wasnt 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

Thanks again for your help, I really appreciate it!
 
Back
Top