how to use vb.net to check if my winform application is block by firewall TCP/port

Hi
I want winform application to be aware of firewall that would block TCP and Port it is using.

Is there such code? to check if firewall is blocking?
JSW21Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
HairbrushConnect With a Mentor Commented:
Hi

Yes, it is possible to do this.  You will need to use the System.Net.Sockets namespace in the .NET Framework.  Try the code below:
Dim hostname As String = "server1"
Dim portno As Integer = 9081
Dim ipa As IPAddress = CType(Dns.GetHostAddresses(hostname)(0), IPAddress)
Try
	Dim sock As System.Net.Sockets.Socket = New System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp)
	sock.Connect(ipa, portno)
	If sock.Connected = True Then ' Port is in use and connection is successful
			MessageBox.Show("Port is Closed")
	End If
	sock.Close()

Catch ex As System.Net.Sockets.SocketException
	If ex.ErrorCode = 10061 Then ' Port is unused and could not establish connection
		MessageBox.Show("Port is Open!")
	Else
		MessageBox.Show(ex.Message)
	End If
End Try

Open in new window

0
 
JSW21Author Commented:
sock.close
will allow me to really connect it again later right?
0
 
HairbrushCommented:
Yes.
0
All Courses

From novice to tech pro — start learning today.