Socket.SetSocketOption 方法

定义

设置 Socket 选项。Sets a Socket option.

重载

SetSocketOption(SocketOptionLevel, SocketOptionName, Object)

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

SetSocketOption(SocketOptionLevel, SocketOptionName, Int32)

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

SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean)

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

SetSocketOption(SocketOptionLevel, SocketOptionName, Byte[])

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

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)

参数

optionLevel
SocketOptionLevel

SocketOptionLevel 值之一。One of the SocketOptionLevel values.

optionName
SocketOptionName

SocketOptionName 值之一。One of the SocketOptionName values.

optionValue
Object

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

例外

optionValuenulloptionValue is null.

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

Socket 已关闭。The Socket has been closed.

示例

下面的代码示例设置 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 选项确定当前 Socket的行为。Socket options determine the behavior of the current Socket. 使用此重载设置 LingerAddMembershipDropMembershipSocket 选项。Use this overload to set the Linger, AddMembership, and DropMembershipSocket options. 对于 Linger 选项,请对 optionLevel 参数使用 SocketFor 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)

将指定的 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)

参数

optionLevel
SocketOptionLevel

SocketOptionLevel 值之一。One of the SocketOptionLevel values.

optionName
SocketOptionName

SocketOptionName 值之一。One of the SocketOptionName values.

optionValue
Int32

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

例外

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

Socket 已关闭。The Socket has been closed.

示例

下面的代码示例将 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 选项确定当前 Socket的行为。Socket 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. 这些选项按适当的 SocketOptionLevel分组。These options are grouped by the appropriate SocketOptionLevel. 如果要设置这些选项中的任何一个,请确保对 optionLevel 参数使用适当的 SocketOptionLevelIf 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)

将指定的 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)

参数

optionLevel
SocketOptionLevel

SocketOptionLevel 值之一。One of the SocketOptionLevel values.

optionName
SocketOptionName

SocketOptionName 值之一。One of the SocketOptionName values.

optionValue
Boolean

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

例外

Socket 对象已关闭。The Socket object has been closed.

尝试访问套接字时出错。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 选项确定当前 Socket的行为。Socket options determine the behavior of the current Socket. optionValue 设置为 true 以启用该选项,或设置为 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. 如果要设置这些选项中的任何一个,请确保对 optionLevel 参数使用适当的 SocketOptionLevel 值。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[])

将指定的 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())

参数

optionLevel
SocketOptionLevel

SocketOptionLevel 值之一。One of the SocketOptionLevel values.

optionName
SocketOptionName

SocketOptionName 值之一。One of the SocketOptionName values.

optionValue
Byte[]

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

例外

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

Socket 已关闭。The Socket has been closed.

示例

下面的代码示例将 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 选项确定当前 Socket的行为。Socket 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.

另请参阅

适用于