View source code Display the source code in std/socket.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

std.socket.socket.set_option - multiple declarations

Function Socket.setOption

Set the linger option.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  Linger value
) @trusted;

Function Socket.setOption

Set a socket option.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  void[] value
) @trusted;

Function Socket.setOption

Common case for setting integer and boolean options.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  int value
) @trusted;

Function Socket.setOption

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.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  Duration value
) @trusted;

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.

Parameters

NameDescription
level The level at which a socket option is defined.
option Either SocketOption.SNDTIMEO or SocketOption.RCVTIMEO.
value The timeout duration to set. Must not be negative.

Throws

SocketException if setting the options fails.

Example

import std.datetime;
auto pair = socketPair();
scope(exit) foreach (s; pair) s.close();

// Set a receive timeout, and then wait at one end of
// the socket pair, knowing that no data will arrive.
pair[0].setOption(SocketOptionLevel.SOCKET,
    SocketOption.RCVTIMEO, dur!"seconds"(1));

auto sw = StopWatch(AutoStart.yes);
ubyte[1] buffer;
pair[0].receive(buffer);
writefln("Waited %s ms until the socket timed out.",
    sw.peek.msecs);

Function Socket.setOption

Set the linger option.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  Linger value
) @trusted;

Function Socket.setOption

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.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  Duration value
) @trusted;

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.

Parameters

NameDescription
level The level at which a socket option is defined.
option Either SocketOption.SNDTIMEO or SocketOption.RCVTIMEO.
value The timeout duration to set. Must not be negative.

Throws

SocketException if setting the options fails.

Example

import std.datetime;
auto pair = socketPair();
scope(exit) foreach (s; pair) s.close();

// Set a receive timeout, and then wait at one end of
// the socket pair, knowing that no data will arrive.
pair[0].setOption(SocketOptionLevel.SOCKET,
    SocketOption.RCVTIMEO, dur!"seconds"(1));

auto sw = StopWatch(AutoStart.yes);
ubyte[1] buffer;
pair[0].receive(buffer);
writefln("Waited %s ms until the socket timed out.",
    sw.peek.msecs);

Function Socket.setOption

Set a socket option.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  void[] value
) @trusted;

Function Socket.setOption

Common case for setting integer and boolean options.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  int value
) @trusted;

Function Socket.setOption

Set the linger option.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  Linger value
) @trusted;

Function Socket.setOption

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.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  Duration value
) @trusted;

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.

Parameters

NameDescription
level The level at which a socket option is defined.
option Either SocketOption.SNDTIMEO or SocketOption.RCVTIMEO.
value The timeout duration to set. Must not be negative.

Throws

SocketException if setting the options fails.

Example

import std.datetime;
auto pair = socketPair();
scope(exit) foreach (s; pair) s.close();

// Set a receive timeout, and then wait at one end of
// the socket pair, knowing that no data will arrive.
pair[0].setOption(SocketOptionLevel.SOCKET,
    SocketOption.RCVTIMEO, dur!"seconds"(1));

auto sw = StopWatch(AutoStart.yes);
ubyte[1] buffer;
pair[0].receive(buffer);
writefln("Waited %s ms until the socket timed out.",
    sw.peek.msecs);

Function Socket.setOption

Set a socket option.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  void[] value
) @trusted;

Function Socket.setOption

Common case for setting integer and boolean options.

Prototype

void setOption(
  SocketOptionLevel level,
  SocketOption option,
  int value
) @trusted;

Authors

Christopher E. Miller, David Nadlinger, Vladimir Panteleev

License

Boost License 1.0.

Comments