【发布时间】:2012-01-11 06:02:53
【问题描述】:
这是我用来测试嵌入式产品上的网络服务器的代码,当 HTTP 请求通过多个 TCP 数据包分段进入时,该产品表现不佳:
/* This is all within a loop that cycles size_chunk up to the size of the whole
* test request, in order to test all possible fragment sizes. */
TcpClient client_sensor = new TcpClient(NAME_MODULE, 80);
client_sensor.Client.NoDelay = true; /* SHOULD force the TCP socket to send the packets in exactly the chunks we tell it to, rather than buffering the output. */
/* I have also tried just "client_sensor.NoDelay = true, with no luck. */
client_sensor.Client.SendBufferSize = size_chunk; /* Added in a desperate attempt to fix the problem before posting my shameful ignorance on stackoverflow. */
for (int j = 0; j < TEST_HEADERS.Length; j += size_chunk)
{
String request_fragment = TEST_HEADERS.Substring(j, (TEST_HEADERS.Length < j + size_chunk) ? (TEST_HEADERS.Length - j) : size_chunk);
client_sensor.Client.Send(Encoding.ASCII.GetBytes(request_fragment));
client_sensor.GetStream().Flush();
}
/* Test stuff goes here, check that the embedded web server responded correctly, etc.. */
查看 Wireshark,我看到只有一个 TCP 数据包发出,其中包含整个测试标头,而不是我期望的大约 header length / chunk size 数据包。我之前使用过 NoDelay 来关闭 Nagle 算法,它通常像我期望的那样工作。 NoDelayhttp://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.nodelay%28v=vs.90%29.aspx 的在线文档在其相关代码示例中明确指出“在调用 NetworkStream.Write 时立即发送数据”,所以我认为我一直在正确使用它。
无论我是否单步执行代码,都会发生这种情况。 .NET 运行时是否优化了我的数据包碎片?
我正在运行 x64 Windows 7、.NET Framework 3.5、Visual Studio 2010。
【问题讨论】:
-
我想知道 WireShark 是否会在数据包在线后合并数据包。在这种情况下它似乎根本不适用,但它显然确实有一些 reassembly 功能。这似乎不太可能,我可能只是在浪费您的时间,建议您将其视为一种可能性。
-
我建议不要使用诸如“nanny-state”之类的贬义词,直到您确定您正确使用了系统。上马的第一条规则是,“确保你的不只是一头驴。”
-
@KennetBelenky,请注意,我已经删除了有问题的文字。不过我只是在开玩笑=)
-
@SamSkuce 谢谢和NP。我只是有点生气,因为人们通常没有意识到他们可以避免多少行烦人的样板 C++,以换取.Net 偶尔的怪异。话虽如此,我已经多次将头撞在 .Net 上,但每次都证明我是没有掌握全局的人。