System.Net.Sockets.Socket.SetSocketOption Method

Sets the specified System.Net.Sockets.Socket option to the specified integer value.


public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)


One of the System.Net.Sockets.SocketOptionLevel values.
One of the System.Net.Sockets.SocketOptionName values.
A value of the option.


Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.
System.Security.SecurityExceptionA caller in the call stack does not have the required permissions.
ObjectDisposedExceptionThe current instance has been disposed.


System.Net.Sockets.Socket options determine the behavior of the current System.Net.Sockets.Socket. For an option with a bool data type, specify a nonzero value to enable the option, and a zero value to disable the option. For an option with an integer data type, specify the appropriate value. System.Net.Sockets.Socket options are grouped by level of protocol support.

Listed below are the various System.Net.Sockets.Socket options that can be set using this overload. These options are grouped by the appropriate System.Net.Sockets.SocketOptionLevel. If you intend to set any of these options, be sure to use the appropriate System.Net.Sockets.SocketOptionLevel for the optionLevel parameter. The option you choose to set must be specified in the optionName parameter. If you want to get the current value of any of the options listed, use the Socket.GetSocketOption(SocketOptionLevel, SocketOptionName) method.

SocketOptionLevel.Socket options that can be set using this overload.

SocketOptionLevel.IP options that can be set using this overload.

SocketOptionLevel.Tcp options that can be set using this overload.

SocketOptionLevel.Udp options that can be set using this overload.

SocketOptionLevel.IPv6 options that can be set using this overload.

For more information about these options, refer to the System.Net.Sockets.SocketOptionName enumeration.


If you receive a System.Net.Sockets.SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.


This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].


System.Security.Permissions.SecurityPermissionSome options require permission to access unmanaged code. All the options that do not require permission are noted in the tables in the Description section. All options not so noted require this permission. See System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode.


Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0,,