[英]TCP Socket Communication Limit
TCP客户端可以接收的数据大小是否有限制。 使用TCP套接字通信,服务器正在发送更多数据,但客户端只获得4K并停止。
我猜你正在做1 Send
和1 Receive
。
您需要进行多次读取,不能保证从套接字中读取的所有内容都包含所有内容。
Receive
方法将读取尽可能多的数据,最大可达缓冲区的大小。 但是当它有一些数据时它会返回,所以你的程序可以使用它。
不,应该没问题。 我怀疑从客户端读取的代码存在缺陷,但如果没有实际显示它,很难说。
没有限制,TCP套接字是一个流。
您可以考虑在多个调用中拆分读/写。 我在过去肯定遇到过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.
理论上对TCP的数据没有限制,因为我们受到物理资源(即内存)的限制,Microsoft Winsock等实现者使用了一种称为“tcp窗口大小”的东西。
这意味着当您使用Winsock的send()函数发送内容时(并且未在套接字处理程序上设置任何选项),数据将首先复制到套接字的临时缓冲区。 只有当接收方确认他已获得该数据时,Winsock才会再次使用此内存。
所以,你可以通过发送比释放更快的速度来缓冲这个缓冲区然后 - 错误!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.