【问题标题】:I cannot make UDP Ports work on a Windows Azure Virtual Machine我无法使 UDP 端口在 Windows Azure 虚拟机上工作
【发布时间】:2014-05-22 08:46:49
【问题描述】:

我无法在 Windows Azure 虚拟机上接收 UDP 数据包。我做了以下事情:

  1. 在虚拟机上,通过 Windows 防火墙,我为 UDP 和 TCP 协议打开了入站和出站端口 1234*。我没有添加任何 IP 排除项。该规则应适用于域、私有和公共配置文件。我允许块边缘遍历。

  2. 在 Azure 管理门户中,我为我的虚拟机实例添加了端点。我添加了 UDP 和 TCP 协议端点。公共和私有端口号都是 1234。

我编写了两个测试程序:UDPSender 和 UDPReceiver。使用我本地网络上的两台计算机,测试程序成功地在它们之间发送了一个数据包。 (编辑:我还使用UDPTester Android App 成功地将“trans-ISP”消息发送到运行 UDPReceiver 的 PC。)

将 UDPReceiver 移动到我的虚拟机,我无法成功接收消息。

我是否遗漏了 Azure 终结点配置中的任何内容?请帮忙!

*端口号更改以保护无辜者。


下面的测试程序代码...


UDPSender:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        textMessage.Text = "Knock, knock";
        textIP.Text = "xxx.xxx.xxx.xxx";
        textPort.Text = "1234";
    }

    private void buttonSend_Click(object sender, EventArgs e)
    {
        UdpClient udpClient = new UdpClient(textIP.Text, Convert.ToInt32(textPort.Text));
        Byte[] sendBytes = Encoding.ASCII.GetBytes(textMessage.Text);
        try
        {
            udpClient.Send(sendBytes, sendBytes.Length);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

UDPReceiver:

private static void Main(string[] args)
    {
        //Creates a UdpClient for reading incoming data.
        UdpClient receivingUdpClient = new UdpClient(1234);
        while (true)
        {
            //Creates an IPEndPoint to record the IP Address and port number of the sender.
            // The IPEndPoint will allow you to read datagrams sent from any source.
            System.Net.IPEndPoint RemoteIpEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
            try
            {

                // Blocks until a message returns on this socket from a remote host.
                Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

                string returnData = Encoding.ASCII.GetString(receiveBytes);

                string messageOut = String.Format("[{0},{1}]@[{2}]: {3}",
                    RemoteIpEndPoint.Address.ToString(),
                    RemoteIpEndPoint.Port.ToString(),
                    DateTime.Now,
                    returnData.ToString());

                Console.WriteLine(messageOut);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }

【问题讨论】:

    标签: sockets networking azure udp virtual-machine


    【解决方案1】:

    代码看起来正确。我将它与我的实现进行了比较。以下是供参考的关键部分:

    您的 csdef 应该具有正确的端口协议。您说您是通过门户执行此操作的,但请确认已保存的设置:

    <Endpoints>
        <InputEndpoint name="UdpEndpoint" protocol="udp" port="8080" localPort="8080" />
    </Endpoints>
    

    (来自https://github.com/ytechie/LogHub/blob/master/Microsoft.LogHub.Cloud/ServiceDefinition.csdef

    监听正确的端口很简单:

    var endPoint = new IPEndPoint(IPAddress.Any, 0);
    var udp = new UdpClient(8080);
    

    (来自https://github.com/ytechie/LogHub/blob/master/Microsoft.LogHub/Global.asax.cs

    请随意查看我的实现并寻找差异。我确实注意到您使用的是“接收”的同步版本,但这不重要。

    我也很好奇这是 PaaS 还是 IaaS。无论哪种情况,您都需要使用负载平衡端点,而不是无法从 Internet 访问的内部端点。

    【讨论】:

    • 我应该清楚我没有将我的服务/exe 部署为云应用程序。我已经部署了一个 Windows Server 2012 虚拟机并安装了我希望在该机器上运行的应用程序。对于安装在虚拟机上的 winforms 可执行文件,我还需要 csdef 文件吗?最终我想将其部署为云服务,但在此之前需要克服一些学习曲线。同样,没有什么比在表单应用程序中填充多行文本框进行调试更重要的了。 ;)
    • 另外,还没有任何服务的负载平衡。
    • Azure 中的虚拟机仍然驻留在云服务中。如果您在您的帐户中查看您的云服务,您会看到它。这将为您提供公共端点。即使您不进行负载平衡,仍然有一个“路由器”需要对您的 VM 进行 NAT。门户将为您提供云服务地址。我的第一段代码将通过 IaaS 的 Azure 门户进行配置(听起来像您这样做了),我将关闭 VM 防火墙进行测试,而不是添加异常。
    • 我将 VM 视为云服务,并且我看到该 VM/云服务的公共端点(包括 UDP 1234)。好消息是关闭虚拟机上的防火墙是可行的。坏消息是,当我需要在迁移到生产环境时锁定 VM 时,我仍然不知道该怎么做。虚拟机的 csdef 文件在哪里?在虚拟机级别?我是否将其添加到我的 Winforms 项目中?并且,感谢您迄今为止的所有帮助。我学到了很多东西。
    • 我应该更清楚。 VM 没有csdef。而是通过门户专门配置 VM。我只是想证明一个 VM 和一个 PaaS 实例都驻留在云服务中,并且具有相似的路由功能,尽管它们可以通过不同的方式进行配置。
    猜你喜欢
    • 2014-01-31
    • 1970-01-01
    • 2016-09-15
    • 2012-10-02
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多