Blitz3D+ Command Reference

UDPStreamIP ( udp_stream )

Parameters

udp_stream - a UDP stream handle returned by CreateUDPStream

Description

Returns the local IP address a UDP stream is bound to.

In practice this returns 0, which DottedIP renders as "0.0.0.0". That is not an error: CreateUDPStream binds the stream to every network interface the machine has (wired, wireless, loopback, ...), and "0.0.0.0" is the address that means "all of them". So don't reach for this command to discover your own IP address - a machine can have several anyway. The useful half of the pair is UDPStreamPort, which reports a real, concrete port number.

If you want addresses worth showing, look elsewhere: UDPMsgIP gives the remote address each received packet came from, and CountHostIPs/HostIP look up the addresses of a named machine. The example prints the honest 0.0.0.0 next to a real sender address so the difference is obvious.

See also: UDPStreamPort, UDPMsgIP, CreateUDPStream, DottedIP.

Example

; UDPStreamIP Example
; -------------------

; This program is both ends of a UDP conversation at once: two
; streams on this machine swap packets over the loopback address.

; The loopback address 127.0.0.1 as a packed integer
loopback=(127 Shl 24) Or 1

host=CreateUDPStream(5407)
If host=0 Then
    Print "Could not bind port 5407 (it may be in use)"
Else
    ; UDPStreamIP returns the address the stream is bound to.
    ; CreateUDPStream listens on ALL of the machine's interfaces,
    ; so this reports 0 - which DottedIP renders as 0.0.0.0
    Print "Host: UDPStreamIP = "+UDPStreamIP(host)+" ("+DottedIP$(UDPStreamIP(host))+")"
    Print "Host: UDPStreamPort = "+UDPStreamPort(host)
    Print "(0.0.0.0 means 'every local interface', not an error)"

    player=CreateUDPStream()
    If player=0 Then
        Print "Player: could not create a UDP stream"
    Else
        WriteLine player,"Where are you?"
        SendUDPMsg player,loopback,5407

        UDPTimeouts 500
        If RecvUDPMsg(host)=0 Then
            Print "Host: no packet arrived"
        Else
            Print "Host: message = '"+ReadLine$(host)+"'"

            ; Contrast: UDPMsgIP gives the REMOTE address a packet
            ; came from - here the loopback address itself
            Print "Host: the packet came from "+DottedIP$(UDPMsgIP(host))
        EndIf

        CloseUDPStream player
    EndIf
    CloseUDPStream host
EndIf

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

End

Index