The level at which a socket option is defined.
Either SocketOption.SNDTIMEO or SocketOption.RCVTIMEO.
The timeout duration to set. Must not be negative.
SocketException if setting the options fails.
1 import std.datetime; 2 import std.typecons; 3 auto pair = socketPair(); 4 scope(exit) foreach (s; pair) s.close(); 5 6 // Set a receive timeout, and then wait at one end of 7 // the socket pair, knowing that no data will arrive. 8 pair[0].setOption(SocketOptionLevel.SOCKET, 9 SocketOption.RCVTIMEO, dur!"seconds"(1)); 10 11 auto sw = StopWatch(Yes.autoStart); 12 ubyte[1] buffer; 13 pair[0].receive(buffer); 14 writefln("Waited %s ms until the socket timed out.", 15 sw.peek.msecs);
Sets a timeout (duration) option, i.e. SocketOption.SNDTIMEO or RCVTIMEO. Zero indicates no timeout.
In a typical application, you might also want to consider using a non-blocking socket instead of setting a timeout on a blocking one.
Note: While the receive timeout setting is generally quite accurate on *nix systems even for smaller durations, there are two issues to be aware of on Windows: First, although undocumented, the effective timeout duration seems to be the one set on the socket plus half a second. setOption() tries to compensate for that, but still, timeouts under 500ms are not possible on Windows. Second, be aware that the actual amount of time spent until a blocking call returns randomly varies on the order of 10ms.