Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Get Public IP address (checkip.dyndns.com)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    PBWin/PBCC Get Public IP address (checkip.dyndns.com)

    Code:
    FUNCTION GetPublicIPAddress(BYVAL PortNumber AS LONG) AS STRING
     
       'Return dotted public ip address from checkip.dyndns.com
       'Pass 80 to lookup on primary port or 8245 for secondary
     
       LOCAL buffer, temp, site AS STRING
       LOCAL StartByte,LastByte,nSocket AS LONG
       Site  = "checkip.dyndns.com"
     
       nSocket = FREEFILE
       TCP OPEN PORT PortNumber AT site AS nSocket TIMEOUT 5000
       IF ERR THEN EXIT FUNCTION
     
       TCP PRINT nSocket, "GET " & " HTTP/1.0"
       TCP PRINT nSocket, "Accept: */*"
       TCP PRINT nSocket, "Accept-Language: en-us"
       TCP PRINT nSocket, "Host: " & site
       TCP PRINT nSocket, "Pragma: no-cache"
       TCP PRINT nSocket, "Referer: URL=http://www.MySite.com"
       TCP PRINT nSocket, "User-Agent: MySite.com"
       TCP PRINT nSocket, ""
     
       DO
          TCP RECV nSocket, 4096, buffer
          temp = temp & buffer
       LOOP UNTIL LEN(Buffer) = 0 OR ERRCLEAR
       TCP CLOSE nSocket
     
       'Hard-coded to only work with checkip.dyndns.com!
       StartByte = INSTR(UCASE$(temp),"<BODY>CURRENT IP ADDRESS: ")
       IF StartByte THEN
         StartByte = StartByte + 26
         LastByte =  INSTR(StartByte,UCASE$(temp),"</BODY>")
         FUNCTION = MID$(temp,StartByte,LastByte-StartByte)
       END IF
     
    END FUNCTION

    #2
    This one also checks myglobalip.com

    Code:
    %DisplayPage = 1
     
    FUNCTION PBMAIN AS LONG 'GetPublicIpAddress.com
     
      ? GetPublicIpAddress("checkip.dyndns.com"  ,""     ,80)
      ? GetPublicIpAddress("myglobalip.com"      ,"/myip",80)
      ? GetPublicIpAddress("checkip.dyndns.com"  ,""     ,8245)
     
    END FUNCTION
     
    FUNCTION GetPublicIPAddress(Site AS STRING, file AS STRING, BYVAL PortNumber AS LONG) AS STRING
      LOCAL buffer, temp, StartSearchFor,EndSearchFor AS STRING
      LOCAL StartByte,LastByte,nSocket AS LONG
      nSocket = FREEFILE
      TCP OPEN PORT PortNumber AT site AS nSocket TIMEOUT 5000
      IF ERR THEN ? "Unable to connect to " + site:EXIT FUNCTION
     
      TCP PRINT nSocket, "GET " & file &  " HTTP/1.0"
      TCP PRINT nSocket, "Accept: */*"
      TCP PRINT nSocket, "Accept-Language: en-us"
      TCP PRINT nSocket, "Host: " & site
      TCP PRINT nSocket, "Pragma: no-cache"
      TCP PRINT nSocket, "Referer: URL=http://www.MySite.com"
      TCP PRINT nSocket, "User-Agent: MySite.com"
      TCP PRINT nSocket, ""
      DO
        TCP RECV nSocket, 4096, buffer
        temp = temp & buffer
      LOOP UNTIL LEN(Buffer) = 0 OR ERRCLEAR
      TCP CLOSE nSocket
      #IF %DisplayPage = 0
        ? Temp
      #ENDIF
     
      SELECT CASE Site
     
    CASE "checkip.dyndns.com"
          StartSearchFor = "<BODY>CURRENT IP ADDRESS: "
          EndSearchFor   = "</BODY"
          StartByte = INSTR(UCASE$(temp),StartSearchFor)
          IF StartByte THEN
            StartByte = StartByte + LEN(StartSearchFor)
            LastByte =  INSTR(StartByte,UCASE$(temp),EndSearchFor)
            FUNCTION = MID$(temp,StartByte,LastByte-StartByte)
          END IF
     
        CASE  "myglobalip.com"
           StartByte = INSTR(-1,Temp,$LF) +1
           FUNCTION = MID$(temp,StartByte)
     
        CASE ELSE: ? "Invalid site entered"
     
       END SELECT
     
     
    END FUNCTION

    Comment

    Working...
    X
    😀
    🥰
    🤢
    😎
    😡
    👍
    👎