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[])

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

public:
 void GetSocketOption(System::Net::Sockets::SocketOptionLevel optionLevel, System::Net::Sockets::SocketOptionName optionName, cli::array <System::Byte> ^ optionValue);
public void GetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName, byte[] optionValue);
member this.GetSocketOption : System.Net.Sockets.SocketOptionLevel * System.Net.Sockets.SocketOptionName * byte[] -> unit
Public Sub GetSocketOption (optionLevel As SocketOptionLevel, optionName As SocketOptionName, optionValue As Byte())

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::Write("This application will timeout if Send does not return within ");
Console::WriteLine(Encoding::ASCII->GetString(s->GetSocketOption(SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 4)));

// Blocks until send returns.
int i = s->Send(msg);

// Blocks until read returns.
array<Byte>^ bytes = gcnew array<Byte>(1024);

s->Receive(bytes);

// Displays to the screen.
Console::WriteLine(Encoding::ASCII->GetString(bytes));
s->Shutdown(SocketShutdown::Both);
Console::Write("If data remains to be sent, this application will stay open for ");
Console::WriteLine(safe_cast<LingerOption^>(s->GetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Linger))->LingerTime.ToString());
s->Close();
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();
    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
    Dim i As Integer = s.Send(msg)

    ' blocks until read returns
    Dim bytes(1024) As Byte
    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 " + CType(s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger), LingerOption).LingerTime.ToString()))
    s.Close()
End Sub

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

GetSocketOption(SocketOptionLevel, SocketOptionName, Int32)

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

public:
 cli::array <System::Byte> ^ GetSocketOption(System::Net::Sockets::SocketOptionLevel optionLevel, System::Net::Sockets::SocketOptionName optionName, int optionLength);
public byte[] GetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName, int optionLength);
member this.GetSocketOption : System.Net.Sockets.SocketOptionLevel * System.Net.Sockets.SocketOptionName * int -> byte[]
Public Function GetSocketOption (optionLevel As SocketOptionLevel, optionName As SocketOptionName, optionLength As Integer) As Byte()

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::Write("This application will timeout if Send does not return within ");
Console::WriteLine(Encoding::ASCII->GetString(s->GetSocketOption(SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 4)));

// Blocks until send returns.
int i = s->Send(msg);

// Blocks until read returns.
array<Byte>^ bytes = gcnew array<Byte>(1024);

s->Receive(bytes);

// Displays to the screen.
Console::WriteLine(Encoding::ASCII->GetString(bytes));
s->Shutdown(SocketShutdown::Both);
Console::Write("If data remains to be sent, this application will stay open for ");
Console::WriteLine(safe_cast<LingerOption^>(s->GetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Linger))->LingerTime.ToString());
s->Close();
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();
    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
    Dim i As Integer = s.Send(msg)

    ' blocks until read returns
    Dim bytes(1024) As Byte
    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 " + CType(s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger), LingerOption).LingerTime.ToString()))
    s.Close()
End Sub

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

GetSocketOption(SocketOptionLevel, SocketOptionName)

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

public:
 System::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);
public object GetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName);
member this.GetSocketOption : System.Net.Sockets.SocketOptionLevel * System.Net.Sockets.SocketOptionName -> obj
Public Function GetSocketOption (optionLevel As SocketOptionLevel, optionName As SocketOptionName) As Object

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::Write("This application will timeout if Send does not return within ");
Console::WriteLine(Encoding::ASCII->GetString(s->GetSocketOption(SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 4)));

// Blocks until send returns.
int i = s->Send(msg);

// Blocks until read returns.
array<Byte>^ bytes = gcnew array<Byte>(1024);

s->Receive(bytes);

// Displays to the screen.
Console::WriteLine(Encoding::ASCII->GetString(bytes));
s->Shutdown(SocketShutdown::Both);
Console::Write("If data remains to be sent, this application will stay open for ");
Console::WriteLine(safe_cast<LingerOption^>(s->GetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Linger))->LingerTime.ToString());
s->Close();
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();
    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
    Dim i As Integer = s.Send(msg)

    ' blocks until read returns
    Dim bytes(1024) As Byte
    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 " + CType(s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger), LingerOption).LingerTime.ToString()))
    s.Close()
End Sub

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