ManInMoon
ManInMoon

Reputation: 7005

Difficult to explain delay in TcpClient

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

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;
        } 
    }
  }

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.

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

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.

Upvotes: 1

Views: 1101

Answers (1)

ManInMoon
ManInMoon

Reputation: 7005

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

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

Upvotes: 1

Related Questions