Azure VM - Can't connect to a TCP server that

2019-08-19 23:33发布

问题:

I'm trying to establish a TCP connection between an on-prem client and an Azure VM (Standard D1 v2) running Windows 2K16.

On the VM I'm running a simple TCP server (see code below) that receives the port. I've passes port 51515 in the arguments.

The network interface have a public IP I'm trying to use.

I've added an inbound port rule on TCP port 51515 on the portal (source=any, source port=*, destination=any, dest. port=51515, protocol=TCP, action=allow).

Windows Firewall on the VM is off (public, domain and private).

I'm using Telnet from the on-prem side, using the public IP and the 51515 port. Getting the could not open connection message there.

I've tried accessing the IIS on the VM and it is accessible from the on prem using another inbound rule.

Any idea?

Thanks,

Tom

class Program
{
    static void Main(string[] args)
    {
        try
        {
            var server = new TcpListener(new IPEndPoint(IPAddress.Loopback, int.Parse(args[0])));
            server.AllowNatTraversal(true);
            server.Start();
            server.AcceptTcpClientAsync().ContinueWith(c => System.Console.WriteLine("Client connected from:" + c.Result.Client.RemoteEndPoint));
            System.Console.WriteLine("server listens... press any key to exit");
            System.Console.ReadKey();
        }
        catch (Exception e)
        {
            System.Console.WriteLine(e);
            System.Console.WriteLine("press any key to exit");
            System.Console.ReadKey();
        }
    }
}

回答1:

As EJP said, IPAddress.Loopback means 127.0.0.1, the service only could access inside VM.

You need bind port on 0.0.0.0 or VM's private ip. Like below:

var server = new TcpListener(new IPEndPoint(IPAddress.Any, int.Parse(args[0])));


回答2:

It is not only listening on a specific port but also at a specific address. You don't want that. You are binding the server socket to IPAddress.Loopback, i.e. 127.0.0.1, which means it won't accept any connections from outside its local host.

Bind it to 0.0.0.0.