使用 TCP 发送/接收时 C# 流乱序
当我在 TCP 流中遇到一些似乎混合的东西时,我正在创建一个小型客户端/服务器应用程序,因此我编写了一个小型测试应用程序
服务器仅创建一个 TCP 套接字并发送一个递增计数器
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 7777);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ipEndPoint);
socket.Listen(10);
Socket clientSocket = socket.Accept();
byte test = 0;
while (true)
{
if (clientSocket.Send(new byte[] { test }, 0, 1, SocketFlags.None) == 1)
{
test++;
}
}
因为这是一个 TCP 流我期望客户端的流从 0 到 255 等等。
在客户端使用以下内容:
IPEndPoint ipEndPoint = new IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 7777);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipEndPoint);
byte[] buffer = new byte[20000];
byte last = 255;
while (true)
{
int bytesRead = socket.Receive(buffer);
for (int i = 0; i < bytesRead; i++)
{
if (buffer[i] != (byte)(last+1))
{
//problem here
}
last = buffer[i];
}
}
if 条件永远不应该返回 true,但它确实返回 true。在我分析客户端的流后,它表明缓冲区通常包含类似 000 001 002 003 004 005 006 102 103 104 105 的序列。
我在这里缺少什么?我一直认为TCP会在TCP层面确保订单交付。
更新:Thorarin指出了正确的解决方案。区域警报正在扰乱流(或者可能是数据包)。我要卸载这个软件。
I was creating a small client/server application when I encountered something that seemed to be a mixup in the TCP streem, so I wrote a small test application
The server just creates a TCP socket and sends an incrementing counter
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 7777);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ipEndPoint);
socket.Listen(10);
Socket clientSocket = socket.Accept();
byte test = 0;
while (true)
{
if (clientSocket.Send(new byte[] { test }, 0, 1, SocketFlags.None) == 1)
{
test++;
}
}
As this is a TCP stream I would expect a stream at the client that just goes from 0 to 255 and so on.
Using the following at the client:
IPEndPoint ipEndPoint = new IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 7777);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipEndPoint);
byte[] buffer = new byte[20000];
byte last = 255;
while (true)
{
int bytesRead = socket.Receive(buffer);
for (int i = 0; i < bytesRead; i++)
{
if (buffer[i] != (byte)(last+1))
{
//problem here
}
last = buffer[i];
}
}
The if condition should never return true but it does. After I analyzed the stream at the client it showed that quite often the buffer contains sequences like 000 001 002 003 004 005 006 102 103 104 105.
What am I missing here? I always thought TCP would ensure in order delivery at the TCP level.
Update: Thorarin pointed to the correct solution. Zone Alarm was messing around with the stream (or probably with the packets). I am going to uninstall this software.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TCP 确实保证订单交付。我的猜测是你的接收缓冲区出了问题。也许您连接两次并使用相同的缓冲区?
我试图重现你的问题,但我不能。我不确定 clientSocketState 指的是什么,因此我使用了常规字节数组作为缓冲区。我懒惰地使用了
Accept
,而不是BeginAccept
,但没有进行其他更改。肯定还有其他事情正在发生,而您没有告诉我们:-)
更新:考虑到一切,我开始相信某些防火墙或防病毒软件正在给您带来麻烦。
TCP does guarantee in order delivery. My guess would be something is going wrong with your receive buffer. Maybe you are connecting twice and using the same buffer?
I've tried to reproduce your problem, but I cannot. I'm not sure what
clientSocketState
is referring to, so I used a regular byte array for the buffer. Instead ofBeginAccept
I lazily usedAccept
, but no other changes were made.Something else must be going on that you're not telling us :-)
Update: considering everything, I'm starting to believe that some firewall or anti-virus software is messing things up for you.