473,499 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

networkStream.Read gets incomplete data

!NoItAll
297 Contributor
So I've been wrapped around the axle on this problem using the tcpClient and networkStream.
My code first sends a byte count (8 bytes) so the receiver knows exactly how much to expect. This works flawlessly. Subsequent transmissions would sometimes get everything, but with large records (I'm sending object(s) serialized as XML) the data would often get truncated.
I tried:

Expand|Select|Wrap|Line Numbers
  1. Dim RxSize as Integer = receiveBufferLength(networkStream)
  2. Dim RxBytes(RxSize) as Byte
  3. Do While tcpClient.Available < RxSize
  4.     Threading.Thread.Sleep(100)
  5. End While
  6.  
  7. RxBytes = networkStream.Read(RxBytes, 0, RxSize)
  8.  
But often the tcpClient.Available bytes was less than the RxSize and would NOT increment to the expected RxSize.

What I found is that once the tcpClient.Available bytes stopped incrementing I had go get them and then the underlying socket would begin adding new bytes to the buffer. A secondary read would get the rest.
I tried setting the tcpClient.ReceiveBufferSize to RxSize - that did not help. I also set tcpClient.NoDelay=True - again no help...
This seemed confusing but here is the code I wound up working out...

Expand|Select|Wrap|Line Numbers
  1. ' Get the stream
  2. Dim networkStream As NetworkStream = tcpClient.GetStream()
  3.  
  4. 'get the buffersize and set it from the data in the first transmission
  5. Dim RxSize As Integer = receiveBufferLength(networkStream)
  6. tcpClient.ReceiveBufferSize = RxSize
  7.  
  8. ' Read the stream into a byte array
  9. Dim rxBytes(rxSize) As Byte
  10.  
  11. Dim rxBits(0) As Byte
  12. Dim iBits As Integer = 0
  13. Dim iPos As Integer = 0
  14.  
  15. Dim Timeout As Integer = 0
  16.  
  17.  While iPos < rxSize
  18.     iBits = tcpClient.Available
  19.     ReDim rxBits(iBits)
  20.     networkStream.Read(rxBits, 0, iBits)
  21.     rxBits.CopyTo(rxBytes, iPos)
  22.     iPos += (rxBits.Length - 1)
  23.     Threading.Thread.Sleep(100)
  24.     Timeout += 1
  25.     If Timeout > 20 Then Exit While
  26.  End While
  27.  
  28.  
This does as many reads as necessary to get all of the bytes from the networkStream and concatenates it to my receive buffer (rxBytes). It allows for up to 21 times through the loop before giving up (I don't know if that's enough - but so far its never gone through more than twice)

So my question is:
what do people on this forum think of this approach? Does anyone have an idea for more efficient code?
Oct 13 '11 #1
0 1524

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2387
by: Hai Ly Hoang \(MT00KSTN\) | last post by:
Hi, FileStream.Read(buf, offset, siz) will return when reaching the end of file or a block (size siz) can be read from the file. In the case of NetworkStream, it's more confusing. Now, consider...
1
1795
by: Napo | last post by:
Hi I use a c# method to open an excel file and to read it's data.But i find a problem that if i changed the excel file data manually by using excelapplication,the readed data by c# method isn't...
2
5194
by: Brent Burkart | last post by:
Below is the error I am receiving. I have checked SQL Profiler and it is receiving the correct query which runs fine in Query Analyzer. Any ideas? Server Error in '/lockinsheet' Application....
1
3500
by: Steve | last post by:
I'm working on a program that talks to the Yahoo chat servers. But I have run into a small problem and I'm not sure what to do next. After I successfully log into yahoo and receive my cookies I...
3
4677
by: Vijay | last post by:
Hi Folks, I having one issue, in my application I am going to use the server push for streaming the data by keeping the connection open. At client side, i am having the XMhttprequest object (i.e...
4
7523
by: DaveR | last post by:
I have a background thread to accept and process TCP/IP connections. When the application shuts down I want to gracefully terminate it. The thread will block in NetworkStream.Read() waiting for...
4
12761
by: Cyron | last post by:
Hello Friends, When I make a call to NetworkStream.Read(byte buffer, int offset, int size) I have found that this method will block until size bytes have been read or the connection is closed. ...
1
2384
by: Charles | last post by:
Hi, Is there a way to find out how many bytes NetworkStream Read/Write actually read/wrote when there is an exception (i.e. socket read/write timeout). Or, can I assume that if there is an...
10
9097
by: ohmmega | last post by:
hello out there, I use a thread data from a linuxserver. i ask for data, i read the data (fix in size) - so far so good. this works great for the first 61 datapackages, then my thread abruptly...
1
1876
Oodles Of Noodles
by: Oodles Of Noodles | last post by:
Hello fellow geeks I have a problem in my database iVB .Net program that is generating 'Error:Invalid attempt to read when no data is present.' The weird part is that when you call the page from...
0
7178
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
7223
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...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.