KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Resolving a host name into an IP address
PRODUCT: 4D | VERSION: | PLATFORM: Mac & Win
Published On: September 29, 2000

Here's a method that uses the 4D Internet Commands plug-in to convert a host name into an IP address:

Copy and Paste the following code into your own 4D project:
` Project Method: TCP_HostResolveF (hostName) -> IP Address
` Given a host name or IP address, returns the IP Address.
` If not possible, then returns back the host name.
` By Tim Tonooka, 4D, Inc.
` September 21, 2000
C_BOOLEAN($isValidIPAddress)
C_LONGINT($i;$nCharCode;$nError;$nLength;$nPosColon)
C_TEXT($0;$1;$tDotTest;$tHostName;$tIPAddress)

$tHostName:=$1

 ` If the host name includes a port ID, remove that:
$nPosColon:=Position(Char(58);$tHostName)
If ($nPosColon>0)
 $tHostName:=Substring($tHostName;1;$nPosColon-1)
End if

 ` Determine if the address supplied is a host name or IP address:
$isValidIPAddress:=True
$nLength:=Length($tHostName)
For ($i;1;$nLength)
 $nCharCode:=Ascii($tHostName[[$i]])
 Case of
  : ($nCharCode<46)
  $isValidIPAddress:=False
  $i:=$nLength
  : ($nCharCode>57)
  $isValidIPAddress:=False
  $i:=$nLength
  : ($nCharCode=47)
  $isValidIPAddress:=False
  $i:=$nLength
 End case
End for

If ($isValidIPAddress)
 ` Test to make sure the address contains exactly three periods:
 $tDotTest:=Replace string($tHostName;Char(46);"")
 $isValidIPAddress:=(($nLength-Length($tDotTest))=3)
End if

If ($isValidIPAddress) ` It's already a valid IP address
$tIPAddress:=$tHostName

Else
 $nError:=NET_Resolve ($tHostName;$tIPAddress)
 If ($nError#0)
  $tIPAdd<> ress:=$tHostName
 End if
End if
$0:=$tIPAddress