469,568 Members | 1,375 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

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?
Sep 20 '05 #1
4 6902
I have the exact same problem:

Friend Sub ReadCallback(ByVal ar As IAsyncResult)

Try

Debug.WriteLine("Reading network stream...", Now.ToString)

Debug.WriteLine("Asynch read callback wait handle:" +
ar.AsyncWaitHandle.Handle.ToString, Now.ToString)

If DirectCast(DirectCast(ar.AsyncState, System.Array).GetValue(0),
NetworkStream).DataAvailable Then

Debug.WriteLine("Network stream has data.", Now.ToString)

Else

Debug.WriteLine("Network stream does not have any data.", Now.ToString)

End If

Debug.WriteLine("Ending read...", Now.ToString)

Debug.WriteLine(DirectCast(DirectCast(ar.AsyncStat e,
System.Array).GetValue(0), NetworkStream).EndRead(ar).ToString + " bytes
read from network stream.", Now.ToString)

Debug.WriteLine("Read has ended.", Now.ToString)

Catch ex As Exception

Debug.WriteLine(ex.Message)

End Try

End Sub

Public Function SendRequest(ByVal requestPdu As RocProtocolDataUnit) As
RocProtocolDataUnit

Dim ResponsePDU As RocProtocolDataUnit

Dim RetryCount As Int32

Dim StartTime As DateTime

Dim ReadBytes() As Byte

Dim ReadBuffer(247) As Byte

Dim ReadLength As Int32

Dim ReadAsyncResult As IAsyncResult

Dim MyNetworkStream As NetworkStream = _Connection.GetRocTcpClient.GetStream

StartTime = Now

Send:

Call MyNetworkStream.Write(requestPdu(True), 0, requestPdu(True).Length)

Debug.WriteLine("Attempting asynch read operation...", Now.ToString)

Dim ObjectArray(1) As Object

ObjectArray(0) = MyNetworkStream

ObjectArray(1) = ReadLength

ReadAsyncResult = MyNetworkStream.BeginRead(ReadBuffer, 0, 247, New
AsyncCallback(AddressOf Connection.ReadCallback), ObjectArray)

Debug.WriteLine("Got an asynch read result handle: " +
ReadAsyncResult.AsyncWaitHandle.Handle.ToString, Now.ToString)

Debug.WriteLine("Waiting for asynchronous operation to complete (or time
out).", Now.ToString)

Call ReadAsyncResult.AsyncWaitHandle.WaitOne(10000, True)

Debug.WriteLine("Finished waiting", Now.ToString)

If ReadAsyncResult.IsCompleted Then

Debug.WriteLine("Read completed", Now.ToString)

Else

Debug.WriteLine("Read uncomplete", Now.ToString)

End If

Debug.WriteLine("Dumping read buffer:", Now.ToString)

Debug.WriteLine(ConvertToHexByteString(ReadBuffer) , Now.ToString)

Return ResponsePDU

End Function

Here is my debug output:

04/10/2005 11:58:43 PM: Attempting asynch read operation...

04/10/2005 11:58:43 PM: Got an asynch read result handle: 3604

04/10/2005 11:58:43 PM: Waiting for asynchronous operation to complete (or
time out).

04/10/2005 11:58:53 PM: Finished waiting

04/10/2005 11:58:53 PM: Read uncomplete

04/10/2005 11:58:53 PM: Dumping read buffer:

04/10/2005 11:58:53 PM:
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00

04/10/2005 11:59:10 PM: Attempting asynch read operation...

04/10/2005 11:59:10 PM: Got an asynch read result handle: 3644

04/10/2005 11:59:10 PM: Waiting for asynchronous operation to complete (or
time out).

04/10/2005 11:59:14 PM: Reading network stream...

04/10/2005 11:59:14 PM: Asynch read callback wait handle:3604

04/10/2005 11:59:14 PM: Network stream does not have any data.

04/10/2005 11:59:14 PM: Ending read...

04/10/2005 11:59:14 PM: 88 bytes read from network stream.

04/10/2005 11:59:14 PM: Read has ended.

04/10/2005 11:59:20 PM: Finished waiting

04/10/2005 11:59:20 PM: Read uncomplete

04/10/2005 11:59:20 PM: Dumping read buffer:

04/10/2005 11:59:20 PM:
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00

"0to60" <ho****************@yahoo.com> wrote in message
news:Ow**************@TK2MSFTNGP09.phx.gbl...
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?

Oct 5 '05 #2
Can you post a small client and server that demonstrates the issue?

--
William Stacey [MVP]

"0to60" <ho****************@yahoo.com> wrote in message
news:Ow**************@TK2MSFTNGP09.phx.gbl...
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?

Oct 5 '05 #3
I have created a client and server for you:

ftp://craighome.dyndns.org/NetworkSt...nReadIssue.zip

The lack of examples and documentation of the NetworkStream.BeginRead method
has left me with many questions:

1.) Why is my ReadCallBack method invoked only when data is available on the
NetworkStream, it should be called right after the NetworkStream.BeginRead
method is called, regardless of any conditions.
2.) With slower connections (ie. CDMA Modems, Satelite Earth Stations, etc),
when data is written to the stream and an asynch read is performed without
waiting for the NetworkStream.DataAvailable property to return True,
ReadCallBack method is never invoked. Why does this happen?
2.) How do I prevent the network stream from being read if the WaitOne
method times out and then some data arrives?
3.) Why is the stream being read and the WaitOne method returning before I
call the EndRead method?
4.) Why does the NetworkStream.DataAvailable property return false, and yet
after I call the NetworkStream.EndRead method and that returns the number of
bytes read from the NetworkStream into the buffer.

"William Stacey [MVP]" <st*****@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Can you post a small client and server that demonstrates the issue?

--
William Stacey [MVP]

"0to60" <ho****************@yahoo.com> wrote in message
news:Ow**************@TK2MSFTNGP09.phx.gbl...
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?


Oct 6 '05 #4


Jason Craig wrote:
1.) Why is my ReadCallBack method invoked only when data is available on the
NetworkStream, it should be called right after the NetworkStream.BeginRead
method is called, regardless of any conditions.
The callback is invoked after data is read from the stream and ready for
processing.
2.) With slower connections (ie. CDMA Modems, Satelite Earth Stations, etc),
when data is written to the stream and an asynch read is performed without
waiting for the NetworkStream.DataAvailable property to return True,
ReadCallBack method is never invoked. Why does this happen?
You shouldn't check DataAvailable when using AsyncIO, you should invoke
thestream.EndRead(asyncResult) to get the number of bytes actually read.
2.) How do I prevent the network stream from being read if the WaitOne
method times out and then some data arrives?
(you got 2.) two times :)
3.) Why is the stream being read and the WaitOne method returning before I
call the EndRead method?
2(b) and 3 are related to your specific code, I haven't read that so I
can't answer them.
4.) Why does the NetworkStream.DataAvailable property return false, and yet
after I call the NetworkStream.EndRead method and that returns the number of
bytes read from the NetworkStream into the buffer.


Because the DataAvailable is unrelated to the data you have read in your
async read.

example-code (not compiled):

public class ProcessStuff {
public readonly Stream Input;
public ProcessStuff(Stream s) { this.Input= s; }
public void readNext(int count) {
buffer = new byte[count];
Input.BeginRead(buffer, 0, buffer.Length,
new AsyncCallback(processInput), buffer);
}
protected void processInput(IAsyncResult r) {
int read;
try {
read = Input.EndRead(r);
} catch (Exception e) {
// notify someone of exception
// notify someone that you will stop reading
return;
}
if ( read == 0 ) { // Closed
// notify someone that you will stop reading
return;
}
byte[] mybuf = (byte[])r.AsyncState;

#if METHOD1
// Here i start read immideatly, this means that the order
// in which data arrive from the stream may not be maintained in
// the order of invocations of f.
// but there was hopefully a reason for using Aysync IO instead
// of sync.
readNext(somecount); // Keep reading data
f(mybuf, 0, read); // Do your stuff with the read data
#endif

#if METHOD2
// You may wish to
// 1) start f async (BeginInvoke)
// 2) wait for f to signal this thread that it has started and
// performed any nessesary synchronisation
// 3) start read-next after
// so that you can maintain the order of stream input as
// the order of f invocations, while still restarting reads
// as soon as possible
f.BeginInvoke(mybuf, 0 read);
somethingSignalledByf.WaitOne();
readnext(somecount);
#endif
}
}

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Oct 6 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Manuel Costa | last post: by
6 posts views Thread by Stephen Brooker | last post: by
1 post views Thread by Nicholas Holder | last post: by
3 posts views Thread by Terry Olsen | last post: by
4 posts views Thread by 0to60 | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.