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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What namespace do I need to import? 2 28
Footer for each row on Gridview 2 21
Please explain the parts of these 2 LINQ expressions 3 29
JSON  parse help 7 24
Network ports are the threads that hold network communication together. They are an essential part of networking that can be easily ignore or misunderstood, my goals is to show those who don't have a strong network foundation how network ports opera…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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