System.Net.Sockets.SocketOptions Enum
Specifies options for a Socket instance.
Summary
The SocketOptions enumeration defines a set of constants that can be used to set or retrieve various options on a Socket. These options control the behavior of the socket, such as whether it should operate in blocking or non-blocking mode, whether to reuse addresses, and how to handle keep-alive signals.
Understanding and correctly configuring SocketOptions is crucial for building robust and efficient network applications. Incorrect settings can lead to performance issues, connection problems, or security vulnerabilities.
Members
Enable or disable the TCP keep-alive option. When enabled, the system periodically sends a keep-alive packet to the remote host to check if the connection is still active.
This option is typically used with connection-oriented protocols like TCP.
SocketOptionName.KeepAlive
Control the behavior when a socket is closed. The Linger option specifies how long the socket should remain open after Socket.Close is called to ensure that any buffered data is sent.
It takes a LingerOption object as its value.
SocketOptionName.Linger
Indicates whether the Don't Fragment (DF) bit should be set in the IP header. When set, the packet will not be fragmented by routers along the path. This is often used in scenarios where fragmentation can cause issues, like with Path MTU Discovery.
SocketOptionName.DontFragment
Enable or disable the ability to send broadcast packets. Broadcasts are used to send data to all hosts on a network segment. Note that broadcast support can be restricted by network configuration and security policies.
SocketOptionName.Broadcast
Allow a socket to be bound to an address that is already in use. This is particularly useful in server applications that might need to restart quickly after being shut down, preventing "Address already in use" errors.
SocketOptionName.ReuseAddress
Set the size of the socket's send buffer. This buffer holds data waiting to be transmitted over the network. Adjusting this can improve performance for high-throughput applications.
SocketOptionName.SendBuffer
Set the size of the socket's receive buffer. This buffer holds incoming data that has been received from the network but not yet read by the application. Similar to `SendBuffer`, this can impact performance.
SocketOptionName.ReceiveBuffer
Enable or disable the Nagle algorithm for TCP sockets. When disabled (i.e., set to true), the socket will send data immediately, without waiting to accumulate more data. This can reduce latency but may increase the number of small packets sent.
SocketOptionName.NoDelay
Specify the network interface to use for outgoing traffic. This is useful on machines with multiple network adapters to control which interface a socket's traffic will use.
SocketOptionName.OutgoingInterface
Set the Time To Live (TTL) value for outgoing IP packets. The TTL is a hop limit to prevent packets from circulating indefinitely on the network.
SocketOptionName.IpTimeToLive
Usage Example
The following example demonstrates how to set the ReuseAddress and NoDelay options on a Socket.
using System;
using System.Net;
using System.Net.Sockets;
// ... inside a method or class
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
// Enable the ReuseAddress option.
listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
// Disable the Nagle algorithm for lower latency.
listener.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
// Bind the socket to the local endpoint and listen for incoming connections.
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 1111);
listener.Bind(localEndPoint);
listener.Listen(100);
Console.WriteLine("Socket options set. Listening on port 1111...");
// ... accept connections, etc.
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
// listener.Close(); // Important to close the socket when done
}
For UDP sockets, options like Broadcast and DontFragment are particularly relevant. Ensure you are using the correct SocketOptionLevel when setting options, as it differs between TCP and UDP.