如何检查UPnP端口1900和Bonjour端口5353是否被防火墙阻止

时间:2011-05-28 09:50:06

标签: port firewall upnp blocked

我在C#.NET中使用UPnP列出所有局域网设备,我使用的是C#.NET中的Windows提供的UPnP API(通过添加对UPnP COM库的引用)。当UPnP Scan没有看到任何设备时,我必须检查该端口(1900)是否被防火墙阻止,如果是,我必须通知用户。

扫描UPnP设备的代码

UPnPDeviceFinder devFinder = new UPnPDeviceFinder();
UPnPDevices devices = devFinder.FindByType("upnp:rootdevice", 0);
Debug.WriteLine("Devices Count:=" + devices.Count);

我已连接局域网中的多台设备,我可以在UPnP扫描中看到这些设备。当我阻止1900端口然后devFinder.FindByType(“upnp:rootdevice”,0);返回0个设备。所以我需要编写一些端口扫描代码,告诉我端口是打开还是关闭。由于UPnP使用UDP,当我尝试连接到“239.255.255.250:1900”地址时,我没有得到任何异常。 下面是我写的代码片段

try
{
  Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  IPEndPoint iep = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);

  byte[] data = Encoding.ASCII.GetBytes("This is a test message");
  server.SendTo(data, iep);
  server.Close();
  Console.WriteLine("UPnP Port is open");
}
catch(SocketException ex)
{
  Console.WriteLine("UPnP Port is blocked by firewall");
}

我已经阻止了防火墙中的1900端口,所以我期待SocketException,但我从来没有得到任何异常,所以我无法找到端口是打开还是被阻塞

0 个答案:

没有答案