I cannot make UDP Ports work on a Windows Azure Virtual Machine

心不动则不痛 提交于 2021-02-10 15:53:22

问题


I cannot receive a UDP packet on a Windows Azure Virtual Machine. I have done the following:

  1. On the Virtual Machine, via Windows Firewall, I opened up Port 1234* both Inbound and Outbound for both UDP and TCP protocols. I did not add any IP exclusions. The rule should apply to Domain, Private and Public profiles. I am allowing Block Edge Traversal.

  2. In the Azure Management Portal, I added Endpoints for my Virtual Machine instance. I added both UDP and TCP protocol endpoints. Public and Private port numbers are both 1234.

I have two test programs that I wrote: UDPSender and UDPReceiver. Using two computers on my local network, the test programs successfully sent a packet between them. (Edit: I also used UDPTester Android App to successfully send a 'trans-ISP' message to my PC running the UDPReceiver.)

Moving UDPReceiver to my Virtual Machine, I cannot successfully receive a message.

Did I miss anything in my Azure Endpoint configuration? Please Help!

* Port Number changed to protect the innocent.


Test Program Code Below...


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());
            }
        }
    }

回答1:


The code looks correct. I compared it against my implementation. Here are the key sections for reference:

Your csdef should have the correct port protocol. You said you did this through the portal, but confirm the settings saved:

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

(from https://github.com/ytechie/LogHub/blob/master/Microsoft.LogHub.Cloud/ServiceDefinition.csdef)

And listening on the correct port was as easy as:

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

(from https://github.com/ytechie/LogHub/blob/master/Microsoft.LogHub/Global.asax.cs)

Feel free to take a look at my implementation and look for differences. I did notice that you're using the synchronous version of "Receive", but that shouldn't matter.

I'm also curious if this is PaaS or IaaS. In either case, you'll need to hit against the load balanced endpoint, and not an internal endpoint which would be inaccessible from the internet.



来源:https://stackoverflow.com/questions/23794543/i-cannot-make-udp-ports-work-on-a-windows-azure-virtual-machine

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