Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted (edited)

that was only an example of what im doing, here the actual thing:

 

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

Edited by divil
Posted

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

Posted

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

.nerd
Posted
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

.nerd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...