Socket.GetSocketOption Socket.GetSocketOption Socket.GetSocketOption Socket.GetSocketOption Method

定义

返回 Socket 选项的值。Returns the value of a Socket option.

重载

GetSocketOption(SocketOptionLevel, SocketOptionName) GetSocketOption(SocketOptionLevel, SocketOptionName) GetSocketOption(SocketOptionLevel, SocketOptionName) GetSocketOption(SocketOptionLevel, SocketOptionName)

返回指定的 Socket 选项的值,表示为一个对象。Returns the value of a specified Socket option, represented as an object.

GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[]) GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[]) GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[]) GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[])

返回指定的 Socket 选项设置,表示为字节数组。Returns the specified Socket option setting, represented as a byte array.

GetSocketOption(SocketOptionLevel, SocketOptionName, Int32) GetSocketOption(SocketOptionLevel, SocketOptionName, Int32) GetSocketOption(SocketOptionLevel, SocketOptionName, Int32) GetSocketOption(SocketOptionLevel, SocketOptionName, Int32)

返回数组中指定的 Socket 选项的值。Returns the value of the specified Socket option in an array.

GetSocketOption(SocketOptionLevel, SocketOptionName) GetSocketOption(SocketOptionLevel, SocketOptionName) GetSocketOption(SocketOptionLevel, SocketOptionName) GetSocketOption(SocketOptionLevel, SocketOptionName)

返回指定的 Socket 选项的值,表示为一个对象。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);
member this.GetSocketOption : System.Net.Sockets.SocketOptionLevel * System.Net.Sockets.SocketOptionName -> obj
Public Function GetSocketOption (optionLevel As SocketOptionLevel, optionName As SocketOptionName) As Object

参数

返回

一个对象,表示选项的值。An object that represents the value of the option. 当将 optionName 参数设置为 Linger 时,返回值为 LingerOption 类的一个实例。When the optionName parameter is set to Linger the return value is an instance of the LingerOption class. 当将 optionName 设置为 AddMembershipDropMembership 时,返回值为 MulticastOption 类的一个实例。When optionName is set to AddMembership or DropMembership, the return value is an instance of the MulticastOption class. optionName 为其他任何值时,返回值为整数。When optionName is any other value, the return value is an integer.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket.

- 或 --or- optionName 设置为不支持的值 MaxConnectionsoptionName was set to the unsupported value MaxConnections.

示例

下面的代码示例检索LingerOptionSend超时值,并显示到控制台。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 'SetSocketOptions

注解

Socket 选项将确定当前的行为SocketSocket options determine the behavior of the current Socket. 使用此重载来获取LingerAddMembership,并DropMembershipSocket选项。Use this overload to get the Linger, AddMembership, and DropMembershipSocket options. 有关Linger选项,请使用SocketoptionLevel参数。For the Linger option, use Socket for the optionLevel parameter. 有关AddMembershipDropMembership,使用IPFor AddMembership and DropMembership, use IP. 如果你想要设置任何上面列出的选项的值,请使用SetSocketOption方法。If you want to set the value of any of the options listed above, use the SetSocketOption method.

备注

如果你收到SocketException,使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码有关错误的详细说明的文档。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅在.NET Framework 中进行网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[]) GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[]) GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[]) GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[])

返回指定的 Socket 选项设置,表示为字节数组。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())

参数

optionValue
Byte[]

Byte 类型的数组,用于接收选项设置。An array of type Byte that is to receive the option setting.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket.

- 或 --or- 在 .NET Compact Framework 应用程序中,Windows CE 默认缓冲区的空间被设置为 32768 字节。In .NET Compact Framework applications, the Windows CE default buffer space is set to 32768 bytes. 通过调用 SetSocketOption,可以更改每个套接字缓冲区的空间。You can change the per socket buffer space by calling SetSocketOption.

示例

下面的代码示例检索LingerOptionSend超时值,并显示到控制台。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 'SetSocketOptions

注解

Socket 选项将确定当前的行为SocketSocket options determine the behavior of the current Socket. 此方法成功完成后,通过将指定的数组optionValue参数包含指定的值Socket选项。Upon successful completion of this method, the array specified by the optionValue parameter contains the value of the specified Socket option.

时的长度optionValue数组是比存储指定的值所需的字节数小Socket选项,GetSocketOption将引发SocketExceptionWhen 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. 如果你收到SocketException,使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码有关错误的详细说明的文档。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅在.NET Framework 中进行网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

GetSocketOption(SocketOptionLevel, SocketOptionName, Int32) GetSocketOption(SocketOptionLevel, SocketOptionName, Int32) GetSocketOption(SocketOptionLevel, SocketOptionName, Int32) GetSocketOption(SocketOptionLevel, SocketOptionName, Int32)

返回数组中指定的 Socket 选项的值。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()

参数

optionLength
Int32 Int32 Int32 Int32

所需返回值的长度(以字节为单位)。The length, in bytes, of the expected return value.

返回

Byte[]

Byte 类型的数组,它包含套接字选项的值。An array of type Byte that contains the value of the socket option.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket.

- 或 --or- 在 .NET Compact Framework 应用程序中,Windows CE 默认缓冲区的空间被设置为 32768 字节。In .NET Compact Framework applications, the Windows CE default buffer space is set to 32768 bytes. 通过调用 SetSocketOption,可以更改每个套接字缓冲区的空间。You can change the per socket buffer space by calling SetSocketOption.

示例

下面的代码示例检索LingerOptionSend超时值,并显示到控制台。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 'SetSocketOptions

注解

optionLength参数设置返回的字节数组的最大大小。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. 如果该选项的值需要更多字节GetSocketOption将引发SocketExceptionIf 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.

备注

如果你收到SocketException,使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码有关错误的详细说明的文档。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅在.NET Framework 中进行网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

适用于