?
Solved

Asynchronous TCP/IP Network Stream size

Posted on 2004-08-13
8
Medium Priority
?
278 Views
Last Modified: 2012-08-14
I am writing an application that uses ansynchronous TCP/IP communication with the TCPClient and TCPListener objects. For some reason, if I send a string longer than 1024 bytes, it is split up into pieces. These are the important pieces of code:

'Writing the data (server side)
    Public Sub SendData(ByVal Data As String)
        SyncLock client.GetStream
            Dim writer As New IO.StreamWriter(client.GetStream)
            writer.Write(Data & Chr(13) & Chr(10))
            writer.Flush()
        End SyncLock
    End Sub

'Listening for information (client side)
client.GetStream.BeginRead(readBuffer, 0, 10000, AddressOf DoRead, Nothing)

How can I make is so the information doesn't split? If you need to see more code, I'll be happy to post it. Thanks.
0
Comment
Question by:srcalc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11800985
takea look at the nodelay property ... also have you set send/receive buffer sizes ? you should also be bufferring your data in the case that it is split between multiple calls, this could be being caused by a packet arriving (thus available) but the next packet has not arrived (will be read on the next read)
0
 
LVL 4

Author Comment

by:srcalc
ID: 11802233
Should nodelay be true of false? I checked it and it is false. The buffer sizes are both 10000. I don't totaly understand the last thing that you said...
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 11804664
The last part is where I think your problem lies ...

reading from a TCP socket is like reading from any other file ...

A sends 5k of data.

The OS breaks this up into multiple packets in order to send.

B at the OS level receives the first packet and reassembles it (luckily its the first packet it is now available for client B to read

Client B's read operation reads all of the data that was contained in the packet, unless another packet has been reassembled, client b hits the end of the stream and returns as if the data had been read.

The thing is that client B may require multiple read calls in order to get all of the data that it requires therefor you need to be able to handle the case that a read operation return prematurely (it just means you are waiting on more data to continue reading)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:srcalc
ID: 11805177
OK I have code for compiling together multiple packets if the data is split:

            strMessage = System.Text.Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2)
            If strMessage = "Course" Then
                Do Until client.GetStream.DataAvailable = True
                Loop
            End If
            Do Until client.GetStream.DataAvailable = False
                BytesRead = client.GetStream.Read(readBuffer, 0, 10000)
                strMessage += System.Text.Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2)
            Loop

What I do here is, before sending the large amont of data, have Client A send a small string = "Course" that tells Client B that multiple packets are incoming, then Client B waits for all the packets before moving on. So there isn't a way to stop the breakup of the data, I just need to wait for them all like I do here?
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11805226
why not just always send the first few bytes as an integer representing the number of bytes for the client to expect ?
0
 
LVL 4

Author Comment

by:srcalc
ID: 11805925
hmm, good idea. One last question before I let you go... How do I define how big the data chunks are? Right now they are 1024, but I can't fund the setting to change that.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11805928
not sure but I believe it is not readily in your control (and shouldnt be) the data is being reordered for you ... you should let the OS deal with things such as TCP packet sizes.
0
 
LVL 4

Author Comment

by:srcalc
ID: 11805935
OK thanks for the advice!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question