使用Stream.ReadByte()时出现延迟

时间:2017-09-06 16:30:26

标签: c# tcp

我正在通过NetworkStream从tcpClient读取可变大小的数据。

以下是代码:

$("td.focusable").on('click', function() {
    var tds = $(this).siblings();
    tds.each(function(index, tdElement { 
      tdElement.first().attr("readonly", true) // ensures all td elements' input child element will be enabled to readonly
    });
    $(this).first().attr("readonly", false);
    // do your other work with this
});

输出:

  public void ReadCompleteMessageUntilTLF(NetworkStream tcpStream)
    {
        Console.WriteLine("Start ReadCompleteMessageUntilTLF: " + LbTime.Stopwatch.ElapsedMilliseconds.ToString("F0"));
        List<byte> byteList = new List<byte>();
        while (true )
        {
            Console.WriteLine("              Pre ReadCompleteMessageUntilTLF: " + LbTime.Stopwatch.ElapsedMilliseconds.ToString("F0"));

            while (!tcpStream.CanRead)
            {
                System.Threading.Thread.Sleep(1);
            }
            int abyte = tcpStream.ReadByte();
            if (abyte == -1)
            {
                Console.WriteLine("     xxx");
            }
            byteList.Add((byte)abyte);
            if (abyte == ItchConstants.TLF)
                break;
        }
        MessageData = byteList.ToArray();
        Console.WriteLine("End ReadCompleteMessageUntilTLF: " + LbTime.Stopwatch.ElapsedMilliseconds.ToString("F0"));
    }

   public static GetMessage(NetworkStream tcpStream)
    {
        lock (TcpLock)
        {
            int abyte = tcpStream.ReadByte();
            try
            {
                ReadCompleteMessageUntilTLF(tcpStream);
                //Do stuff with received data
            }
            catch (Exception err)
            {
                throw;
            } 
        }
    }

但是,我从wireshark那里知道消息是以1个数据包到达的。

什么可能导致这种延迟?

0 个答案:

没有答案