Blitz3D+ Command Reference

HostIP ( host_index )

Parameters

host_index - which address to read, from 1 up to the count returned by CountHostIPs

Description

Returns one of the IP addresses found by the most recent CountHostIPs lookup.

CountHostIPs does the actual name lookup and stashes what it finds; HostIP reads the stash. Indexes start at 1, so the usual shape is a For loop from 1 to the count - or just HostIP(1) when any address of the server will do.

The value is a packed integer, the common currency of the networking commands: pass it straight to SendUDPMsg as a destination, or through DottedIP to show the player which server they are about to join.

Two rules keep it honest. Call CountHostIPs first - HostIP only reads that command's results, and each new lookup replaces the previous list. And stay inside 1..count: in debug mode an out-of-range index stops the program with "Host index out of range", so guard the count (especially the 0 of a failed lookup) before reading.

See also: CountHostIPs, DottedIP, SendUDPMsg, OpenTCPStream.

Example

; HostIP Example
; --------------

; HostIP reads the results of the most recent CountHostIPs
; lookup, so that call always comes first - "localhost"
; resolves even with no internet connection.
count=CountHostIPs("localhost")
Print "CountHostIPs('localhost') found "+count+" address(es)"

; Indexes run from 1 to the count returned by CountHostIPs
For i=1 To count
    ip=HostIP(i)

    ; HostIP returns the address as a packed integer -
    ; DottedIP turns it into the familiar dotted form
    Print "HostIP("+i+") = "+ip+"  ->  "+DottedIP$(ip)
Next

If count>0 Then
    Print ""
    Print "A game would now connect to "+DottedIP$(HostIP(1))
    Print "(OpenTCPStream and SendUDPMsg both accept these values)"
EndIf

Print ""
Print "Press any key to close the example"
WaitKey

End

Index