Click here to Skip to main content
15,886,199 members

Async Sockets

Lee Humphries asked:

Open original thread
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 />      /* Retrieve the client socket from the asynchronous state object.*/<br />      StateObject state = (StateObject)ar.AsyncState;<br /><br />      sErr = new SocketError();<br /><br />      /* Read data from the remote device.*/<br />      /* however this line frequently throws an exception*/<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.

Tags: .NET (.NET CF)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900