简体   繁体   English

NetworkStream出现奇怪的问题并读取消息的前两个字节

[英]Strange issue with NetworkStream and reading the first two bytes of a message

I am running into this strange issue where reading the first two bytes of a TCP message can take 5-10 seconds (after accepting the connection) but reading the rest of the message is almost instant. 我遇到了一个奇怪的问题,其中读取TCP消息的前两个字节可能需要5到10秒钟(接受连接后),但是读取其余消息几乎是即时的。

This only seems to occur when receiving requests from an external integrator, no issues with reading requests from inside the network or even from my computer at home. 仅当接收到来自外部集成商的请求时才出现这种情况,从网络内部甚至从我的家里的计算机读取请求都没有问题。 Because of that I am assuming it is a network issue with us and them. 因此,我假设这是我们和他们之间的网络问题。

An network issue could take forever to resolve so I am hoping there is something I could fix with my code. 网络问题可能需要永远解决,因此我希望我的代码可以解决一些问题。

NetworkStream networkStream = client.GetStream();

byte[] lengthHeader = new byte[2];
stream.Read(lengthHeader, 0, 2);
int length = lengthHeader[0] * 256 + lengthHeader[1];

byte[] buffer = new byte[length];
stream.Read(buffer, 0, length);

Never mind. 没关系。

Was a miscommunication issue between me and the other guy. 是我和另一个人之间的沟通不畅的问题。 Turns out he was expecting a persistent socket connection. 原来他期待一个持久的套接字连接。

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

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