NetworkStream.Read doesn't get all the data.

SDittmar

Newcomer
Joined
Feb 9, 2003
Messages
11
This is what im basically doing

-
Dim tcp as New TcpClient
tcp.Connect("host",80)

Dim ns as NetworkStream = tcp.GetStream
dim size as integer

While ns.DataAvailable
size = ns.read(buffer,0,buffer.length)
End While
-

How do i wait for all the data to be received? Because i end up getting only some of the data then the loop ends.

thanx for any replies
 
You're overwriting the buffer you've created everytime the contents of the While/End While block execute. Try appending the contents of the newly filled buffer to a variable that isn't going to be overwritten.
 
that was only an example of what im doing, here the actual thing:

Visual Basic:
Dim size As Integer
        Dim all() As Byte = New Byte() {}
        Dim current() As Byte = New Byte(8191) {}
        Dim timeout As Single
        Dim res As New NNTPResponse()
        Dim x As Integer
        Dim stream As NetworkStream = _tcp.GetStream

        While Not stream.DataAvailable Or CInt(timeout) = _timeout
            Sleep(500)
            timeout += 0.5
        End While

        If timeout = _timeout Then
            _timedout = True
            Exit Function
        End If


        While stream.DataAvailable
            size = stream.Read(current, 0, current.Length)
            Debug.Write(size & vbCrLf)
            ReDim Preserve current(size - 1)
            ReDim Preserve all(all.Length + size - 1)
            Array.Copy(current, 0, all, all.Length - size, current.Length)
            ReDim current(8191)
        End While
 
Last edited by a moderator:
It seems to work fine in debug mode (obviously because it has more time to receive data) but not normally. What property can i use to wait for the typical 8192 bytes?

thanx
 
Your timing scheme is flawed. Instead of telling the current thread to sleep for 500 milliseconds the code should keep polling the DataAvailable property until data is either present or the procedure times out.
 
Excuse me for interfering,
but to my understanding the code does exactly what Derek proposed: Polling the Dataavailable property until either data is present or the procedure times out.

There's just a short delay between the polls, and rightly so, I believe, in order not to grab all cpu power for this busy waiting.

my 2c
 
Visual Basic:
Dim size As Integer
Dim all() As Byte = New Byte() {}
Dim current() As Byte = New Byte(8191) {}
Dim timeout As Single
Dim res As New NNTPResponse()
Dim x As Integer
Dim stream As NetworkStream = _tcp.GetStream

' BEGIN OUTER LOOP

While Not stream.DataAvailable Or CInt(timeout) = _timeout
Sleep(500)
timeout += 0.5
End While

If timeout = _timeout Then
_timedout = True
Exit Function
End If

While stream.DataAvailable
size = stream.Read(current, 0, current.Length)
Debug.Write(size & vbCrLf)
ReDim Preserve current(size - 1)
ReDim Preserve all(all.Length + size - 1)
Array.Copy(current, 0, all, all.Length - size, current.Length)
ReDim current(8191)
End While

' Reset Timeout
' END OUTER LOOP
 
Back
Top