Link to home
Start Free TrialLog in
Avatar of srcalc
srcalc

asked on

Asynchronous TCP/IP Network Stream size

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.
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

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)
Avatar of srcalc
srcalc

ASKER

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...
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srcalc

ASKER

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?
why not just always send the first few bytes as an integer representing the number of bytes for the client to expect ?
Avatar of srcalc

ASKER

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.
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.
Avatar of srcalc

ASKER

OK thanks for the advice!