I created a NetworkStream object and read bytes matches TcpClient's ReceiveBufferSize. It worked fine on Windows 7 but seemed have problems on Windows 8.1. As a result, I had to make my buffer size large enough, such as 65536 as title.
I don't think create this quite large size buffer in main loop is a good choice. Any suggestion ?
Thanks a lot.
[update]
I checked this page msdn-tcpclient.receivebuffersize and found that the default size is 8192. Maybe in fact the default size will be different in different platform.
So I just make it safer like this,
var client = tcpListener.AcceptTcpClient();
var data = new byte[client.ReceiveBufferSize];
client.GetStream().Read(data, 0, client.ReceiveBufferSize);
better ?
But my problem is not how to new an enough buffer size for reading purpose, my problem is why ReceiveBufferSize is set to 65536 on Windows 8.1, especially I didn't set manually just keep it default, or otherwise how can it work on Windows 7.
As far as I understand it now, ReceiveBufferSize doesn't actually get the size of the buffer, its just a way for you to set a size manually first, and use it later.
Looks like we're gonna have to set a fixed size here, with no way to get the size of the buffer before it's reading.
https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.receivebuffersize?view=net-5.0