开发者

How to check whether UPnP port 1900 and Bonjour port 5353 is blocked by the firewall

I am listing the all the local area network devices using UPnP in C# .NET, I am using UPnP API's given by windows in C# .NET (by adding reference to UPnP COM Library). When UPnP Scan don't see any device then I have to check whether that port (1900) is blocked by the firewall, if yes I have to notify the user.

Code to scan the UPnP Devices

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

I have connected multiple devices in the LAN and I am able to see those devices in the UPnP Scan. When I block 1900 port then devFinder.FindByType("upnp:rootdevice", 0); returns 0 devices. So I need to write some port scanning code which will tell me whether the port is open or close. As UPnP use UDP I am not getting any exception when I am trying to connect to the "239.255.255.250:1900" address. Below is the code snip which I have written

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

I have blocked the 1900 port 开发者_开发问答in firewall, so I was expecting the SocketException, but I never gets any exception, so I was not able to find whether port is open or blocked

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜