Okay, this is a weird.
I have a simple server/client application going using TCPListener and TCPClient, written in VB.NET 2005. Like any server/client TCP program, the server starts listening, then the client connects, and then I define NetworkStream streams for both. The client and server are their own separate projects (though both projects are in the same solution).
In my case I'm running both on the same machine. The client connets to 127.0.0.1.
The client calls stream.beginwrite() to asynchronously write a large string of data (e.g. 200kb) out the networkstream to the server. Here's the call:
' "chunk" is a byte array that is +200kb long
stream.BeginWrite(chunk, 0, chunk.Length, AddressOf Callback_WriteComplete, 0)
The server should see this on it's end, but the tcplistener object's "Available" property never goes higher than 8192 bytes. I figure that's as much as it will buffer in one go maybe?
But in any case, that's alright. I use stream.Read() to read in a small 16 byte "header" that I defined when I send the data from the client, part of the header describes how big the rest of the chunk's data is, so I know how much to read, and when to stop.
I already parsed the header and filled the "readSize" integer variable in with that information. Then I have the server call stream.BeginRead() to asynchronously read all the data in. Here's my beginread() call:
' "allDone" is an IAsyncResult object
' "readSize" is the already known size of the data I want to read, in the 200kb range
' "bytesRead" is an integer that gets filled with the amount of data that got read by the async read call.
allDone = stream.BeginRead(data, 0, readSize, AddressOf Callback_ReadComplete, 0)
bytesRead = stream.EndRead(allDone)
The thing is, the data() byte array only gets filled with ~15-60k of data. The exact amount varies for each call. This is reflected in the bytesRead return value. Even though I requested that it read +200kb of data, it only reads in a few kb. I have to call beginread over and over again to get everything.
Now before we blame the asynchronous reading on this behavior, I DID try it with a normal synchronous read() call. Same problem!
Seems to me like reading the data in chunk by chunk should be something that the TCPListener/Client have to worry about, not me. If I ask for 200kb of data I'd expect to receive that, especially in the synchronous read() call which sits and waits for that amount of data to become available before returning.
Is this an expected behavior? Or a bug of some kind? Or am I just doing it wrong?