UDPMsgIP ( udp_stream )
Parameters
| udp_stream - a UDP stream handle returned by CreateUDPStream |
Description
|
Returns the IP address the most recently received UDP packet came from. After RecvUDPMsg delivers a packet, UDPMsgIP tells you which machine sent it - the same packed integer RecvUDPMsg itself returned, kept around so you can ask again while handling the message. Pair it with UDPMsgPort and you have the sender's full return address: pass both straight to SendUDPMsg to answer, which is exactly how a game host welcomes each new player in the example. Because one UDP stream can hear from any number of machines, checking the sender of every packet is normal practice - it is how a server tells its players apart (and how it ignores strangers). DottedIP formats the value for a lobby screen or log. The value only means something after a successful RecvUDPMsg on that stream; before the first packet it just echoes the stream's own bound address (0). It is refreshed by each newly received packet. See also: UDPMsgPort, RecvUDPMsg, SendUDPMsg, DottedIP. |
Example
; 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
Index