TCP套接字通信限制 [英] TCP Socket Communication Limit

查看:94
本文介绍了TCP套接字通信限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TCP客户端可以接收的数据大小是否有任何限制. 通过TCP套接字通信,服务器正在发送更多数据,但客户端仅获得4K并停止.

Is there any limit on the size of data that can be received by TCP client. With TCP socket communication, server is sending more data but the client is only getting 4K and stopping.

推荐答案

您可以考虑将读/写分为多个调用.过去,我肯定在TcpClient上遇到过一些问题.为了解决这个问题,我们使用带有包装的流类和以下read/write方法:

You may consider splitting your read/writes over multiple calls. I've definitely had some problems with TcpClient in the past. To fix that we use a wrapped stream class with the following read/write methods:

public override int Read(byte[] buffer, int offset, int count)
{
    int totalBytesRead = 0;
    int chunkBytesRead = 0;
    do
    {
        chunkBytesRead = _stream.Read(buffer, offset + totalBytesRead, Math.Min(__frameSize, count - totalBytesRead));
        totalBytesRead += chunkBytesRead;
    } while (totalBytesRead < count && chunkBytesRead > 0);
    return totalBytesRead;
}

    public override void Write(byte[] buffer, int offset, int count)
    {
        int bytesSent = 0;
        do
        {
            int chunkSize = Math.Min(__frameSize, count - bytesSent);
            _stream.Write(buffer, offset + bytesSent, chunkSize);
            bytesSent += chunkSize;
        } while (bytesSent < count);
    }

//_stream is the wrapped stream
//__frameSize is a constant, we use 4096 since its easy to allocate.

这篇关于TCP套接字通信限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆