难以解释 TcpClient 中的延迟 [英] Difficult to explain delay in TcpClient

查看:28
本文介绍了难以解释 TcpClient 中的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 ITCH 协议服务器接收大量数据(经常是小消息).

I am receiving large amounts of data (frequent small messages) from an ITCH protocol server.

我的应用运行良好,因为所有消息都被正确解析.但是,当(据我所知)除了代码设置在ReadByte"之外什么都没有发生时,我会遇到神秘的延迟.

My app works well in that all messages are correctly parsed. However, I get mysterious delays when ( as far as I can tell) nothing is happening except code is setting at "ReadByte".

TcpClient simpleTcp = new TcpClient(serverAddress, serverPort);
simpleTcp.NoDelay = true;
simpleTcp.Client.NoDelay = true;                   
simpleTcp.Client.ReceiveBufferSize = 1024; 
simpleTcp.ReceiveBufferSize = 1024;
simpleTcp.ReceiveTimeout = 5000;


NetworkStream tcpStream = simpleTcp.GetStream();

//Send login and subscciptions...

while (true)
{
    lock (TcpLock)
    {
        int abyte = tcpStream.ReadByte();
        try
        {
            LbTime.Stopwatch.Restart();
            Console.WriteLine("\n>>>>GetMessage Start: " + DateTime.UtcNow.ToString("HH:mm:ss.fff") + " " + LbTime.Stopwatch.ElapsedMilliseconds.ToString("F0"));

            //Process message (Complex)

            Console.WriteLine(">>>>GetMessage End: " + LbTime.Stopwatch.ElapsedMilliseconds.ToString("F0") + " Now: " + DateTime.UtcNow.ToString("HH:mm:ss.fff") + "\n");
        }
        catch (Exception err)
        {
            throw;
        } 
    }
  }

我显示处理开始和结束的时间.但是,当我收到稍后的消息时 - 时间与收到的消息中埋藏的时间有很大不同.IE.我收到消息晚了.

I show the time at beginning and end of processing. However, when I get a later message - the time differs greatly from the time buried in the message received. I.e. I am receiving the message late.

我已经在 2 台机器上尝试过这个,后者没有在上面运行其他任何东西,有时我仍然会遇到神秘的延迟.

I have tried this on 2 machines, the latter had nothing else running on it, and I still get mysterious delays sometimes.

我理解这很难提供帮助,因为我无法提供工作示例,因为它依赖于连接到专用 ITCH 协议服务器.

I undertand this is very difficult to help with as I am unable to provide a working example because it is dependent on connecting to a private ITCH protocol server.

如果有人能提供一些启示那就太好了.

If any one can shed some light that would be great.

推荐答案

这种延迟让我无法解释.但是,类似问题的几个答案表明使用原始套接字会更好.

This delay remained inexplicable to me. However, several answers to similar questions suggested that using raw Sockets would be better.

我用 Socket 替换了我所有的 Stream 和 NetworkStreams,现在 Socket.Receive() 没有任何临时延迟.

I replacde all my Stream and NetworkStreams with Socket and now the Socket.Receive() works without any ad-hoc delays.

这篇关于难以解释 TcpClient 中的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆