Check if port is blocked by windows firewall

旧城冷巷雨未停 提交于 2019-12-19 04:15:14

问题


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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/7062647/check-if-port-is-blocked-by-windows-firewall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!