SocketOptionName Enum

Specifies socket options.

Namespace: System.Net.Sockets
Assembly: System.Net.Primitives (in System.Net.Primitives.dll)

Syntax


public enum SocketOptionName

Members

Name Description
AcceptConnection Enables or disables the ability to accept connections on a listening socket. This option is used with SocketOptionLevel.Socket.
AddMembership Adds a socket to a multicast group. This option is used with SocketOptionLevel.IP.
Broadcast Enables or disables broadcast packets. This option is used with SocketOptionLevel.Socket.
BugWorkaround Enables or disables a bug workaround. This option is used with SocketOptionLevel.Socket.
DontFragment Prevents fragmentation of outgoing UDP packets. This option is used with SocketOptionLevel.IP.
DropMembership Removes a socket from a multicast group. This option is used with SocketOptionLevel.IP.
ExclusiveAddressUse Enables or disables the exclusive use of an address. This option is used with SocketOptionLevel.Socket.
FragmentSize Specifies the fragmentation size for outgoing packets. This option is used with SocketOptionLevel.IP.
HopLimit Specifies the hop limit for outgoing IP packets. This option is used with SocketOptionLevel.IP.
IPOptions Enables or disables the use of IP options. This option is used with SocketOptionLevel.IP.
KeepAlive Enables or disables the keep-alive socket option. This option is used with SocketOptionLevel.Socket.
Linger Specifies the time-out value for linger operations. This option is used with SocketOptionLevel.Socket.
NoDelay Enables or disables the Nagle algorithm. This option is used with SocketOptionLevel.Tcp.
OutBandInline Enables or disables the inline transmission of out-of-band data. This option is used with SocketOptionLevel.Socket.
ProtocolInformation Enables or disables the protocol information. This option is used with SocketOptionLevel.Socket.
ReceiveBuffer Specifies the size of the receive buffer for the socket. This option is used with SocketOptionLevel.Socket.
ReceiveMessageTimeout Specifies the time-out value for receiving messages. This option is used with SocketOptionLevel.Socket.
ReceiveTimeout Specifies the time-out value for receiving data. This option is used with SocketOptionLevel.Socket.
ReuseAddress Enables or disables the reuse of an address. This option is used with SocketOptionLevel.Socket.
SendBuffer Specifies the size of the send buffer for the socket. This option is used with SocketOptionLevel.Socket.
SendMessageTimeout Specifies the time-out value for sending messages. This option is used with SocketOptionLevel.Socket.
SendTimeout Specifies the time-out value for sending data. This option is used with SocketOptionLevel.Socket.
TypeCompatibility Enables or disables type compatibility. This option is used with SocketOptionLevel.Socket.
TypeToService Specifies the type of service to be provided. This option is used with SocketOptionLevel.IP.
UseLoopback Enables or disables the use of the loopback interface. This option is used with SocketOptionLevel.IP.
Vad Enables or disables voice activity detection. This option is used with SocketOptionLevel.IP.

Note: The specific availability and behavior of socket options can vary depending on the underlying operating system and network stack.

Remarks

The SocketOptionName enumeration provides constants for various socket options that can be set or retrieved using the Socket.SetSocketOption and Socket.GetSocketOption methods.

Each option is associated with a specific SocketOptionLevel, which indicates the protocol or layer to which the option applies (e.g., SocketOptionLevel.Socket for general socket options, SocketOptionLevel.Tcp for TCP-specific options, or SocketOptionLevel.IP for IP-specific options).

When setting or getting a socket option, you will typically use a combination of the appropriate SocketOptionLevel and a SocketOptionName value.

Example Usage

The following example demonstrates how to enable the KeepAlive option for a TCP socket:


using System;
using System.Net;
using System.Net.Sockets;

public class SocketOptionExample
{
    public static void Main(string[] args)
    {
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        try
        {
            // Enable KeepAlive
            byte[] keepAliveValues = new byte[] { 1, 0, 0, 0, Convert.ToByte(10), 0, 0, 0, Convert.ToByte(5), 0, 0, 0 }; // Keep-alive enabled, time=10s, interval=5s
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
            // Note: For more detailed KeepAlive configuration (time, interval, retry count),
            // you would typically use Winsock's SIO_KEEPALIVE_VALS IOCTL, which is more complex in .NET.
            // The basic 'true' sets it to default OS values.

            Console.WriteLine("KeepAlive option enabled for the socket.");

            // Example: Get the ReceiveTimeout value
            int receiveTimeout = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
            Console.WriteLine($"Current ReceiveTimeout: {receiveTimeout} ms");

            // Set a new ReceiveTimeout
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 30000); // 30 seconds
            receiveTimeout = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
            Console.WriteLine($"New ReceiveTimeout set to: {receiveTimeout} ms");

            // Example: Broadcast option (typically for UDP)
            // socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
            // Console.WriteLine("Broadcast option enabled.");
        }
        catch (SocketException e)
        {
            Console.WriteLine($"SocketException caught: {e.Message}");
        }
        finally
        {
            if (socket.Connected)
            {
                socket.Disconnect(false);
            }
            socket.Close();
        }
    }
}