SDittmar Posted February 24, 2003 Posted February 24, 2003 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 Quote
*Gurus* Derek Stone Posted February 24, 2003 *Gurus* Posted February 24, 2003 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. Quote Posting Guidelines
SDittmar Posted February 24, 2003 Author Posted February 24, 2003 (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 February 24, 2003 by divil Quote
SDittmar Posted February 24, 2003 Author Posted February 24, 2003 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 Quote
a_jam_sandwich Posted February 24, 2003 Posted February 24, 2003 if I was you i'd have a time out of perhaps 30secs Andy Quote Code today gone tomorrow!
*Gurus* Derek Stone Posted February 24, 2003 *Gurus* Posted February 24, 2003 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. Quote Posting Guidelines
a_jam_sandwich Posted February 24, 2003 Posted February 24, 2003 quite true use the timer control so on each tick it checks Andy Quote Code today gone tomorrow!
*Gurus* Derek Stone Posted February 24, 2003 *Gurus* Posted February 24, 2003 A timer control is largely unnecessary. Use the Environment.TickCount property instead. Quote Posting Guidelines
Heiko Posted February 24, 2003 Posted February 24, 2003 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 Quote .nerd
Heiko Posted February 24, 2003 Posted February 24, 2003 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 Quote .nerd
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.