简体   繁体   English

TCP套接字通信限制

[英]TCP Socket Communication Limit

Is there any limit on the size of data that can be received by TCP client. TCP客户端可以接收的数据大小是否有限制。 With TCP socket communication, server is sending more data but the client is only getting 4K and stopping. 使用TCP套接字通信,服务器正在发送更多数据,但客户端只获得4K并停止。

I'm guessing that you're doing exactly 1 Send and exactly 1 Receive . 我猜你正在做1 Send和1 Receive

You need to do multiple reads, there is no guarantee that a single read from the socket will contain everything. 您需要进行多次读取,不能保证从套接字中读取的所有内容都包含所有内容。

The Receive method will read as much data as is available, up to the size of the buffer. Receive方法将读取尽可能多的数据,最大可达缓冲区的大小。 But it will return when it has some data so your program can use it. 但是当它有一些数据时它会返回,所以你的程序可以使用它。

No, it should be fine. 不,应该没问题。 I suspect that your code to read from the client is flawed, but it's hard to say without you actually showing it. 我怀疑从客户端读取的代码存在缺陷,但如果没有实际显示它,很难说。

没有限制,TCP套接字是一个流。

You may consider splitting your read/writes over multiple calls. 您可以考虑在多个调用中拆分读/写。 I've definitely had some problems with TcpClient in the past. 我在过去肯定遇到过TcpClient问题。 To fix that we use a wrapped stream class with the following read/write methods: 为了解决这个问题,我们使用带有以下read/write方法的包装流类:

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.

There's no limit for data with TCP in theory BUT since we're limited by physical resources (ie memory), implementors such as Microsoft Winsock utilize something called "tcp window size". 理论上对TCP的数据没有限制,因为我们受到物理资源(即内存)的限制,Microsoft Winsock等实现者使用了一种称为“tcp窗口大小”的东西。

That means that when you send something with the Winsock's send() function for example (and didn't set any options on the socket handler) the data will be first copied to the socket's temporary buffer. 这意味着当您使用Winsock的send()函数发送内容时(并且未在套接字处理程序上设置任何选项),数据将首先复制到套接字的临时缓冲区。 Only when the receiving side has acknowledged that he got that data, Winsock will use this memory again. 只有当接收方确认他已获得该数据时,Winsock才会再次使用此内存。

So, you might flood this buffer by sending faster than it frees up and then - error! 所以,你可以通过发送比释放更快的速度来缓冲这个缓冲区然后 - 错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM