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,126 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 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

13 Experts available now in Live!

Get 1:1 Help Now