.NET Documentation

SocketOptionName Enumeration

Defined in System.Net.Sockets

Specifies socket options that can be set or retrieved.

Syntax

public enum SocketOptionName

Members

AcceptConnection 2

Indicates whether the listener is accepting connections.

AddMembership 12

Indicates whether to add a socket to a multicast group.

Broadcast 32

Indicates whether broadcast messages are enabled.

BusyTimeout 64

Indicates the timeout value for busy operations.

Crc32Checksum 17

Indicates whether CRC32 checksum is enabled for UDP datagrams.

Debug 1

Indicates whether debugging is enabled for the socket.

DontFragment 128

Indicates whether the Don't Fragment flag is set.

ExclusiveAddressUse 1024

Indicates whether to bind to an address exclusively.

ExpeditedData 32

Indicates whether expedited data transmission is enabled.

HeaderIncluded 1

Indicates whether the IP header is included in the buffer.

HopLimit 21

Indicates the hop limit for IP packets.

KeepAlive 8

Indicates whether keep-alive probes are enabled.

Linger 128

Indicates whether to linger on close.

Loopback 64

Indicates whether multicast loopback is enabled.

MaxConnections 2147483647

Represents the maximum number of connections.

MulticastInterface 9

Indicates the multicast interface.

MulticastTimeToLive 10

Indicates the multicast time to live.

NoDelay 1

Indicates whether the Nagle algorithm is disabled.

OutofBandInline 256

Indicates whether out-of-band data is received inline.

ProtocolOptions 4

Indicates protocol-specific options.

ReceiveBuffer 4097

Indicates the receive buffer size.

ReceiveLowat 4098

Indicates the minimum number of bytes to return for each receive call.

ReceiveTimeout 4102

Indicates the receive timeout in milliseconds.

ReuseAddress 4

Indicates whether the address can be reused.

SendBuffer 4097

Indicates the send buffer size.

SendLowat 4098

Indicates the minimum number of bytes to send in each send call.

SendTimeout 4101

Indicates the send timeout in milliseconds.

TypeofService 13

Indicates the Type of Service (ToS) byte for IP packets.

UseLoopback 61

Indicates whether to use the loopback interface.

VersionHelpher 255

A value used internally by the system.

Remarks

The SocketOptionName enumeration is used with the Socket.SetSocketOption and Socket.GetSocketOption methods to specify socket options.

These options allow you to control the behavior of a socket, such as setting buffer sizes, enabling broadcast messages, or configuring keep-alive behavior.

Examples

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

using System;
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
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
            Console.WriteLine("KeepAlive option enabled.");

            // You can then proceed with your socket operations...
        }
        catch (SocketException ex)
        {
            Console.WriteLine($"SocketException: {ex.Message}");
        }
        finally
        {
            if (socket.Connected)
            {
                socket.Disconnect(false);
            }
            socket.Close();
        }
    }
}