我无法使UDP端口在Windows Azure虚拟机上工作



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

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

  2. 在Azure管理门户中,我为虚拟机实例添加了端点。我添加了UDP和TCP协议端点。Public和Private端口号均为1234

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

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

我是否错过了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());
            }
        }
    }

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

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)

请随意查看我的实现并查找差异。我确实注意到你正在使用同步版本的"Receive",但这应该无关紧要。

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

最新更新