User Tools

Site Tools


Sidebar

Network and communication

k24:k24.1:k24.1.4:start

24.1.4 DNSClient

The class DNSClient (gb.net) implements a simple DNS client (resolver) for determining the host name to a given IP address (reverse lookup) and for determining the IP address to a given host name (forward lookup). Host names are said to be resolved into IP addresses and vice versa. The DNS client directs the queries to a DNS server that is either entered in the TCP/IP settings for your system or can be requested via DHCP.

The DNS client can work in asynchronous or synchronous mode. You can set the mode via the 'Async' property. In synchronous mode, the programme will not react to events during the resolution of host names and IP addresses. The default is synchronous mode.

For an interesting overview of DNS (Domain Name System), see the website http://de.wikipedia.org/wiki/Domain_Name_System.

24.1.4.1 Properties

The DNSClient class has these properties:

PropertyDataTypeDefaultDescription
AsyncBooleanFalseDetermines or sets the asynchronous mode of the DNS client. If this property is set to TRUE, the DNS client will operate in asynchronous mode so that the programme is not stopped during the resolution process. The status property then has the value Net.Active and the 'Finished' event is triggered after the resolution process has finished.
HostIPString-This property can be used in two ways: Read the IP address after which the host name was resolved or specify the IP address from which you want to determine the host name.
HostNameString-This property can be used in two ways: Read the host name by which the IP address was resolved or specify the host name from which you want to determine the IP address.
StatusInteger0This property reflects the current status of the DNS client: Net.Active - it is trying to resolve host names to IP address or vice versa and Net.Inactive (DNS client is inactive).

Table 24.1.4.1.1 : Properties of the DNSClient class

24.1.4.2 Methods

The DNSClient class has these three methods:

MethodDescription
GetHostIP()It tries to get (resolve) the IP address to the specified host name - stored in the property 'HostName'. The result of the resolution is stored in the property 'HostIP'. The property 'HostIP' has the value NULL if the resolution failed.
GetHostName()Attempts to determine (resolve) the host name for the specified IP address - stored in the 'HostIP' property. The result of the resolution is stored in the property 'HostName'. If the resolution failed, then property 'HostName' has the value NULL.
Stop()Cancels only an asynchronous request. If the DNS client is working in synchronous mode or is inactive, then the 'Stop()' method does nothing.

Table 24.1.4.2.1 : Methods of the class DNSClient

24.1.4.3 Event

The DNSClient class has only the Finished property. This event is triggered when an (asynchronous) resolution request has finished. Within the event handling routine, you can read the values stored in the HostIP and HostName properties and process them further in your programme.

24.1.4.4 Project

DNS
The DNS client from the Gambas sample collection has been adapted to use all properties, methods and the 'Finished' event. You can find the adapted project as project source archive in the download area. The next pictures should give you an impression of working with the DNS client:

1
Figure 24.1.4.4.1: Programme interface

2
Figure 24.1.4.4.2: Resolution Host Name → IP Address

3
Figure 24.1.4.4.3: Resolution IP address → host name

4
Figure 24.1.4.4.4: Resolution host name → IP address

5
Figure 24.1.4.4.5: Failed to resolve gambas-book.net.

6
Figure 24.1.4.4.6: Failed to resolve IP address 46.30.56.205

Comments:

  • Figure 24.1.4.4.1:\ After starting the programme, you can enter three host names for three DNS clients. The asynchronous mode is set.
  • Figure 24.1.4.4.2:\ With Start you trigger the resolution process (host name → IP address) for all three DNS clients and receive the displayed IP addresses.
  • Figure 24.1.4.4.3:\ With the double arrow button you take over the 3 IP addresses or enter IP addresses there yourself and can resolve them into the 3 host names with 'Start' - if this is possible.
  • Figure 24.1.4.4:
    The resolution also succeeds for local host names.
  • Figure 24.1.4.4.5:\ The resolution for the host name gambas-book.net does not succeed because this host name is obviously not known to any DNS server. In this case, the return value NULL is acknowledged with the constant Private Const NULL_NOTFOUND As String = “Not found”.
  • Figure 24.1.4.4.6:
    The resolution for the IP address 46.30.56.205 returned NULL. Problems with resolving IP addresses or host names include firewalls.

24.1.4.5 Excursus

For the use of the EXEC and SHELL instructions, you can successfully use the presented commands to determine the host name to a given IP address or the IP address to a given host name:

hans@linux:~$ nslookup gambas-buch.de
Server:		127.0.0.1
Address:	127.0.0.1#53
Non-authoritative answer:
Name:	gambas-buch.de
Address: 109.237.140.40

hans@linux:~$ nslookup 109.237.140.40
Server:		127.0.0.1
Address:	127.0.0.1#53
Non-authoritative answer:
40.140.237.109.in-addr.arpa	name = alfa3035.alfahosting-server.de.

hans@linux:~$ resolveip -s gambas-buch.de
109.237.140.40
hans@linux:~$ dig +short gambas-buch.de
109.237.140.40

hans@linux:~$ host gambas-buch.de
gambas-buch.de has address 109.237.140.40

hans@linux:~$ ping -a -c 1 gambas-buch.de
PING gambas-buch.de (109.237.140.40) 56(84) bytes of data.
64 bytes from alfa3035.alfahosting-server.de (109.237.140.40): icmp_req=1 ttl=58 time=59.3 ms

Often it is of interest which IP address has been assigned to you by the provider. In this case, you will reach your goal with one of the four instructions:

hans@linux:~$ curl -s http://checkip.dyndns.org/ | grep -o "[[:digit:].]\+"
hans@linux:~$ wget -qO - http://cfaj.freeshell.org/ipaddr.cgi
hans@linux:~$ curl "http://www.networksecuritytoolkit.org/nst/cgi-bin/ip.cgi"
hans@linux:~$ wget -qO - www.meineip.de 2>&1 | grep 'title="Meine IP:' | sed -e 's/.*"ip">//' -e 's/<.p>.*//'

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k24/k24.1/k24.1.4/start.txt · Last modified: 16.08.2022 (external edit)

Page Tools