Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Asynchronous TCP/IP Network Stream size

Posted on 2004-08-13
8
Medium Priority
?
279 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

609 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