|
-
Mar 19th, 2009, 04:56 PM
#1
Thread Starter
New Member
[RESOLVED] TcpClient - Messages bigger than buffer size problem
Hi everyone,
I have an application and a server. The application has a method which sends a request to the server, and then waits for a reply using client.getstream.read().
After the server has recvd the request, it does some processing and sends a reply using client.getstream.write()
The problem occurs when the servers reply is larger than 8192(default buff size).
Server sending code:
Code:
Dim ns As System.Net.Sockets.NetworkStream
SyncLock _client.GetStream
ns = _client.GetStream
End SyncLock
Dim myWriteBuffer As Byte() = Encoding.ASCII.GetBytes(message)
ns.Write(myWriteBuffer, 0, myWriteBuffer.Length)
Client receiving code:
Code:
Dim ourStream As NetworkStream = fromClient.GetStream
Dim ourData(fromClient.ReceiveBufferSize) As Byte
Dim myReadBuffer(1024) As Byte
Dim myCompleteMessage As [String] = ""
Dim numberOfBytesRead As Integer = 0
' Incoming message may be larger than the buffer size.
Do
numberOfBytesRead = ourStream.Read(myReadBuffer, 0, myReadBuffer.Length)
myCompleteMessage = [String].Concat(myCompleteMessage, System.Text.Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
Loop While ourStream.DataAvailable
As you can see from the code, I loop if there is still data availble to be read incase the message is larger than the buffer size. In my example, the reply is supposed to be 12000 bytes.
When the server is on my local machine, I don't get any problems and recv 8192 bytes first, then the remaining bytes which are concated to the string.
However, when I test it through the internet, I get the first 8192 bytes, but only 600 bytes on the second time thru the loop. Following this, datavailable is false.
I managed to fix this error by adding this code to the client before the end of the loop:
Code:
Threading.Thread.Sleep(100)
After this, I receive all the required bytes. It seems like latency is an issue here. Does anyone know what the proper and best way to read messages bigger than the buffer size is?? My code is basically taken from MSDNs doco for each method, so I don't know why its failing.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|