How do I detect what application, if any, is listening on a given TCP port on Windows? Here's xampp doing this:
I'd prefer to do this in VB.NET, and I want to offer the user the option to close that app.
Dim hostname As String = "server1"
Dim portno As Integer = 9081
Dim ipa As IPAddress = DirectCast(Dns.GetHostAddresses(hostname)(0), IPAddress)
Try
Dim sock As 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
this helped me :)