SocketOptionLevel Enumeration
The SocketOptionLevel enumeration specifies the level at which a socket option is set or retrieved. This enumeration is used in conjunction with the Socket.SetSocketOption and Socket.GetSocketOption methods.
Description
When you set or get a socket option, you need to specify which protocol level the option belongs to. SocketOptionLevel provides these distinct levels:
Socket: Options that apply directly to the socket itself, regardless of the underlying protocol.IP: Options that apply to the Internet Protocol (IP) layer.Tcp: Options that apply to the Transmission Control Protocol (TCP) layer.Udp: Options that apply to the User Datagram Protocol (UDP) layer.IPv6: Options that apply to the Internet Protocol version 6 (IPv6) layer.Icmp: Options that apply to the Internet Control Message Protocol (ICMP) layer.
Members
| Member | Value | Description |
|---|---|---|
Socket |
0 | Specifies socket options. |
IP |
1 | Specifies IP protocol options. |
Tcp |
6 | Specifies TCP protocol options. |
Udp |
17 | Specifies UDP protocol options. |
IPv6 |
41 | Specifies IPv6 protocol options. |
Icmp |
1 | Specifies ICMP protocol options. Note: This shares the same value as IP on some platforms. |
Usage Example
Here's how you might use SocketOptionLevel to set the DontFragment option on an IP socket:
C# Example
using System;
using System.Net;
using System.Net.Sockets;
public class SocketExample
{
public static void SetDontFragment()
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Raw);
try
{
// Set the DontFragment option to true for IP layer
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DontFragment, true);
Console.WriteLine("DontFragment option set successfully.");
}
catch (SocketException ex)
{
Console.WriteLine($"SocketException: {ex.Message}");
}
finally
{
if (socket.IsBound)
{
socket.Close();
}
}
}
public static void Main(string[] args)
{
SetDontFragment();
}
}