Socket.SetSocketOption Socket.SetSocketOption Socket.SetSocketOption Socket.SetSocketOption Method

定义

设置 Socket 选项。Sets a Socket option.

重载

SetSocketOption(SocketOptionLevel, SocketOptionName, Object) SetSocketOption(SocketOptionLevel, SocketOptionName, Object) SetSocketOption(SocketOptionLevel, SocketOptionName, Object) SetSocketOption(SocketOptionLevel, SocketOptionName, Object)

将指定的 Socket 选项设置为指定值,表示为对象。Sets the specified Socket option to the specified value, represented as an object.

SetSocketOption(SocketOptionLevel, SocketOptionName, Int32) SetSocketOption(SocketOptionLevel, SocketOptionName, Int32) SetSocketOption(SocketOptionLevel, SocketOptionName, Int32) SetSocketOption(SocketOptionLevel, SocketOptionName, Int32)

将指定的 Socket 选项设置为指定的整数值。Sets the specified Socket option to the specified integer value.

SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean)

将指定的 Socket 选项设置为指定的 Boolean 值。Sets the specified Socket option to the specified Boolean value.

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

将指定的 Socket 选项设置为指定的值,表示为字节数组。Sets the specified Socket option to the specified value, represented as a byte array.

SetSocketOption(SocketOptionLevel, SocketOptionName, Object) SetSocketOption(SocketOptionLevel, SocketOptionName, Object) SetSocketOption(SocketOptionLevel, SocketOptionName, Object) SetSocketOption(SocketOptionLevel, SocketOptionName, Object)

将指定的 Socket 选项设置为指定值,表示为对象。Sets the specified Socket option to the specified value, represented as an object.

public:
 void SetSocketOption(System::Net::Sockets::SocketOptionLevel optionLevel, System::Net::Sockets::SocketOptionName optionName, System::Object ^ optionValue);
public void SetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName, object optionValue);
member this.SetSocketOption : System.Net.Sockets.SocketOptionLevel * System.Net.Sockets.SocketOptionName * obj -> unit
Public Sub SetSocketOption (optionLevel As SocketOptionLevel, optionName As SocketOptionName, optionValue As Object)

参数

optionValue
Object Object Object Object

一个 LingerOptionMulticastOption,它包含该选项的值。A LingerOption or MulticastOption that contains the value of the option.

异常

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

示例

下面的代码示例设置LingerOptionSend超时值。The following code example sets the LingerOption and Send time out values.

// Specifies that send operations will time-out 
// if confirmation is not received within 1000 milliseconds.
s->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 1000 );

// Specifies that the Socket will linger for 10 seconds after Close is called.
LingerOption^ lingerOption = gcnew LingerOption( true,10 );

s->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::Linger, lingerOption );
// Send operations will time-out if confirmation 
// is not received within 1000 milliseconds.
s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);

// The socket will linger for 10 seconds after Socket.Close is called.
LingerOption lingerOption = new LingerOption (true, 10);

s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);

'Send operations will time-out if confirmation is 
' not received within 1000 milliseconds.
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000)

' The socket will linger for 10 seconds after Socket.Close is called.
Dim lingerOption As New LingerOption(True, 10)
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption)

注解

Socket 选项将确定当前的行为SocketSocket options determine the behavior of the current Socket. 使用此重载来设置LingerAddMembership,并DropMembershipSocket选项。Use this overload to set 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. 如果你想要获取任何上面列出的选项的当前值,请使用GetSocketOption方法。If you want to get the current value of any of the options listed above, use the GetSocketOption 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.

另请参阅

SetSocketOption(SocketOptionLevel, SocketOptionName, Int32) SetSocketOption(SocketOptionLevel, SocketOptionName, Int32) SetSocketOption(SocketOptionLevel, SocketOptionName, Int32) SetSocketOption(SocketOptionLevel, SocketOptionName, Int32)

将指定的 Socket 选项设置为指定的整数值。Sets the specified Socket option to the specified integer value.

public:
 void SetSocketOption(System::Net::Sockets::SocketOptionLevel optionLevel, System::Net::Sockets::SocketOptionName optionName, int optionValue);
public void SetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName, int optionValue);
member this.SetSocketOption : System.Net.Sockets.SocketOptionLevel * System.Net.Sockets.SocketOptionName * int -> unit
Public Sub SetSocketOption (optionLevel As SocketOptionLevel, optionName As SocketOptionName, optionValue As Integer)

参数

optionValue
Int32 Int32 Int32 Int32

该选项的值。A value of the option.

异常

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

示例

下面的代码示例设置LingerOptionSend超时值。The following code example sets the LingerOption and Send time-out values.

// Specifies that send operations will time-out 
// if confirmation is not received within 1000 milliseconds.
s->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 1000 );

// Specifies that the Socket will linger for 10 seconds after Close is called.
LingerOption^ lingerOption = gcnew LingerOption( true,10 );

s->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::Linger, lingerOption );
// Send operations will time-out if confirmation 
// is not received within 1000 milliseconds.
s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);

// The socket will linger for 10 seconds after Socket.Close is called.
LingerOption lingerOption = new LingerOption (true, 10);

s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);

'Send operations will time-out if confirmation is 
' not received within 1000 milliseconds.
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000)

' The socket will linger for 10 seconds after Socket.Close is called.
Dim lingerOption As New LingerOption(True, 10)
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption)

注解

Socket 选项将确定当前的行为SocketSocket options determine the behavior of the current Socket. 有关使用选项Boolean数据类型,指定一个非零值以启用该选项和零值以禁用该选项。For an option with a Boolean 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. Socket 按协议支持的级别进行分组选项。Socket options are grouped by level of protocol support.

下面列出了各种Socket可以使用此重载来设置的选项。Listed below are the various Socket options that can be set using this overload. 这些选项进行分组的相应SocketOptionLevelThese options are grouped by the appropriate SocketOptionLevel. 如果你要设置上述任何选项,请确保使用适当SocketOptionLeveloptionLevel参数。If you intend to set any of these options, be sure to use the appropriate SocketOptionLevel for the optionLevel parameter. 选择要设置的选项必须指定在optionName参数。The option you choose to set must be specified in the optionName parameter. 如果你想要获取任何所列的选项的当前值,请使用GetSocketOption方法。If you want to get the current value of any of the options listed, use the GetSocketOption method.

SocketOptionLevel.Socket 可以使用此重载来设置的选项。SocketOptionLevel.Socket options that can be set using this overload.

SocketOptionLevel.IP 可以使用此重载来设置的选项。SocketOptionLevel.IP options that can be set using this overload.

SocketOptionLevel.Tcp 可以使用此重载来设置的选项。SocketOptionLevel.Tcp options that can be set using this overload.

SocketOptionLevel.Udp 可以使用此重载来设置的选项。SocketOptionLevel.Udp options that can be set using this overload.

SocketOptionLevel.IPv6 可以使用此重载来设置的选项。SocketOptionLevel.IPv6 options that can be set using this overload.

有关这些选项的详细信息,请参阅SocketOptionName枚举。For more information about these options, refer to the SocketOptionName enumeration.

备注

如果你收到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.

另请参阅

SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean)

将指定的 Socket 选项设置为指定的 Boolean 值。Sets the specified Socket option to the specified Boolean value.

public:
 void SetSocketOption(System::Net::Sockets::SocketOptionLevel optionLevel, System::Net::Sockets::SocketOptionName optionName, bool optionValue);
public void SetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName, bool optionValue);
member this.SetSocketOption : System.Net.Sockets.SocketOptionLevel * System.Net.Sockets.SocketOptionName * bool -> unit
Public Sub SetSocketOption (optionLevel As SocketOptionLevel, optionName As SocketOptionName, optionValue As Boolean)

参数

optionValue
Boolean Boolean Boolean Boolean

选项的值,表示为 BooleanThe value of the option, represented as a Boolean.

异常

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

示例

下面的代码示例打开一个套接字并启用DontLingerOutOfBandInline套接字选项。The following code example opens a socket and enables the DontLinger and the OutOfBandInline socket options.

// Establish the local endpoint for the socket.
IPHostEntry^ ipHost = Dns::GetHostEntry( Dns::GetHostName() );
IPAddress^ ipAddr = ipHost->AddressList[ 0 ];
IPEndPoint^ ipEndPoint = gcnew IPEndPoint( ipAddr,11000 );

// Create a TCP socket.
Socket^ client = gcnew Socket( AddressFamily::InterNetwork,SocketType::Stream,ProtocolType::Tcp );

// Connect the socket to the remote endpoint.
client->Connect( ipEndPoint );

// Set option that allows socket to close gracefully without lingering.
client->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::DontLinger, true );

// Set option that allows socket to receive out-of-band information in the data stream.
client->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::OutOfBandInline, true );

// Establish the local endpoint for the socket.
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress  ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);

// Create a TCP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp);

// Connect the socket to the remote endpoint.
client.Connect(ipEndPoint);

// Set option that allows socket to close gracefully without lingering.
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);

// Set option that allows socket to receive out-of-band information in the data stream.
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.OutOfBandInline, true);

注解

Socket 选项将确定当前的行为SocketSocket options determine the behavior of the current Socket. 设置optionValuetrue以启用该选项,或设置为false以禁用该选项。Set optionValue to true to enable the option, or to false to disable the option.

Socket 按协议支持的级别进行分组选项。Socket options are grouped by level of protocol support.

下面列出了各种Socket可以使用此重载来设置的选项。Listed below are the various Socket options that can be set using this overload. 这些选项进行分组的相应SocketOptionLevel值。These options are grouped by the appropriate SocketOptionLevel value. 如果你要设置上述任何选项,请确保使用适当SocketOptionLeveloptionLevel参数。If you intend to set any of these options, be sure to use the appropriate SocketOptionLevel value for the optionLevel parameter. 选择要设置的选项必须指定在optionName参数。The option you choose to set must be specified in the optionName parameter. 如果你想要获取任何所列的选项的当前值,请使用GetSocketOption方法。If you want to get the current value of any of the options listed, use the GetSocketOption method.

SocketOptionLevel.Socket 可以使用此重载来设置的选项。SocketOptionLevel.Socket options that can be set using this overload.

SocketOptionLevel.IP 可以使用此重载来设置的选项。SocketOptionLevel.IP options that can be set using this overload.

SocketOptionLevel.Tcp 可以使用此重载来设置的选项。SocketOptionLevel.Tcp options that can be set using this overload.

SocketOptionLevel.Udp 可以使用此重载来设置的选项。SocketOptionLevel.Udp options that can be set using this overload.

有关这些选项的详细信息,请参阅SocketOptionName枚举。For more information on these options, refer to the SocketOptionName enumeration.

备注

如果你收到SocketException异常,请使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException exception, 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.

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

将指定的 Socket 选项设置为指定的值,表示为字节数组。Sets the specified Socket option to the specified value, represented as a byte array.

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

参数

optionValue
Byte[]

Byte 类型的数组,表示选项值。An array of type Byte that represents the value of the option.

异常

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

示例

下面的代码示例设置LingerOptionSend超时值。The following code example sets the LingerOption and Send time-out values.

// Specifies that send operations will time-out 
// if confirmation is not received within 1000 milliseconds.
s->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 1000 );

// Specifies that the Socket will linger for 10 seconds after Close is called.
LingerOption^ lingerOption = gcnew LingerOption( true,10 );

s->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::Linger, lingerOption );
// Send operations will time-out if confirmation 
// is not received within 1000 milliseconds.
s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);

// The socket will linger for 10 seconds after Socket.Close is called.
LingerOption lingerOption = new LingerOption (true, 10);

s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);

'Send operations will time-out if confirmation is 
' not received within 1000 milliseconds.
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000)

' The socket will linger for 10 seconds after Socket.Close is called.
Dim lingerOption As New LingerOption(True, 10)
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption)

注解

Socket 选项将确定当前的行为SocketSocket options determine the behavior of the current Socket. 使用此重载来设置那些Socket需要为选项值的字节数组的选项。Use this overload to set those Socket options that require a byte array as an option value.

备注

如果你收到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.

另请参阅

适用于