473,394 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 7160
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: DC | last post by:
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...
1
by: Manuel Costa | last post by:
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...
2
by: psg | last post by:
Hello! I am developing Windows application which after sending a request to a server will listen to response, a response will be infinite in length, but will not be continuous in time. What I...
6
by: Stephen Brooker | last post by:
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...
1
by: Nicholas Holder | last post by:
A client creates a connection to the server using the TCPListener/Client classes and transfers data via a NetworkStream. When the client connects, the server creates a process and redirects its...
3
by: Terry Olsen | last post by:
Dim clt as TcpClient Dim stm as NetworkStream clt = New TcpClient() clt.Connect("192.168.0.200", 9999) Do Until clt.Connected Loop stm = clt.GetStream ' --The following line throws "Object...
4
by: 0to60 | last post by:
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...
2
by: TDC | last post by:
I'm working on tying in a library that uses a TcpClient/NetworkStream/BeginRead. When that lib object is started, it connects and immediately does a BeginRead that stays pending until the socket...
7
by: littleIO | last post by:
Hi, I'm stuck on a very simple problem and just cant seem to get around it, little help would be much appreciated. I have a server which listens, receives calls, processes them and sends back the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.