|
read_millis - how long a TCP read may wait for data, in milliseconds (starts out as 10000) accept_millis - how long AcceptTCPStream waits for a connection, in milliseconds (starts out as 0) |
|
Sets the timeouts used by all TCP streams and servers. read_millis is the safety net under the read commands: a read that cannot get its data within this time gives up. The default is 10000 (ten seconds). When a read times out the stream is marked as failed - Eof returns -1 and the stream cannot be used again - so a timeout is a death sentence for that connection, not a retry. The way to avoid ever hitting it in a game loop is to check ReadAvail and only read data that has already arrived. accept_millis controls AcceptTCPStream: with the default 0 it returns immediately when no one is waiting, which suits polling in a main loop. A dedicated server loop can set a small wait instead of spinning. The settings are global - they apply to every TCP stream, current and future, not per stream. See also: AcceptTCPStream, OpenTCPStream, ReadAvail, Eof. |
; TCPTimeouts Example
; -------------------
; TCPTimeouts controls two waiting times:
; read_millis - how long a read may wait for data before
; erroring (default 10000 ms)
; accept_millis - how long AcceptTCPStream waits for a new
; connection (default 0 = return immediately)
port=4006
server=CreateTCPServer(port)
If server=0 Then
Print "Could not listen on port "+port+" (it may be in use)"
Else
; With the default accept timeout, Accept returns at once
TCPTimeouts 10000,0
start=MilliSecs()
stream=AcceptTCPStream(server)
Print "TCPTimeouts 10000,0 : AcceptTCPStream returned "+stream+" after "+(MilliSecs()-start)+" ms"
; With a 2 second accept timeout, Accept WAITS for a caller
; (nobody connects here, so it gives up after ~2000 ms)
TCPTimeouts 10000,2000
Print ""
Print "Now waiting up to 2 seconds for a connection..."
start=MilliSecs()
stream=AcceptTCPStream(server)
Print "TCPTimeouts 10000,2000 : AcceptTCPStream returned "+stream+" after "+(MilliSecs()-start)+" ms"
; Restore the defaults for any code that follows
TCPTimeouts 10000,0
CloseTCPServer server
EndIf
Print ""
Print "Press any key to close the example"
WaitKey
End