Azure VM - Can't connect to a TCP server that listens on a specific port

感情迁移 提交于 2019-12-24 19:28:32

问题


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.



来源:https://stackoverflow.com/questions/47560932/azure-vm-cant-connect-to-a-tcp-server-that-listens-on-a-specific-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!