Solved

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

Posted on 2011-03-25
3
1,113 Views
Last Modified: 2012-06-21
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?
0
Comment
Question by:JSW21
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
Hairbrush earned 500 total points
ID: 35213960
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
 

Author Closing Comment

by:JSW21
ID: 35217934
sock.close
will allow me to really connect it again later right?
0
 
LVL 12

Expert Comment

by:Hairbrush
ID: 35230399
Yes.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now