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

发布于 2024-11-10 09:05:02 字数 1110 浏览 3 评论 0原文

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

扫描 UPnP 设备的代码

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

我已在 LAN 中连接了多个设备,并且我能够在 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,但我从未得到任何异常,所以我无法找到端口是打开还是被阻止

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文