Socket.SetOptions

.NET System.Net.Sockets Namespace

Method Summary

public void SetOptions(SocketOptionLevel level, SocketOptionName option, int optionValue)
public void SetOptions(SocketOptionLevel level, SocketOptionName option, object optionValue)

Configures the behavior of the current Socket instance by setting a socket option.

Parameters

Name Type Description
level SocketOptionLevel The scope of the socket option. This can be SocketOptionLevel.Socket, SocketOptionLevel.IP, or SocketOptionLevel.Tcp, among others.
option SocketOptionName The name of the socket option to set. This corresponds to the level.
optionValue int or object The value of the socket option. The type depends on the specific option being set. For many options, an int is sufficient. For others, like LingerOption or IPOptions, an object is required.

Remarks

The SetOptions method allows fine-grained control over the underlying operating system's socket behavior. This is crucial for optimizing network performance, handling specific network conditions, and implementing advanced networking features.

Important: The available SocketOptionLevel and SocketOptionName values are platform-dependent. Always consult the documentation specific to your target operating system if you encounter unexpected behavior. Common options include:

  • SocketOptionLevel.Socket: Options that apply to the socket itself (e.g., Broadcast, DontFragment, Linger, SendBuffer, ReceiveBuffer, ReuseAddress).
  • SocketOptionLevel.IP: Options related to the Internet Protocol (e.g., IpTimeToLive, DontFragment, AddMembership, DropMembership).
  • SocketOptionLevel.Tcp: Options specific to TCP sockets (e.g., NoDelay, KeepAlive, ExpeditedData).

Common Socket Options

Here are some frequently used options:

Examples

Example 1: Enabling Broadcasts

using System.Net.Sockets;

// Assuming 'socket' is an initialized Socket object
try
{
    socket.SetOptions(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); // 1 to enable
    Console.WriteLine("Broadcast enabled.");
}
catch (SocketException ex)
{
    Console.WriteLine($"Error setting broadcast option: {ex.Message}");
}
                
Example 2: Configuring Linger Option

using System.Net.Sockets;

// Assuming 'socket' is an initialized Socket object
bool enableLinger = true;
int lingerTime = 10; // seconds

LingerOption lingerOption = new LingerOption(enableLinger, lingerTime);
try
{
    socket.SetOptions(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
    Console.WriteLine($"Linger option set: Enabled={enableLinger}, Time={lingerTime}s");
}
catch (SocketException ex)
{
    Console.WriteLine($"Error setting linger option: {ex.Message}");
}
                
Example 3: Setting Time-To-Live (TTL) for IP

using System.Net.Sockets;

// Assuming 'socket' is an initialized Socket object
int ttlValue = 64; // A common TTL value
try
{
    socket.SetOptions(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, ttlValue);
    Console.WriteLine($"IP TTL set to: {ttlValue}");
}
catch (SocketException ex)
{
    Console.WriteLine($"Error setting IP TTL option: {ex.Message}");
}
                

See Also