Socket.GetSocketOption Method

Definition

Returns the value of a Socket option.

Overloads

GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[])

Returns the specified Socket option setting, represented as a byte array.

GetSocketOption(SocketOptionLevel, SocketOptionName, Int32)

Returns the value of the specified Socket option in an array.

GetSocketOption(SocketOptionLevel, SocketOptionName)

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

GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[])

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Returns the specified Socket option setting, represented as a byte array.

public void GetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName, byte[] optionValue);

Parameters

optionLevel
SocketOptionLevel

One of the SocketOptionLevel values.

optionName
SocketOptionName

One of the SocketOptionName values.

optionValue
Byte[]

An array of type Byte that is to receive the option setting.

Exceptions

An error occurred when attempting to access the socket.

-or-

In .NET Compact Framework applications, the Windows CE default buffer space is set to 32768 bytes. You can change the per socket buffer space by calling SetSocketOption.

The Socket has been closed.

Examples

The following code example retrieves the LingerOption and Send time-out values and displays them to the console.

Console.WriteLine("This application will timeout if Send does not return within " + Encoding.ASCII.GetString(s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 4)));

// blocks until send returns
int i = s.Send(msg);

// blocks until read returns
byte[] bytes = new byte[1024];

s.Receive(bytes);

// Display to the screen
Console.WriteLine(Encoding.ASCII.GetString(bytes));
s.Shutdown(SocketShutdown.Both);
Console.WriteLine("If data remains to be sent, this application will stay open for " + ((LingerOption)s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger)).LingerTime.ToString());
s.Close();

Remarks

Socket options determine the behavior of the current Socket. Upon successful completion of this method, the array specified by the optionValue parameter contains the value of the specified Socket option.

When the length of the optionValue array is smaller than the number of bytes required to store the value of the specified Socket option, GetSocketOption will throw a SocketException. If you receive a 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 for a detailed description of the error. Use this overload for any sockets that are represented by Boolean values or integers.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

GetSocketOption(SocketOptionLevel, SocketOptionName, Int32)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Returns the value of the specified Socket option in an array.

public byte[] GetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName, int optionLength);

Parameters

optionLevel
SocketOptionLevel

One of the SocketOptionLevel values.

optionName
SocketOptionName

One of the SocketOptionName values.

optionLength
Int32

The length, in bytes, of the expected return value.

Returns

Byte[]

An array of type Byte that contains the value of the socket option.

Exceptions

An error occurred when attempting to access the socket.

-or-

In .NET Compact Framework applications, the Windows CE default buffer space is set to 32768 bytes. You can change the per socket buffer space by calling SetSocketOption.

The Socket has been closed.

Examples

The following code example retrieves the LingerOption and Send time-out values and displays them to the console.

Console.WriteLine("This application will timeout if Send does not return within " + Encoding.ASCII.GetString(s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 4)));

// blocks until send returns
int i = s.Send(msg);

// blocks until read returns
byte[] bytes = new byte[1024];

s.Receive(bytes);

// Display to the screen
Console.WriteLine(Encoding.ASCII.GetString(bytes));
s.Shutdown(SocketShutdown.Both);
Console.WriteLine("If data remains to be sent, this application will stay open for " + ((LingerOption)s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger)).LingerTime.ToString());
s.Close();

Remarks

The optionLength parameter sets the maximum size of the returned byte array. If the option value requires fewer bytes, the array will contain only that many bytes. If the option value requires more bytes, GetSocketOption will throw a SocketException. Use this overload for any sockets that are represented by Boolean values or integers.

Note

If you receive a 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 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 Network Tracing in .NET Framework.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

GetSocketOption(SocketOptionLevel, SocketOptionName)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

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

public object? GetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName);
public object GetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName);

Parameters

optionLevel
SocketOptionLevel

One of the SocketOptionLevel values.

optionName
SocketOptionName

One of the SocketOptionName values.

Returns

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

Exceptions

An error occurred when attempting to access the socket.

-or-

optionName was set to the unsupported value MaxConnections.

The Socket has been closed.

Examples

The following code example retrieves the LingerOption and Send time-out values and displays them to the console.

Console.WriteLine("This application will timeout if Send does not return within " + Encoding.ASCII.GetString(s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 4)));

// blocks until send returns
int i = s.Send(msg);

// blocks until read returns
byte[] bytes = new byte[1024];

s.Receive(bytes);

// Display to the screen
Console.WriteLine(Encoding.ASCII.GetString(bytes));
s.Shutdown(SocketShutdown.Both);
Console.WriteLine("If data remains to be sent, this application will stay open for " + ((LingerOption)s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger)).LingerTime.ToString());
s.Close();

Remarks

Socket options determine the behavior of the current Socket. Use this overload to get the Linger, AddMembership, and DropMembershipSocket options. For the Linger option, use Socket for the optionLevel parameter. For AddMembership and DropMembership, use IP. If you want to set the value of any of the options listed above, use the SetSocketOption method.

Note

If you receive a 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 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 Network Tracing in .NET Framework.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1