; SendUDPMsg 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(5403) If host=0 Then Print "Could not bind port 5403 (it may be in use)" Else player=CreateUDPStream() If player=0 Then Print "Player: could not create a UDP stream" Else ; Everything written to the stream is held in a buffer... WriteLine player,"Two lines of chat" WriteLine player,"travelling together" Print "Player: wrote two lines - nothing has been sent yet" ; ...until SendUDPMsg fires the whole buffer as ONE packet. ; Always give dest_port - see the SendUDPMsg help page. SendUDPMsg player,loopback,5403 Print "Player: SendUDPMsg fired them as one packet to "+DottedIP$(loopback)+":5403" ; The host receives a single packet holding both lines UDPTimeouts 500 If RecvUDPMsg(host)=0 Then Print "Host: no packet arrived" Else Print "Host: one packet arrived ("+ReadAvail(host)+" bytes)" Print "Host: line 1 = '"+ReadLine$(host)+"'" Print "Host: line 2 = '"+ReadLine$(host)+"'" EndIf ; A second send makes a second, separate packet WriteLine player,"Fresh packet" SendUDPMsg player,loopback,5403 If RecvUDPMsg(host)<>0 Then Print "Host: next packet = '"+ReadLine$(host)+"'" EndIf CloseUDPStream player EndIf CloseUDPStream host EndIf Print "" Print "Press any key to close the example" WaitKey End