无法接收 UDP 数据包
Unable to receive UDP packets
我正在尝试使用两个单独的程序从同一台机器发送和接收 UDP 数据包,但到目前为止我还没有收到任何东西。我已经尝试完全关闭我的防火墙,但仍然没有用。
发送节目
var sender = new UdpClient(Dns.GetHostName(), 2055);
var data = new byte[] { 0, 1, 2, 3, 4, 5 };
sender.Send(data, data.Length);
接收节目
var receiver = new UdpClient(2055);
var endPoint = new IPEndPoint(IPAddress.Any, 2055);
var data = receiver.Receive(ref endPoint);
System.Console.WriteLine("Data received");
我什至复制并粘贴了本教程中的代码http://www.codeproject.com/Articles/10649/An-Introduction-to-Socket-Programming-in-NET-using,但仍然没有成功。
来自About multicast IP addresses and ports (TechNet):
Multicast IP addresses are class-D addresses that fall within two ranges: 224.0.0.0 through 239.255.255.255 and FF00:0000:0000:0000:0000:0000:0000:0000 through FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF. The addresses in the first range are Internet Protocol version 4 (IPv4) addresses. The addresses in the second range are Internet Protocol version 6 (IPv6) addresses.
For intranet use, it is recommended that you use IPv4 addresses in the range 239...*. Port numbers can range between 1 and 65535. The routers on the network over which you are transmitting your content must be multicast-enabled, meaning that they must be able to interpret class-D addresses or they will not be able to forward your multicast information to clients.
由于网络级过滤,您必须使用此范围内的 IP 地址。维基百科上有 table 个 commonly-used multicast addresses。
实际上,您使用的教程也注意到了这一点:
UDP also supports multicasting i.e. sending a single datagram to multiple receivers. To do so, the sender sends a packet to an IP address in the range 224.0.0.1 – 239.255.255.255 (Class D address group).
问题出在var sender = new UdpClient(Dns.GetHostName(), 2055);
您需要指定您的本地主机 IP,即 127.0.0.1
,因为您在同一台机器上。这应该有效:
var sender = new UdpClient("127.0.0.1", 2055);
我正在尝试使用两个单独的程序从同一台机器发送和接收 UDP 数据包,但到目前为止我还没有收到任何东西。我已经尝试完全关闭我的防火墙,但仍然没有用。
发送节目
var sender = new UdpClient(Dns.GetHostName(), 2055);
var data = new byte[] { 0, 1, 2, 3, 4, 5 };
sender.Send(data, data.Length);
接收节目
var receiver = new UdpClient(2055);
var endPoint = new IPEndPoint(IPAddress.Any, 2055);
var data = receiver.Receive(ref endPoint);
System.Console.WriteLine("Data received");
我什至复制并粘贴了本教程中的代码http://www.codeproject.com/Articles/10649/An-Introduction-to-Socket-Programming-in-NET-using,但仍然没有成功。
来自About multicast IP addresses and ports (TechNet):
Multicast IP addresses are class-D addresses that fall within two ranges: 224.0.0.0 through 239.255.255.255 and FF00:0000:0000:0000:0000:0000:0000:0000 through FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF. The addresses in the first range are Internet Protocol version 4 (IPv4) addresses. The addresses in the second range are Internet Protocol version 6 (IPv6) addresses.
For intranet use, it is recommended that you use IPv4 addresses in the range 239...*. Port numbers can range between 1 and 65535. The routers on the network over which you are transmitting your content must be multicast-enabled, meaning that they must be able to interpret class-D addresses or they will not be able to forward your multicast information to clients.
由于网络级过滤,您必须使用此范围内的 IP 地址。维基百科上有 table 个 commonly-used multicast addresses。
实际上,您使用的教程也注意到了这一点:
UDP also supports multicasting i.e. sending a single datagram to multiple receivers. To do so, the sender sends a packet to an IP address in the range 224.0.0.1 – 239.255.255.255 (Class D address group).
问题出在var sender = new UdpClient(Dns.GetHostName(), 2055);
您需要指定您的本地主机 IP,即 127.0.0.1
,因为您在同一台机器上。这应该有效:
var sender = new UdpClient("127.0.0.1", 2055);