Trying to read data from a TcpClient buffer into a byte array:
private byte[] excessData;
public bool receiveMessage(ref string recvStr, string daddy = "")
{
recvStr = "";
CSocket_PacketProtocol packprot = new CSocket_PacketProtocol(0, this);
WriteDebugWithTimestamp("receiveMessage obtaining client NetworkStream - daddy = " + daddy);
NetworkStream nStream = client.GetStream();
WriteDebugWithTimestamp("receiveMessage client NetworkStream obtained, entering receive loop");
int bytesRead = 0;
do
{
if (!packprot.done)
{
try
{
byte[] inStream = new byte[10025]; //We'll read into this stream
int buffSize = client.ReceiveBufferSize;
WriteDebugWithTimestamp(String.Format("Starting nStream.Read"));
WriteDebugWithTimestamp(String.Format(String.Format("buffSize={0} -- inStream.Length={1}", buffSize, inStream.Length)));
bytesRead = nStream.Read(inStream, 0, buffSize); //Recieve data from stream to inStream, return amount of bytes read
WriteDebugWithTimestamp(String.Format("receiveMessage bytesread: {0}", bytesRead.ToString()));
WriteDebugWithTimestamp(String.Format("receiveMessage client.Connected: {0}", client.Connected.ToString()));
../Cut
Where client is a System.Net.Sockets.TcpClient. The exception occurs at nStream.Read.
Output when run by Debian-6.0_64 / Mono 2.11.2 as Mono --runtime=v4.0.30319 Program.exe:
receiveMessage obtaining client NetworkStream - daddy =
receiveMessage client NetworkStream obtained, entering receive loop
receiveMessage this.excessData == null: True -- excessdata.length=n/a
Starting nStream.Read
buffSize=87380 -- inStream.Length=10025
System.ArgumentOutOfRangeException: Argument is out of range.
Parameter name: offset+size exceeds the size of buffer
at System.Net.Sockets.NetworkStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0
at SocketOperations.CSocketOperations.receiveMessage (System.String& recvStr, System.String daddy) [0x00000] in <filename unknown>:0
at SocketServerV3_Demo.CDownloadServer_ClientHandle..ctor (System.Net.Sockets.TcpClient client) [0x00000] in <filename unknown>:0
at SocketServerV3_Demo.Program.handleNewConnection (System.Net.Sockets.TcpClient client) [0x00000] in <filename unknown>:0
The buffSize is way bigger than the byte array length, but that exception doesn’t occur when I run the exact same program on windows (without mono, haven’t tried with). What gives? And how do I fix this? Is it as simple as increasing the buffer size or is there something else going on? The reason I’m asking (rather than just trying it) is because I haven’t got a clue what’s going on and I want to make sure I get it done correctly.
I’d like to find out the reason behind this behaviour, because seeing it like this I’d expect it to crash on windows as well. Unless the size of the buffer is determined by the OS and on Windows it’s smaller/equal to the byte array size.. can anyone explain?
Looks like Mono’s
TcpClient.ReceiveBufferSizeis larger than on Windows.According to the MSDN Docs, the default is 8192 bytes.
I just checked Mono’s implementation and it’s calling
GetSocketOption()on the underlying socket, so it’s using the operating system’s default.You have three options:
client.ReceiveBufferSize = new_size.Don’t read more data than the buffer can hold: