检查端口是否被 windows 防火墙阻止 [英] Check if port is blocked by windows firewall

查看:48
本文介绍了检查端口是否被 windows 防火墙阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查传入和传出连接中的特定端口是否被 Windows 防火墙阻止.

How do i check if specific port is blocked by Windows firewall , both in incoming and outgoing connections.

我见过这样的代码示例:

I've seen code samples such as this one:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim host As String = "localhost"

Dim port As Integer = 6900

Dim addr As IPAddress = CType(Dns.GetHostAddresses(host)(0), IPAddress)

Try

Dim tcpList As New TcpListener(addr, port)

tcpList.Start()

Catch sx As SocketException

' Catch exception here if port is blocked

End Try

End Sub

但是我怎么知道异常是否是因为防火墙阻塞而引发的,而不是因为其他套接字错误?

But how can i know if the exception is thrown specifically because of firewall blocking, and not because of other socket errors?

推荐答案

我认为检查 Windows 防火墙是否阻塞端口的正确方法不是测试它,而是使用 Windows 防火墙 API 以进行最终检查.
我从未使用过它,但我认为 INetFwOpenPorts 将是开始的地方.

I'd say that the proper way to check if Windows Firewall is blocking a port isn't to test it, but to use the Windows Firewall API to check conclusively.
I've never used it but I'd assume that INetFwOpenPorts would be the place to start.

这是 VB.Net 代码示例 似乎包含执行此操作的代码.具体来说,我建议查看该示例中的 IsxPLportOpen 方法,该方法使用 GloballyOpenPorts 方法以获取对 INetFwOpenPorts 的引用.

Here's a VB.Net code sample that seems to contain code that does this. Specifically I'd suggest looking at the IsxPLportOpen method in that sample which uses the GloballyOpenPorts method to get a reference to INetFwOpenPorts.

这篇关于检查端口是否被 windows 防火墙阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆