System.Net.Sockets.Socket.GetSocketOption Method

Returns the value of a specified System.Net.Sockets.Socket option, represented as an object.

Syntax

public object GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName)

Parameters

optionLevel
One of the System.Net.Sockets.SocketOptionLevel values.
optionName
One of the System.Net.Sockets.SocketOptionName values.

Returns

An object that represents the value of the option. When the optionName parameter is set to SocketOptionName.Linger the return value is an instance of the System.Net.Sockets.LingerOption class. When optionName is set to SocketOptionName.AddMembership or SocketOptionName.DropMembership, the return value is an instance of the System.Net.Sockets.MulticastOption class. When optionName is any other value, the return value is an integer.

Exceptions

TypeReason
System.Net.Sockets.SocketException
Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.
ObjectDisposedExceptionThe current instance has been disposed.

Remarks

System.Net.Sockets.Socket options determine the behavior of the current System.Net.Sockets.Socket. Use this overload to get the SocketOptionName.Linger, SocketOptionName.AddMembership, and SocketOptionName.DropMembership System.Net.Sockets.Socket options. For the SocketOptionName.Linger option, use System.Net.Sockets.Socket for the optionLevel parameter. For SocketOptionName.AddMembership and SocketOptionName.DropMembership, use SocketOptionLevel.IP. If you want to set the value of any of the options listed above, use the Socket.SetSocketOption(SocketOptionLevel, SocketOptionName, int) method.

Note:

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.

Note:

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

Example

The following example gets the state of the linger option and the size of the receive buffer, changes the values of both, then gets the new values.

C# Example

using System;
using System.Net.Sockets;

class OptionTest{

  public static void Main() {

    // Get the current option values.
    Socket someSocket =
      new Socket(AddressFamily.InterNetwork,
                 SocketType.Stream,
                 ProtocolType.Tcp);

    LingerOption lingerOp =
      (LingerOption)someSocket.GetSocketOption(
                    SocketOptionLevel.Socket,
                    SocketOptionName.Linger);

    int receiveBuffer =
      (int)someSocket.GetSocketOption(
           SocketOptionLevel.Socket,
           SocketOptionName.ReceiveBuffer);

    Console.WriteLine(
      "Linger option is {0} and set to {1} seconds.",
      lingerOp.Enabled.ToString(),
      lingerOp.LingerTime.ToString() );

    Console.WriteLine(
      "Size of the receive buffer is {0} bytes.",
      receiveBuffer.ToString() );

    // Change the options.
    lingerOp = new LingerOption(true, 10);
    someSocket.SetSocketOption(
      SocketOptionLevel.Socket,
      SocketOptionName.Linger,
      lingerOp);

    someSocket.SetSocketOption(
      SocketOptionLevel.Socket,
      SocketOptionName.ReceiveBuffer,
      2048);

    Console.WriteLine(
      "The SetSocketOption method has been called.");
 
    // Get the new option values.
    lingerOp =
      (LingerOption)someSocket.GetSocketOption(
                    SocketOptionLevel.Socket,
                    SocketOptionName.Linger);

    receiveBuffer =
      (int)someSocket.GetSocketOption(
           SocketOptionLevel.Socket,
           SocketOptionName.ReceiveBuffer);

    Console.WriteLine(
      "Linger option is now {0} and set to {1} seconds.",
      lingerOp.Enabled.ToString(),
      lingerOp.LingerTime.ToString());

    Console.WriteLine(
      "Size of the receive buffer is now {0} bytes.",
      receiveBuffer.ToString());
  }
}
   

The output is

Linger option is False and set to 0 seconds.
Size of the receive buffer is 8192 bytes.
The SetSocketOption method has been called.
Linger option is now True and set to 10 seconds.
Size of the receive buffer is now 2048 bytes.

Requirements

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