; CreateUDPStream Example ; ----------------------- ; This program is both ends of a UDP conversation at once: two ; streams on this machine swap a packet over the loopback address. ; The loopback address 127.0.0.1 as a packed integer loopback=(127 Shl 24) Or 1 ; CreateUDPStream binds a UDP stream to a local port and returns ; a handle, or 0 on failure (for example if the port is in use) host=CreateUDPStream(5401) If host=0 Then Print "Could not bind port 5401 (it may be in use)" Else Print "Host: CreateUDPStream(5401) is listening on port "+UDPStreamPort(host) ; With no port given, CreateUDPStream picks any free port player=CreateUDPStream() If player=0 Then Print "Player: could not create a UDP stream" Else Print "Player: CreateUDPStream() was given free port "+UDPStreamPort(player) ; Prove the streams work: write a line, then send it as one packet WriteLine player,"Hello from the player" SendUDPMsg player,loopback,5401 Print "Player: sent a packet to "+DottedIP$(loopback)+":5401" ; Wait up to half a second for the packet to arrive UDPTimeouts 500 from=RecvUDPMsg(host) If from=0 Then Print "Host: no packet arrived" Else Print "Host: received '"+ReadLine$(host)+"' from "+DottedIP$(from) EndIf CloseUDPStream player EndIf CloseUDPStream host Print "Both streams closed" EndIf Print "" Print "Press any key to close the example" WaitKey End