简体   繁体   English

NetworkStream读取速度极慢

[英]NetworkStream Read Extremely Slow

I'm using a C# NetworkStream to read/write to an IP address (not a DNS address). 我正在使用C# NetworkStream读取/写入IP地址(而不是DNS地址)。
My program is replacing a very old assembly language program that was on a mainframe. 我的程序正在替换大型机上非常老的汇编语言程序。 Now it's on Windows. 现在在Windows上。

I'm writing/reading less than 200 bytes. 我正在写/读少于200个字节。 The strings end with a LineFeed character so I'm using a StreamReader.Readline() to read a response, after my Stream.Write() . 字符串以LineFeed字符结尾,因此我在Stream.Write()之后使用StreamReader.Readline()读取响应。 On the IBM a write/read cycle took 300ms . 在IBM上,写/读周期花费300ms

Now about after every 2nd or 3 read, it takes 15 seconds for the read. 现在大约每2或3次读取后,读取就需要15秒。 When I read the log of the sender it is sending the data in less than a second. 当我阅读发送者的日志时,它在不到一秒钟的时间内发送了数据。 For some reason I get these 15 second delays. 由于某种原因,我得到了这15秒的延迟。

I'm clueless on what's happening. 我对正在发生的事情一无所知。

ps One weird thing I noticed if I set the stream read timeout to 4 seconds, it times out around 4 seconds. ps我注意到,如果我将流读取超时设置为4秒,那么它会超时约4秒,这是我注意到的一件奇怪的事情。 If I set the timeout to 10 seconds or no timeout, it times out after 15 seconds. 如果我将超时设置为10秒或不设置超时,则它将在15秒后超时。

TcpClient tcpc = null;
NetworkStream stream = null;
StreamReader sr = null;

tcpc = new TcpClient();
tcpc.NoDelay = true;
tcpc.ExclusiveAddressUse = false;
tcpc.Connect("172.18.10.100", 4004);

stream = tcpc.GetStream();
sr = new StreamReader(stream, Encoding.ASCII);
sr.Peek();

string Message = null;
Message = "IX3543543" + '\r';
stream.Write(Encoding.ASCII.GetBytes(Message), 0, Message.Length);
string readmsg = null;
for (int i = 0; i < 4; i++)
    readmsg = sr.ReadLine();

Your connection stays open as your code never free your IDisposable resources. 由于代码永远不会释放IDisposable资源,因此您的连接保持打开状态。

  1. I think that your code should run faster if you add the using constructure. 我认为,如果添加using结构,您的代码应该运行得更快。
  2. Also you can merge declaration and assignment, like this (this is only a style comment, the main concern is `IDisposable usage) 您也可以像这样合并声明和赋值(这只是一个样式注释,主要关注的是`IDisposable用法)
  3. And you can ReadToEnd your message in return, and examine it by youself after releasing the resources. 您可以返回ReadToEnd消息,并在释放资源后ReadToEnd检查。

So your code could look something like this: 因此您的代码可能如下所示:

string response = null;
using(var tcpc = new TcpClient())
{
    tcpc.NoDelay = true;
    tcpc.ExclusiveAddressUse = false;
    tcpc.Connect("172.18.10.100", 4004);

    using (var stream = tcpc.GetStream())
    using (var sr = new StreamReader(stream, Encoding.ASCII))
    {
        sr.Peek();

        var Message = "IX3543543" + '\r';
        stream.Write(Encoding.ASCII.GetBytes(Message), 0, Message.Length);
        response = sr.ReadToEnd();
    }
}

// examine message here
var lines = response.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

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

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