PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Get local IP address  (Read 43 times)

Josť Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3229
Get local IP address
« on: February 24, 2020, 02:45:33 PM »

Petrus Vorster posted a question that he has deleted while I was doing some research. It was about retrieving the local IP address of a computer.

In FreeBasic, you can do this:

Code: [Select]
DIM wsad AS WSAData
IF WSAStartup(&h202, @wsad) = 0 THEN
   DIM hostname AS ZSTRING * 256
   IF gethostname(hostname, SIZEOF(hostname)) = 0 THEN
      DIM pHostent AS HOSTENT PTR = gethostbyname(hostname)
      IF pHostent THEN
         DIM IPbuffer AS ZSTRING PTR = inet_ntoa(*CAST(IN_ADDR PTR, pHostent->h_addr_list[0]))
         print "Local IP host address: "; *IPbuffer
      END IF
   END IF
   WSACleanUp
END IF
« Last Edit: February 24, 2020, 02:50:46 PM by Josť Roca »
Logged

Pierre Bellisle

  • Junior Member
  • **
  • Posts: 91
Re: Get local IP address
« Reply #1 on: Today at 02:33:58 AM »

Hola Josť,

Also, this might be of some value, since GetHostByName is deprecated and MS insist on using GetAddrInfo...

Code: [Select]
#Define JumpCompiler "<D:\Free\64\fbc.exe>"
#Define JumpCompilerCmd "<-s gui -w pedantic "D:\Free\bas\~~Default.rc">"

#define UniCode
#Include Once "windows.bi"
#Include Once "win\winsock2.bi"
#Include Once "win\ws2tcpip.bi"
'_____________________________________________________________________________

 DIM wsad AS WSAData
 IF WSAStartup(&h202, @wsad) = ERROR_SUCCESS THEN
   DIM zHostName AS ZSTRING * 256
   IF gethostname(zHostName, SIZEOF(zHostName)) = ERROR_SUCCESS THEN

     'zHostName = "localhost"     'Will return 127.0.0.1
     'zHostName = "microsoft.com" 'In a loop, will return 40.113.200.201, 104.215.148.63, 13.77.161.179, 40.76.4.15, 40.112.72.205

     DIM pAddressInfoList AS ADDRINFOW POINTER
     DIM AddressInfoHint  AS ADDRINFOW
     AddressInfoHint.ai_family   = AF_INET
     AddressInfoHint.ai_socktype = SOCK_STREAM
     AddressInfoHint.ai_protocol = IPPROTO_TCP
     IF GetAddrInfo(zHostName, "http", @AddressInfoHint, @pAddressInfoList) = ERROR_SUCCESS THEN
       DO 'REM to get only the first assigned ip
         DIM pSockAddrIn AS PSOCKADDR_IN = Cast(PSOCKADDR_IN, pAddressInfoList->ai_addr)
         DIM IPbuffer AS ZSTRING PTR = inet_ntoa(*CAST(IN_ADDR POINTER, @pSockAddrIn->sin_addr.s_addr))
         MessageBox(HWND_DESKTOP, "Local IP host address: " & *IPbuffer, zHostName, MB_OK OR MB_TOPMOST)
         IF pAddressInfoList->ai_next = 0 THEN EXIT DO 'REM to get only the first assigned ip
         pAddressInfoList = Cast(ADDRINFOW POINTER, pAddressInfoList->ai_next) 'REM to get only the first assigned ip
       LOOP 'REM to get only the first assigned ip
       FreeAddrInfo(pAddressInfoList) 'Free address list memory
     END IF

     'MS: GetHostByName is deprecated. Developers creating Windows Sockets 2 applications
     '    are urged to use the GetAddrInfo function instead of GetHostByName.
     'DIM pHostent AS HOSTENT PTR = GetHostByName(zHostName)
     'IF pHostent THEN
     '  DIM IPbuffer AS ZSTRING PTR = inet_ntoa(*CAST(IN_ADDR POINTER, pHostent->h_addr_list[0]))
     '  MessageBox(HWND_DESKTOP, "Local IP host address: " & *IPbuffer, zHostName, MB_OK OR MB_TOPMOST)
     'END IF

   END IF
   WSACleanUp()
 END IF
'_____________________________________________________________________________
'
Logged