TCP Listener in .NET 7 deployed on Ubuntu Server environment Responds Slowly Compared to Local Execution

I have a TCP listener application written in .NET 7 which is supposed to listen for incoming client connection, accept connection and process the the massage that comes in and responds to the client.

Locally when I run the TCP listener and client, I am able to send message from the client to the listener and the listener responds fast as it should.

Below is the code for the listener, Please note the listener processes an ISO8583 message

public class TcpServerHostedService : BackgroundService
{
    IPAddress _ipAddress = IPAddress.Parse("127.0.0.1");
    int _port = 2000;
    private readonly IServiceProvider _serviceProvider;
    private readonly IConfiguration _configuration;
    private readonly ILogger<TcpServerHostedService> _logger;

    public TcpServerHostedService(IServiceProvider serviceProvider,
        IConfiguration configuration,
        ILogger<TcpServerHostedService> logger)
    {
        _serviceProvider = serviceProvider;
        _configuration = configuration;
        _logger = logger;
    }



    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {

        TcpListener listener = new TcpListener(_ipAddress, _port);
        listener.Start();
        Console.WriteLine("EchoServer started...");
        while (!stoppingToken.IsCancellationRequested)
        {
            Console.WriteLine("Waiting for incoming client connections...");
            TcpClient client = await listener.AcceptTcpClientAsync();
            Console.WriteLine("Accepted new client connection...");
            NetworkStream stream = client.GetStream();

            try
            {
                using (var scope = _serviceProvider.CreateScope())
                {
                    var _transactionService = scope.ServiceProvider.GetRequiredService<IAfslTransactionService>();
                    var _mailService = scope.ServiceProvider.GetRequiredService<IMailService>();


                    while (!stoppingToken.IsCancellationRequested)
                    {
                        // Read the header bytes
                        byte[] headerBytes = new byte[2];
                        stream.Read(headerBytes, 0, 2);

                        // Calculate the message length based on the header bytes
                        int messageLength = headerBytes[0] * 256 + headerBytes[1];

                        // Receive data from the client
                        byte[] receiveBuffer = new byte[messageLength];
                        int bytesRead = await stream.ReadAsync(receiveBuffer, 0, receiveBuffer.Length);

                        // Convert received bytes to string
                        string receivedData = Encoding.UTF8.GetString(receiveBuffer, 0, bytesRead);


                        // Process the ISO 8583 message
                        string response = await ProcessIsoMessage(receivedData, _transactionService, _mailService);



                        // Convert string message to bytes using UTF-8 encoding
                        string message = response;
                        byte[] messageBytes = Encoding.UTF8.GetBytes(message);

                        // Get the length of the message and convert it to bytes (using 2 bytes for simplicity)
                        byte[] resheaderBytes = BitConverter.GetBytes((short)messageBytes.Length);

                        // Ensure the bytes are in network byte order (big endian)
                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(resheaderBytes);
                        }

                        // Send the length header bytes to the stream
                        await stream.WriteAsync(resheaderBytes, 0, resheaderBytes.Length);

                        // Send the actual message bytes to the stream
                        await stream.WriteAsync(messageBytes, 0, messageBytes.Length);

                        await stream.FlushAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "An error occurred on TCP");
            }
        }
    }


    private async Task<string> ProcessIsoMessage(string isoMessage, IAfslTransactionService _transactionService, IMailService _mailService)
    {
        MessageParser.NET.Tools.ISO8583 iso = new MessageParser.NET.Tools.ISO8583();
        string[] data = iso.Parse(isoMessage);


        Dictionary<int, string> isoFields = ParseStringArrayToDictionary(data);

        string messageTypeCode = isoFields.ContainsKey(0) ? isoFields[0] : "";

        if (!string.IsNullOrEmpty(messageTypeCode))
        {
            Dictionary<int, string> result = new Dictionary<int, string>();

            switch (messageTypeCode)
            {
                case "0100":
                    result = await _transactionService.ProcessHoldReleaseFunds(isoFields);
                    break;
                case "0200":
                    result = await _transactionService.ProcessFinancialTransaction(isoFields);
                    break;
                case "0420":
                    result = await _transactionService.ProcessReversal(isoFields);
                    break;
                case "0800":
                    result = await _transactionService.ProcessNetworkManagement(isoFields);
                    break;
            }

            //Message Type Identifier (Financial Message Response)
            string MTI = result[0];

            //1. Create Arrays AND Assign corresponding data to each array
            string[] DE = ParseDictionaryToStringArray(result);

            //3.Use "Build" method of object iso8583 to create a new  message.
            string NewISOmsg = iso.Build(DE, MTI);

            // Send ISO MESSAGE
            return NewISOmsg;
        }

        return string.Empty;
    }


    static Dictionary<int, string> ParseStringArrayToDictionary(string[] array)
    {
        Dictionary<int, string> dictionary = new Dictionary<int, string>();

        for (int i = 0; i < array.Length; i++)
        {
            // Extract key and value from the item
            int key = i; // Assuming keys are single digits
            string value = array[i];

            // Check if key is "129", assign it to key "0" in dictionary
            if (key == 129)
            {
                dictionary[0] = value;
            }
            // Check if key is "0" or "1", merge them and assign to key "1" in dictionary
            else if (key == 0 || key == 1)
            {
                if (dictionary.ContainsKey(1))
                {
                    dictionary[1] += value;
                }
                else
                {
                    dictionary[1] = value;
                }
            }
            // Check if the value is not "null"
            else if (value != null)
            {
                dictionary[key] = value;
            }
        }

        return dictionary;
    }

    static string[] ParseDictionaryToStringArray(Dictionary<int, string> dictionary)
    {
        dictionary.Remove(0);
        dictionary.Remove(1);
        string[] DE = new string[130];

        for (int i = 0; i < DE.Length; i++)
        {
            string value;

            dictionary.TryGetValue(i, out value);

            DE[i] = !string.IsNullOrEmpty(value) ? value : null;
        }

        return DE;
    }

}

The problem I am facing here is that when I deploy the listener on an ubuntu server I send a message from the TCP Client to the listener and it takes time to respond sometimes even up to 3-5 minutes and this is not good.

Just to mention, I have an Nginx server that handles incoming request for Stream and sends it to my application on the ubuntu server but I don’t think the nginx is the problem because I tried binding the application port to the server port direction and I still got same issue of delayed response.

Please has anyone faced this type of challenge and what can I do to resolve this issue, because this doesn’t look like a code issue

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