加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – TCP套接字通信限制

发布时间:2020-12-15 19:35:23 所属栏目:百科 来源:网络整理
导读:TCP客户端可以接收的数据大小是否有限制. 使用TCP套接字通信,服务器正在发送更多数据,但客户端只获得4K并停止. 解决方法 您可以考虑在多个调用中拆分读/写.我在过去肯定遇到过TcpClient的问题.为了解决这个问题,我们使用带有以下读/写方法的包装流类: publi
TCP客户端可以接收的数据大小是否有限制.
使用TCP套接字通信,服务器正在发送更多数据,但客户端只获得4K并停止.

解决方法

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

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 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读