How to host .net tcpclient listner in azure virtual machine

30 Views Asked by At

I have written the TCPClient Listener to listen to a particular port which will send HL7 data to it. Code is working fine in local but we are facing issues when deploying to azure virtual machine.

How to deploy TCPClientListner in azure virtual machine. Should it be a webservice or worker process?

I was going over the net but not able to get the solution. Also looks like there's another option to deploy it in cloud services.

https://cuteprogramming.blog/2018/01/07/tcp-listener-on-microsoft-azure-for-iot-devices/

public class Program{
  static void Main(string[] args)
{
    var host = Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((hostContext, config) =>
    {
        config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
    }).Build();
    var config = host.Services.GetRequiredService<IConfiguration>();

    string IpAddress = config["IpAddress"];
    int port = Convert.ToInt32(config["port"]);

    TcpListener ourTcpListener;

    try
    {
        // Create a TCPListener to accept client connections through port 1080
        ourTcpListener = new TcpListener(IPAddress.Parse(IpAddress), port);

        //start listening
        ourTcpListener.Start();

        Console.Write($"Started TCP Listener...{ourTcpListener.LocalEndpoint}");
    }
    catch (Exception ex)
    {
        //if there was an error starting the listener then print the error and quit
        Console.WriteLine(ex.Message);
        return;
    }

    var receivedByteBuffer = new byte[200];

    for (; ; )
    {
        // Run the listening loop forever
        // this will keep accepting and servicing client connections
        TcpClient acceptTcpClient = null;
        NetworkStream netStream = null;
        try
        {
            Console.Write("Waiting for incoming client connections...");

            acceptTcpClient = ourTcpListener.AcceptTcpClient(); // Get client connection
            netStream = acceptTcpClient.GetStream();

            Console.Write("Handling incoming client connection...");

            // Keep receiving data from the client closes connection
            var totalBytesReceivedFromClient = 0;
            int bytesReceived; // Received byte count
            while ((bytesReceived = netStream.Read(receivedByteBuffer, 0, receivedByteBuffer.Length)) > 0)
            {
                if (netStream.CanWrite)
                {
                    //echo the received data back to the client
                    netStream.Write(receivedByteBuffer, 0, bytesReceived);
                }

                totalBytesReceivedFromClient += bytesReceived;
            }
            var receivedMsg = Encoding.UTF8.GetString(receivedByteBuffer);
            Console.WriteLine("Received Msgs:{0}", receivedMsg);
            Console.WriteLine("Echoed {0} bytes back to the client.", totalBytesReceivedFromClient);
        }
        catch (Exception e)
        {
            //print any exceptions during the communications to the console
            Console.WriteLine(e.Message);
        }
        finally
        {
            // Close the stream and the connection with the client
            netStream?.Close();
            netStream?.Dispose();
            acceptTcpClient?.Close();
        }
    }
}

}

0

There are 0 best solutions below