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.Get Stream)
writer.Write(Data & Chr(13) & Chr(10))
writer.Flush()
End SyncLock
End Sub
'Listening for information (client side)
client.GetStream.BeginRead (readBuffe r, 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.
'Writing the data (server side)
Public Sub SendData(ByVal Data As String)
SyncLock client.GetStream
Dim writer As New IO.StreamWriter(client.Get
writer.Write(Data & Chr(13) & Chr(10))
writer.Flush()
End SyncLock
End Sub
'Listening for information (client side)
client.GetStream.BeginRead
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.
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)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK I have code for compiling together multiple packets if the data is split:
strMessage = System.Text.Encoding.ASCII .GetString (readBuffe r, 0, BytesRead - 2)
If strMessage = "Course" Then
Do Until client.GetStream.DataAvail able = True
Loop
End If
Do Until client.GetStream.DataAvail able = False
BytesRead = client.GetStream.Read(read Buffer, 0, 10000)
strMessage += System.Text.Encoding.ASCII .GetString (readBuffe r, 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?
strMessage = System.Text.Encoding.ASCII
If strMessage = "Course" Then
Do Until client.GetStream.DataAvail
Loop
End If
Do Until client.GetStream.DataAvail
BytesRead = client.GetStream.Read(read
strMessage += System.Text.Encoding.ASCII
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 ?
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.
ASKER
OK thanks for the advice!