; UDPTimeouts Example ; ------------------- ; This program times RecvUDPMsg on a quiet stream to show what ; UDPTimeouts changes, then delivers a real packet. ; The loopback address 127.0.0.1 as a packed integer loopback=(127 Shl 24) Or 1 host=CreateUDPStream(5409) If host=0 Then Print "Could not bind port 5409 (it may be in use)" Else ; Default timeout is 0: RecvUDPMsg never waits, it just polls started=MilliSecs() r=RecvUDPMsg(host) Print "Timeout 0ms: RecvUDPMsg = "+r+" after "+(MilliSecs()-started)+"ms (instant)" ; UDPTimeouts makes every RecvUDPMsg wait up to that many ; milliseconds for a packet before giving up UDPTimeouts 400 started=MilliSecs() r=RecvUDPMsg(host) Print "Timeout 400ms: RecvUDPMsg = "+r+" after "+(MilliSecs()-started)+"ms (waited)" ; With a packet already waiting, the timeout does not delay it player=CreateUDPStream() If player=0 Then Print "Player: could not create a UDP stream" Else WriteLine player,"No waiting for this one" SendUDPMsg player,loopback,5409 started=MilliSecs() r=RecvUDPMsg(host) If r<>0 Then Print "With data: RecvUDPMsg = sender "+DottedIP$(r)+" after "+(MilliSecs()-started)+"ms" Print "Message: '"+ReadLine$(host)+"'" EndIf CloseUDPStream player EndIf ; Back to non-blocking polling for a typical game loop UDPTimeouts 0 CloseUDPStream host EndIf Print "" Print "Press any key to close the example" WaitKey End