.NET System.Net.Sockets Namespace
Configures the behavior of the current Socket instance by setting a socket option.
| 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. |
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.
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).Here are some frequently used options:
SocketOptionLevel.Socket, SocketOptionName.Linger: Controls how pending data is handled when the socket is closed. The optionValue should be an instance of LingerOption.SocketOptionLevel.Socket, SocketOptionName.ReuseAddress: Allows a socket to be bound to an address that is already in use. Useful for server applications that need to restart quickly.SocketOptionLevel.Socket, SocketOptionName.Broadcast: Enables or disables the ability to send broadcast packets.SocketOptionLevel.Tcp, SocketOptionName.NoDelay: Disables the Nagle algorithm for TCP, which can reduce latency for small, frequent packets at the cost of potentially higher overhead.SocketOptionLevel.Tcp, SocketOptionName.KeepAlive: Enables TCP keep-alive probes, which are sent periodically to check if the remote endpoint is still reachable.SocketOptionLevel.IP, SocketOptionName.IpTimeToLive: Sets the Time-To-Live (TTL) value for outgoing IP packets. This determines the maximum number of hops a packet can traverse before being discarded.
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}");
}
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}");
}
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}");
}