Sample code for 30+ languages & platforms
PureBasic

Resolve Domain to IP Address

See more DNS Examples

Demonstrates how to perform a DNS query to obtain A records for resolving a domain name to an IPv4 address.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkDns.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    dns.i = CkDns::ckCreate()
    If dns.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)

    success = CkDns::ckQuery(dns,"A","microsoft.com",json)
    If success = 0
        Debug CkDns::ckLastErrorText(dns)
        CkDns::ckDispose(dns)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    Debug CkJsonObject::ckEmit(json)

    ; Sample response.
    ; Parsing code below..

    ; {
    ;   "answer": {
    ;     "a": [
    ;       {
    ;         "name": "microsoft.com",
    ;         "ttl": 1528,
    ;         "ipv4": "20.112.250.133"
    ;       },
    ;       {
    ;         "name": "microsoft.com",
    ;         "ttl": 1528,
    ;         "ipv4": "20.231.239.246"
    ;       },
    ;       {
    ;         "name": "microsoft.com",
    ;         "ttl": 1528,
    ;         "ipv4": "20.76.201.171"
    ;       },
    ;       {
    ;         "name": "microsoft.com",
    ;         "ttl": 1528,
    ;         "ipv4": "20.70.246.20"
    ;       },
    ;       {
    ;         "name": "microsoft.com",
    ;         "ttl": 1528,
    ;         "ipv4": "20.236.44.162"
    ;       }
    ;     ]
    ;   }
    ; }

    ; Use this online tool to generate parsing code from sample JSON: 
    ; Generate Parsing Code from JSON

    name.s
    ttl.i
    ipv4.s

    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(json,"answer.a")
    While i < count_i
        CkJsonObject::setCkI(json, i)
        name = CkJsonObject::ckStringOf(json,"answer.a[i].name")
        ttl = CkJsonObject::ckIntOf(json,"answer.a[i].ttl")
        ipv4 = CkJsonObject::ckStringOf(json,"answer.a[i].ipv4")
        i = i + 1
    Wend


    CkDns::ckDispose(dns)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure