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?