Solved

Asynchronous TCP/IP Network Stream size

Posted on 2004-08-13
8
270 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
  • 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 500 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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