【发布时间】:2014-05-22 08:46:49
【问题描述】:
我无法在 Windows Azure 虚拟机上接收 UDP 数据包。我做了以下事情:
在虚拟机上,通过 Windows 防火墙,我为 UDP 和 TCP 协议打开了入站和出站端口 1234*。我没有添加任何 IP 排除项。该规则应适用于域、私有和公共配置文件。我允许块边缘遍历。
在 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