How to Get Your Own IP Address Programmatically

The following function shows how to obtain your machine's host name (IP address or domain name) programmatically, using the Windows Sockets APIs, and use it for initializing the server side.

Function DefaultConnection() As String
   Dim WSAD As WSADATA
   Dim iReturn As Integer
   Dim sLowByte As String, sHighByte As String, sMsg As String
   Dim hostname As String * 256
   Dim hostent_addr As Long
   Dim host As HOSTENT
   Dim hostip_addr As Long
   Dim temp_ip_address() As Byte
   Dim i As Integer
   Dim ip_address As String

   iReturn = WSAStartup(WS_VERSION_REQD, WSAD)

   If iReturn <> 0 Then
      DefaultConnection = ""
   End If

   If gethostname(hostname, 256) = NO_ERROR Then
      hostname = Trim$(hostname)
      hostent_addr = gethostbyname(hostname)

      RtlMoveMemory host, hostent_addr, LenB(host)
      RtlMoveMemory hostip_addr, host.hAddrList, 4
      ReDim temp_ip_address(1 To host.hLength)
      RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength

      For i = 1 To host.hLength
          ip_address = ip_address & temp_ip_address(i) & "."
      Next
      ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
   End If
  
   WSACleanup
  
   DefaultConnection = ip_address
End Function

 

If there are multiple names (IP addresses), you can force any one you want.