Sockets 我无法使UDP端口在Windows Azure虚拟机上工作
我无法在Windows Azure虚拟机上接收UDP数据包。我已经做了以下工作:Sockets 我无法使UDP端口在Windows Azure虚拟机上工作,sockets,networking,azure,udp,virtual-machine,Sockets,Networking,Azure,Udp,Virtual Machine,我无法在Windows Azure虚拟机上接收UDP数据包。我已经做了以下工作: 在虚拟机上,通过Windows防火墙,我为UDP和TCP协议打开了入站和出站端口1234*。我没有添加任何知识产权排除。该规则应适用于域、私有和公共配置文件。我允许块边遍历 在Azure管理门户中,我为我的虚拟机实例添加了端点。我添加了UDP和TCP协议端点。公用和专用端口号均为1234 我编写了两个测试程序:UDPSender和udpreciver。使用本地网络上的两台计算机,测试程序成功地在它们之间发送了一个数
*
更改端口号以保护无辜者
测试程序代码如下
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());
}
}
}
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());
}
}
}
udpreciver:
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());
}
}
}
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>
(来自)
请随意查看我的实现并查找差异。我注意到您使用的是同步版本的“Receive”,但这并不重要
我也很好奇这是PaaS还是IaaS。无论哪种情况,您都需要攻击负载平衡端点,而不是无法从internet访问的内部端点。我应该清楚,我没有将我的service/exe部署为云应用程序。我已经部署了Windows Server 2012虚拟机,并安装了我希望在该虚拟机上运行的应用程序。对于安装在虚拟机上的winforms可执行文件,我是否仍然需要csdef文件?最终,我希望将其部署为一项云服务,但在这之前还有一段学习曲线需要克服。同样,没有什么比在表单应用程序中填充多行文本框进行调试更好的了此外,还没有任何服务上的负载平衡。azure中的虚拟机仍然驻留在云服务中。如果您在帐户中查看云服务,您将看到它。这将为您提供公共端点。即使您没有负载平衡,仍然有一个“路由器”需要NAT到您的VM。门户将为您提供云服务地址。我的第一部分代码将通过Azure portal for IaaS进行配置(听起来像是您这样做的),我将关闭VM防火墙进行测试,而不是仅仅添加一个异常。我将VM视为一个云服务,并查看该VM/云服务的公共端点(包括UDP 1234)。好消息是关闭虚拟机上的防火墙是可行的。坏消息是,我仍然不知道在进入生产阶段时需要锁定VM时该怎么做。虚拟机的csdef文件位于何处?在虚拟机级别上?我是否将其添加到Winforms项目中?而且,谢谢你迄今为止的帮助。我学到了很多,我应该更清楚。VM没有csdef。虚拟机是通过门户专门配置的。我只是想说明VM和PaaS实例都驻留在云服务中,并且具有类似的路由功能,尽管它们可以通过不同的方式进行配置。