.Net Framework >> NetworkStream BeginRead & callback invocation

by UGF0cmljayBHaWxs » Fri, 16 Apr 2004 00:21:08 GMT

BeginRead/EndRead doc implies the callback will execute and block on EndRead until data is available

"Your callback method should implement the EndRead method. When your application calls BeginRead, the system will use a separate thread to execute the specified callback method, and will block on EndRead until the provided NetworkStream reads data or throws an exception.

Testing this shows the callback is executed only when data is available; ie: if no data is received on the socket, the callback is not called. When EndRead is called within the callback, it returns immediately with the number of bytes read. The implication is also that EndRead will never block if the callback is only executed when data is available..

Same behavior was observed with .NET Compact Framework and regular .NET Framework (desktop)

Is this a known discrepancy in the doc and the real behavior is what we observed in our test

-
Thank
Patrick Gil
Motorola

.Net Framework >> RE: NetworkStream BeginRead & callback invocation

by timhuang » Fri, 14 May 2004 19:30:27 GMT


Hi,

Thanks for your post. I am checking this issue and will update you with my
information.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

.Net Framework >> RE: NetworkStream BeginRead & callback invocation

by yhhuang » Thu, 27 May 2004 17:14:44 GMT

Hello Pat,

Based on my understanding, the main concern here is: The doc says "When your application calls BeginRead, the system will use a
separate thread to execute the specified callback method, and will block on EndRead...". On your test, you noticed that the callback is not
called immediately. It seems that when there is data in the stream first, then the callback function is called. Right?

My question here is how long have you waited for the arrival of network stream data. It is quite normal that the application spawns a new
thread. However, the thread is not scheduled to run immediately. When the thread is run in the first time, the network stream data has arrived
already. I think you can observe how many threads are in the process by looking into Debug->Window->Threads when debugging
application.

If you have any more concerns, please feel free to post here. Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

.Net Framework >> RE: NetworkStream BeginRead & callback invocation

by UGF0cmljayBHaWxs » Mon, 31 May 2004 20:06:03 GMT

Hi Yanhong

To answer your questions;
- If there is sufficient data in the stream, the callback function is called
- If there is insufficient data in the stream (either 0 bytes or n bytes where n is smaller than the size requested in BeginRead and no more data is added to the stream), the callback is never called... waiting over 1 minute
- I do understand a new thread is spawned for the callback and it wont run until it is scheduled to but when I test what the Microsoft doc says, no thread is spawned, hence the callback is never called

So the issue from my original post entirely remains

Let me know if something is not clear about the issue I raised

Thank
Patrick Gill

.Net Framework >> RE: NetworkStream BeginRead & callback invocation

by yhhuang » Tue, 01 Jun 2004 10:49:08 GMT

Hi Patrick,

I have received your email. Do you have any repro sample? If having, could you please email me one copy of that? I will look into it on my side.
Please tell me detailed repro steps.

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

.Net Framework >> RE: NetworkStream BeginRead & callback invocation

by yhhuang » Tue, 08 Jun 2004 15:09:19 GMT

Hi Patrick,

Currently one developer has accepted it and will help look into the reason of this behavior. I will reply here with more information soon. Thanks
very much for your patience.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

.Net Framework >> RE: NetworkStream BeginRead & callback invocation

by yhhuang » Wed, 09 Jun 2004 10:13:18 GMT

Hello Patrick,

After discussing with product group, the correct behavior of a stream is that the callback gets called with a thread from the threadpool which is
scheduled when data is received. Until that time, the callback is not called. When called, the socket will either have data or it will have an
exception that is thrown when calling EndRead. That is totally the same as what we said in the testing application.

It seems that MSDN document doesn't mention it clearly. I will speak to MSDN content team on it.

Thanks very much for your feedback. If you have any more concerns on it, please feel free to post here.

Have a good day.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

.Net Framework >> RE: NetworkStream BeginRead & callback invocation

by yhhuang » Wed, 09 Jun 2004 13:57:06 GMT

Hi Patrick,

Another good news. The dev team will file a doc bug into our database and we will change the document in the future to make it clearer.

Thanks very much for your feedback. We appreciate it very much. :)

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

.Net Framework >> RE: NetworkStream BeginRead & callback invocation

by UGF0cmljayBHaWxs » Thu, 10 Jun 2004 02:41:03 GMT

Thanks Yanhong for your support on confirming the issue resides in the BeginRead & EndRead documentation

I am looking forward to seing the updated BeginRead & EndRead documentation

Regard
Patrick

.Net Framework >> RE: NetworkStream BeginRead & callback invocation

by yhhuang » Thu, 10 Jun 2004 10:32:15 GMT

Hi Patrick,

I am glad to be of assistance. :) I have filed the doc bug and assigned it to a developer for follow up.

Thanks again for participating the community.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

.Net Framework >> RE: NetworkStream BeginRead & callback invocation

by Q2hyaXMgVGFuZ2Vy » Thu, 01 Jul 2004 04:36:01 GMT


This is excellent news! In my opinion this allows me to provide "on network data arrived" callback functionality without tying up and wasting ThreadPool threads. I give a thumbs up to the development team! I have a server which could have potentially thousands of socket connections and the ThreadPool's max size for IOCompletionPort threads is 1000 which could be a major problem because 1 thread would be needed for each blocking EndRead request if no data was available on the socket.

--
- Chris Tanger

.Net Framework >> RE: NetworkStream BeginRead & callback invocation

by yhhuang » Thu, 01 Jul 2004 09:25:51 GMT

I am so glad to hear that. :)

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Similar Threads

1. networkstream.beginread, socket error, no callback

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


2. NetworkStream.BeginRead Blocking problem

3. 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?


4. Looking for good c# sample NetworkStream BeginRead/EndRead... - Microsoft .NET Framework

5. NetworkStream.BeginRead and Forms threading

Hi all,

I've got a basic TCP app that is giving me trouble. I have a separate 
class that takes care of the TCP connection, and uses the NetworkStreams 
BeginRead and EndRead with a callback function to deal with the server 
response. All works well and the data is received OK. Once all the data 
is received, the connection is closed and I fire an event indicating all 
is finished. The event is handled in the main form of the application 
and I can work with the returned data OK.

The problem arises when I try to add some of the data to a TreeView. It 
complains that only the creating thread can modify the TreeViews data 
and I should use BeginInvoke etc (all this so far I understand). So I 
assume control is still with the thread created by BeginRead? Am I 
correct? If so, is there any way of forcing control to return to the 
main forms threads once the event is fired for the data being received? 
I've been able to set values on several other controls, the TreeView is 
the only one giving me problems (why is it so picky when none of the 
other controls are).

TIA

Steve.

6. NetworkStream / BeginRead / EndRead / Dataavailable: can't understand MSDN code sample - CSharp/C#

7. NetworkStreams impractical?, Buffering NetworkStreams, how?

For practical purposes, the CF network stream is really not a stream at all.
You can't peek, you can't push things back on the stream, etc.

We are sending packets on the stream with packet seperation marks,

The problem is that one can easily do a read that gets half of two packets,
or other malignant cases. We would like to read just as much to get the tail
of the first packet (and match it with what we read) and leave the second
packet on the stream till the rest arrives.

Logically, this is buffered stream.

The problem is that BufferedStreams are not supported in CF, nor is peek, or
seek, which would allow us to set the stream pointer to the actual logical
packet break.

What are people doing to get around this limitation?



-- 
==================================
Yechezkal Gutfreund
Chief Scientist
Kesser Technical Group, Inc.
==================================


8. networkStream.Write waits until networkstream.close - Microsoft .NET Framework