TCP客户端可以接收的数据大小是否有限制。 使用TCP套接字通信,服务器正在发送更多数据,但客户端只获得4K并停止。
答案 0 :(得分:7)
我猜你正在做1 Send
和1 Receive
。
您需要进行多次读取,不能保证从套接字中读取的内容将包含所有内容。
Receive
方法将读取尽可能多的数据,最大可达缓冲区的大小。但是当它有一些数据时它会返回,所以你的程序可以使用它。
答案 1 :(得分:2)
不,应该没问题。我怀疑从客户端读取的代码存在缺陷,但如果没有实际显示它,很难说。
答案 2 :(得分:2)
没有限制,TCP套接字是一个流。
答案 3 :(得分:2)
您可以考虑通过多次调用拆分读/写。我在过去肯定遇到过TcpClient
的问题。为了解决这个问题,我们使用带有以下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.
答案 4 :(得分:1)
理论上对TCP的数据没有限制但由于我们受到物理资源(即内存)的限制,Microsoft Winsock等实现者使用的是“tcp窗口大小”。
这意味着当您使用Winsock的send()函数发送内容时(并且未在套接字处理程序上设置任何选项),数据将首先复制到套接字的临时缓冲区。只有当接收方承认他获得了这些数据时,Winsock才会再次使用这个内存。
所以,你可以通过发送比释放更快的速度来泛洪这个缓冲区然后 - 错误!