Visual Basic 6

Posted On 2012-10-13 by dwirch
Keywords: VB6 Is the port in use networking check
Tags: Visual Basic 6 Networking Tutorial  
Views: 686
Rating: / 5.00

  • 1
  • 2
  • 3
  • 4
  • 5


Working with network objects in Visual Basic can be a little daunting for newer programmers. It's not that scary, really.

This bit of code (given here by request) will determine if a particular port is in use on the local machine. It leans on the use of Windows Sockets (winsock) to perform the check.

Private Function PortInUse(ByVal PortNumber As Integer) As _
'PURPOSE: Determine if a TCP/IP port is in use
'If PortInUse(21) Then
'MsgBox "The standard FTP port is in use on this machine"
'end if
Dim oSocket As Object
Dim bAns As Boolean

On Error Resume Next
Set oSocket = CreateObject("MSWinsock.Winsock.1")

If Err.Number > 0 Then
Err.Raise 30000, , "Could not create winsock object"
Exit Function
End If


oSocket.LocalPort = PortNumber

'if we get this error, it means
'port is busy
bAns = Err.Number = 10048
Set oSocket = Nothing
PortInUse = bAns

End Function

About the Author

dwirch has posted a total of 155 articles.

You can find more information from dwirch by visiting

Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.