I've just recently ported a sockets client from .Net 2.0 to .Net 3.5 ...
and now it runs into reoccurring problems, of which I'm a little suspicious may be related to subtle differences between the frameworks that I'm not allowing for.
*Update* I've made significant changes following Suggestion 1 - which did improve things (in other areas), and along the way corrected the problem mentioned by Suggestion 2 - but still the problem persists. To keep things working I've actually altered the overall error handling, but thanks to my diagnostics I know that the underlying problem still persists - I'm only working around it.
Specifically the Receive Callback function is being invoked, but the socket itself is no longer connected (and probably disposed). This is in spite of the fact that data is expected to be received.
private void ReceiveCallback(IAsyncResult ar)<br />{<br /> SocketError sErr = SocketError.Success;<br /> int bytesRead = 0;<br /> lock (thisLock)<br /> {<br /> try<br /> {<br /> <br /> StateObject state = (StateObject)ar.AsyncState;<br /><br /> sErr = new SocketError();<br /><br /> <br /> <br /> bytesRead = socket.EndReceive(ar, out sErr);<br /> ...<br />
And the essentials of the exception are:
ReceiveCallback threw an error. System.ObjectDisposedException: Cannot access a disposed object.<br />Object name: 'System.Net.Sockets.Socket'.<br /> at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult, SocketError& errorCode)<br />
The associated socket error code is 10038 - which when you read its description isn't very helpful either.
Just to add to this the previous version of this client is still running and still connecting to the same server without issue.
My request is if anyone can point me in the right direction as to what I'm missing or doing wrong. You can reasonably assume that I've googled every permutation of the above in my quest.
Here's some possibilities:
* There's some subtle distinction in how v2.0 and v3.5 sockets connect (e.g. defaults for various socket options) that the server I'm connecting doesn't like / understand.
* It's a speed issue. The v2.0 code has got a whole heap of throttling tricks, that the v3.5 based version does not - I don't think this one's likely (already been playing around in this area).
* It's a threading and/or locks problem on the socket itself.
* I'm missing something important in using sockets - not likely, but this excuse is here for completeness, just in case you have some "traps for young players" type of insight.
* Or something else.