Latency in receiving TCP String sent from TCPServer

I’m working on an application that requires a very responsive TCP communication across two PC (connected through direct Ethernet). I’ve used the code below to benchmark the responsiveness of the TCP socket (both TCP Server & TCP Client):

TCP Server

class Program
{
    static void Main(string[] args)
    {
        string serverAddress = "192.168.23.88"; // Localhost
        int serverPort = 6012;

        TcpListener server = new TcpListener(IPAddress.Parse(serverAddress), serverPort);
        server.Server.NoDelay = true;
        server.Start();
        Console.WriteLine($"Server started {serverAddress}:{serverPort}...");

        while (true)
        {
            var client = server.AcceptTcpClient();
            client.NoDelay = true;
            Console.WriteLine("Client connected...");
            HandleClientAsync(client); // Fire and forget
        }
    }


    private static void HandleClientAsync(TcpClient client)
    {
        using (client)
        {
            var buffer = new byte[20];
            var stream = client.GetStream();

            var t1RP = Encoding.UTF8.GetBytes("<TEST,1>");
            var t2RP = Encoding.UTF8.GetBytes("<TEST,2>");
            var t3RP = Encoding.UTF8.GetBytes("<TEST,3>");

            List<string> consoleList = new List<string>();
            //string tmp = "";
            while (true)
            {
                int bytesRead;
                try
                {
                    bytesRead = stream.Read(buffer, 0, buffer.Length);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error reading from client: {e.Message}");
                    foreach (var a in consoleList)
                        Console.WriteLine(a);

                    break;
                }

                stream.Write(sovRP, 0, t1RP.Length);
                stream.Write(grbRP, 0, t2RP.Length);
                stream.Write(resRP, 0, t3RP.Length);
            }
        }
    }
}

TCP Client

static async Task Main(string[] args)
{
    string serverAddress = "192.168.23.88"; // Localhost
    int serverPort = 6012;

    using (TcpClient client = new TcpClient())
    {
        try
        {
            int cycle = 5000;
            client.NoDelay = true;
            Console.WriteLine("Connecting to server...");
            await client.ConnectAsync(serverAddress, serverPort);
            Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} Connected to server {serverAddress}:{serverPort}");

            Stopwatch time = new Stopwatch();
            Stopwatch elapsedTime = new Stopwatch();

            var stream = client.GetStream();
            int count = 0;
            {
                byte[] buffer = new byte[20];
                for (int i = 0; i < cycle; ++i)
                {
                    long responseTime = 0;
                    long grabTime = 0;
                    long resultTime = 0;
                    byte[] data = Encoding.UTF8.GetBytes($"<SOV,1,4,>");
                    stream.Write(data, 0, data.Length);
                    _ = stream.FlushAsync();

                    elapsedTime.Restart();
                    
                    string combined = "";
                    while (true)
                    {
                        int bytesRead = stream.Read(buffer, 0, buffer.Length);
                        combined += Encoding.UTF8.GetString(buffer, 0, bytesRead);
                        if (combined.Contains("<TEST,1>"))
                        {
                            _ = stream.FlushAsync();
                            responseTime = elapsedTime.ElapsedMilliseconds;
                            combined = combined.Replace("<TEST,1>", "");
                            if (responseTime > 3)
                            {
                                Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} SOV Response Time {responseTime}");
                                responseTime = elapsedTime.ElapsedMilliseconds;
                                count++;
                            }
                            break;
                        }
                    }
                    //Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} Received from server: {response}");

                    while (true)
                    {
                        int bytesRead = stream.Read(buffer, 0, buffer.Length);
                        combined += Encoding.UTF8.GetString(buffer, 0, bytesRead);
                        if (combined.Contains("<TEST,2>"))
                        {
                            grabTime = elapsedTime.ElapsedMilliseconds - responseTime;
                            combined = combined.Replace("<TEST,2>", "");
                            if (grabTime > 3)
                            {
                                _ = stream.FlushAsync();
                                Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} SOV Grab Time {grabTime}");
                                grabTime = elapsedTime.ElapsedMilliseconds - responseTime;
                                count++;
                            }
                            break;
                        }
                    }
                    //Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} Received from server: {response}");

                    while (true)
                    {
                        int bytesRead = stream.Read(buffer, 0, buffer.Length);
                        combined += Encoding.UTF8.GetString(buffer, 0, bytesRead);
                        if (combined.Contains("<TEST,3>"))
                        {
                            resultTime = elapsedTime.ElapsedMilliseconds - responseTime - grabTime;
                            combined = combined.Replace("<TEST,3>", "");
                            if (resultTime > 10)
                            {
                                _ = stream.FlushAsync();
                                Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} SOV Result Time {resultTime}");
                                resultTime = elapsedTime.ElapsedMilliseconds - responseTime - grabTime;
                                count++;
                            }
                            break;
                        }
                    }
                    //Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} Received from server: {response}");

                    time.Restart();
                    while (time.ElapsedMilliseconds < 50)
                    {

                    }

                    if ((i % 50) == 0)
                        Console.WriteLine($"Complete {i} Cycle");
                }
            }
            Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} Completed {cycle} Cycle, Exceed Count {count}");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.ReadLine();
    }

    Console.WriteLine("Connection closed");
}

I’ve noticed that in 5000 cycles, I will randomly get some delayed response (5ms – 20ms).

I’ve used WireShark to monitor the TCP stream in both Server and Client. In Server side, TCP is sent out immediately but in Client side, there’s some latency:

Server Side

Client Side

Is there anything I have missing out in TCP configuration?

Thanks in advance.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật