SocketOptionName Enumeration
Specifies socket options that can be set or retrieved.
Syntax
public enum SocketOptionName
Members
Indicates whether the listener is accepting connections.
Indicates whether to add a socket to a multicast group.
Indicates whether broadcast messages are enabled.
Indicates the timeout value for busy operations.
Indicates whether CRC32 checksum is enabled for UDP datagrams.
Indicates whether debugging is enabled for the socket.
Indicates whether the Don't Fragment flag is set.
Indicates whether to bind to an address exclusively.
Indicates whether expedited data transmission is enabled.
Indicates whether the IP header is included in the buffer.
Indicates the hop limit for IP packets.
Indicates whether keep-alive probes are enabled.
Indicates whether to linger on close.
Indicates whether multicast loopback is enabled.
Represents the maximum number of connections.
Indicates the multicast interface.
Indicates the multicast time to live.
Indicates whether the Nagle algorithm is disabled.
Indicates whether out-of-band data is received inline.
Indicates protocol-specific options.
Indicates the receive buffer size.
Indicates the minimum number of bytes to return for each receive call.
Indicates the receive timeout in milliseconds.
Indicates whether the address can be reused.
Indicates the send buffer size.
Indicates the minimum number of bytes to send in each send call.
Indicates the send timeout in milliseconds.
Indicates the Type of Service (ToS) byte for IP packets.
Indicates whether to use the loopback interface.
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();
}
}
}