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?
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
.