Solved

Asynchronous TCP/IP Network Stream size

Posted on 2004-08-13
8
264 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now