HOST ADDR statement

Purpose | Translate a host name into a corresponding IP address.
Syntax | HOST ADDR [hostname$] TO ip& HOST ADDR(index&) TO ip&

Remarks

hostname$ is the name of a computer on the network or a domain name such as "powerbasic.com". If hostname$ is zero-length or not specified, the primary IP address of the current computer is returned.

ip& receives the IP address of the specified host name. ip& may be a REGISTER or memory variable.

It is possible for a computer to have more than one IP address. For example, if you have a network card in your computer, and you are dialed into the Internet using a modem, your computer will have two IP addresses. By using the indexed form of the statement:

HOST ADDR(index&) TO ip&

you can retrieve the first IP address with index& = 1, the second with index& = 2, etc. If, on return, ip& contains zero (0), there are no further IP addresses to retrieve on that computer.

A numeric IP address can be easily converted to a dotted IP address string with the following code:

DIM p AS BYTE PTR

HOST ADDR "localhost" TO ip&

p = VARPTR(ip&)

a$ = USING$("#_.#_.#_.#", @p, @p[1], @p[2], @p[3])

' returns "127.0.0.1"

Restrictions | In order to obtain the IP address of the current computer, you must have at least one socket open, or you must first obtain the name of the computer by using the HOST NAME statement.
See also | HOST NAME, TCP and UDP Communications, TCP OPEN, UDP OPEN

Example

HOST ADDR "powerbasic.com" TO ip& ' Primary IP

FUNCTION HowManyIPs() AS LONG

DIM p AS BYTE PTR

RESET index&

DO

HOST ADDR(index&+1) TO ip&

IF ISTRUE ip& THEN

INCR index&

p = VARPTR(ip&)

a$ = USING$("#_.#_.#_.#", @p, @p[1], @p[2], @p[3])

END IF

LOOP UNTIL ip& = 0

FUNCTION = index&

END FUNCTION