; UDPMsgIP 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(5405) If host=0 Then Print "Could not bind port 5405 (it may be in use)" Else player=CreateUDPStream() If player=0 Then Print "Player: could not create a UDP stream" Else WriteLine player,"Anyone there?" SendUDPMsg player,loopback,5405 Print "Player: sent a packet from port "+UDPStreamPort(player) UDPTimeouts 500 If RecvUDPMsg(host)=0 Then Print "Host: no packet arrived" Else Print "Host: message = '"+ReadLine$(host)+"'" ; UDPMsgIP returns the sender's IP address, so a game ; server knows who each packet came from sender_ip=UDPMsgIP(host) sender_port=UDPMsgPort(host) Print "Host: UDPMsgIP says it came from "+DottedIP$(sender_ip) ; ...and gives us everything needed to reply WriteLine host,"Welcome aboard!" SendUDPMsg host,sender_ip,sender_port If RecvUDPMsg(player)<>0 Then Print "Player: reply = '"+ReadLine$(player)+"'" EndIf EndIf CloseUDPStream player EndIf CloseUDPStream host EndIf Print "" Print "Press any key to close the example" WaitKey End