CSharp/C# >> networkstream.beginread, socket error, no callback

by Manuel Costa » Wed, 28 Apr 2004 19:06:47 GMT

Hi,

i was testing a network application that i've been working on which use .net
socket components. To read from a socket i use the networkstream beginread.
If the other side dies, beginread calls the callback delegate. Today i was
testing this in my old laptop (celeron 553MHz) and verify an interesting
thing. Callback is not called and this exception is captured by visual
studio.

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

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


Unhandled Exception: System.NullReferenceException: Object reference not set
to an instance of an object.
at System.Net.OSSOCK.WSAGetOverlappedResult(IntPtr socketHandle, IntPtr
overlapped, UInt32& bytesTransferred, Boolean wait, IntPtr ignored)
at System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback(UInt32
errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)

Therefore, i assume there is a bug in beginread, usually hidden by the cpu
speed or so.

Who knows the why and suggest me the how?

Manuel



Similar Threads

1. NetworkStream BeginRead & callback invocation - .Net Framework

2. Looking for good c# sample NetworkStream BeginRead/EndRead...

Need the correct usage pattern for something like a chat program.

Don't want any threading issues or stack problems.

Kyle!


3. NetworkStream.BeginRead and Forms threading - CSharp/C#

4. NetworkStream / BeginRead / EndRead / Dataavailable: can't understand MSDN code sample

Hi,

I need to asynchronously read from a network (TCP) stream, and I am
having trouble with retrieving whole blocks; I get a break in the data
block every 1460 bytes which relates to network packet sizes I guess.

To get this fixed, I am trying to follow an example in the MSDN, which
pretty much resembles what I want to do. Since the code (I can't do
this in a static class for example) is not 100% what I need, I
certainly would like to understand what I am doing, but I guess I am
lacking some fundamentals of asynchronous programming since I simply
do not understand the code snippet. Here it is:


public static void myReadCallBack(IAsyncResult ar ){

    NetworkStream myNetworkStream = (NetworkStream)ar.AsyncState;
    byte[] myReadBuffer = new byte[1024];
    String myCompleteMessage = "";
    int numberOfBytesRead;

    numberOfBytesRead = myNetworkStream.EndRead(ar);
    myCompleteMessage = 
        String.Concat(myCompleteMessage,
Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
    
    // message received may be larger than buffer size so loop through
until you have it all.
    while(myNetworkStream.DataAvailable){
        
        myNetworkStream.BeginRead(myReadBuffer, 0,
myReadBuffer.Length,
                                                   new
AsyncCallback(NetworkStream_ASync_Send_Receive.myReadCallBack),
                                                   myNetworkStream);  

    }

    // Print out the received message to the console.
    Console.WriteLine("You received the following message : " +
                                myCompleteMessage);
}


I wonder, how myCompleteMessage can ever add up to the complete
message, since it will be reinitialized at every callback, won't it?

If somebody can help me to understand this (what happens
"while(myNetworkStream.DataAvailable){...}") , I would be most
grateful.

Best regards

DC

5. NetworkStream.BeginRead Blocking problem

6. NetworkStream.BeginRead() wierdness

I have a class that wraps a TcpClient object and manages all the async
reading of the socket.  It works really nice, and I use it all over the
place.  But there's this ONE INSTANCE where I create one of these things and
it WON'T read data.  If I set a breakpoint in my EndRead callback, it never
goes off.  NOTHING is different from anywhere else I use this class, its
just this one place.

Now, if I create a second constructor for my class that DOESN'T start off
the async reading process (basically, never calls BeginRead()), subsequent
Read()s will receive data.  Additionally, if I do a loop like this:

while(!networkStream.DataAvailable)
   Sleep(250);

networkStream.BeginRead(...)

Then the class works like it does everywhere else.  Its like, in this one
instance (and this instance isn't any different than anywhere else I use
this class), I can't just do a BeginRead() and wait for data to come in.  I
have to loop and check DataAvailable before calling BeginRead().

Any suggestions?


7. Will Socket.BeginRead IAsyncResult.CompletedSynchronously alwa - .Net Framework

8. Will Socket.BeginRead IAsyncResult.CompletedSynchronously always b

"Chris Tanger" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
>
> Will the IAsyncResult.CompletedSynchronously for Socket.BeginRead always
be false?  I need to know if this is true so I can limit the amount of time
that I use the callback thread for processing.  I have multiple threads that
need to read from the same socket asynchronously, when one thread has
completed a read operation from the socket it will initiate the next read
operation for another caller.  If BeginRead ever completes synchronously
then the processing done by the callback thread could cause the calling
thread to be tied up for too long (anything over 15ms) because the calling
thread would be the callback thread.
>
> - Chris Tanger
>

Ok,  Here's what I think you mean.  You have Thread1 calling BeginRecieve
and passing an async callback method which is too lengthy to be executed by
Thread1.  But many of the async methods will invoke the callback on the
calling thread if, by the end of the BeginXXX call the operation has, in
fact, succeeded.  So you want to know if this is the case with
Socket.BeginRecieve, or if the async callback method is guaranteed to be
executed by a background thread, right?

If so, I believe the answer is that the callback is guaranteed to be
executed by a background thread.


From the Socket.BeginRecieve documentation

"When your application calls BeginReceive, the system will use a separate
thread to execute the specified callback method,"

So you the your AsyncCallback is guaranteed to be defered to a background
thread, and IAsyncResult,CompletedSyncronously will always be false.

A warning, though, if Thread1 cannot tolerate a 15ms absence from its normal
duties, then you may not be able to write this application in .NET.  The
Garbage Collector will suspend your thread while collection runs, and a
collection of Gen2 may easilly take 100ms or more.

David