0

尝试将 TcpClient 缓冲区中的数据读取到字节数组中:

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

客户端是一个System.Net.Sockets.TcpClient. 异常发生在nStream.Read

由 Debian-6.0_64 / Mono 2.11.2 运行时的输出为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 

buffSize 比字节数组长度大得多,但是当我在 Windows 上运行完全相同的程序时不会发生该异常(没有单声道,没有尝试过)。是什么赋予了?我该如何解决这个问题?是像增加缓冲区大小一样简单,还是有其他事情发生?我问的原因(而不仅仅是尝试)是因为我不知道发生了什么,我想确保我正确地完成它。

我想找出这种行为背后的原因,因为看到这样我希望它也会在 Windows 上崩溃。除非缓冲区的大小由操作系统确定,并且在 Windows 上它更小/等于字节数组大小..有人可以解释吗?

4

1 回答 1

6

看起来 MonoTcpClient.ReceiveBufferSize比 Windows 上的要大。

根据MSDN Docs,默认值为 8192 字节。

我刚刚检查了 Mono 的实现,它正在调用GetSocketOption()底层套接字,所以它使用的是操作系统的默认值。

你有三个选择:

  1. 使用 更改套接字的接收缓冲区大小client.ReceiveBufferSize = new_size
  2. 分配足够大的缓冲区。
  3. 不要读取超过缓冲区可以容纳的数据:

    var max_size = Math.Min (client.ReceiveBufferSize, buffer.Length);
    bytesRead = nStream.Read(inStream, 0, max_size);
    
于 2012-10-20T20:57:39.680 回答