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选项确定当前Socket的行为。Socket options determine the behavior of the current Socket. 使用此重载Linger设置、 AddMembershipDropMembership Socket选项。Use this overload to set the Linger, AddMembership, and DropMembershipSocket options. 对于选项, 请使用Socket作为optionLevel参数。 LingerFor the Linger option, use Socket for the optionLevel parameter. 对于AddMembership IPDropMembership, 请使用。For 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选项确定当前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. 如果要设置这些选项中的任何一个, 请确保对SocketOptionLevel optionLevel参数使用合适的。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.

示例

下面的代码示例打开套接字, 并启用DontLinger OutOfBandInline和套接字选项。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. 设置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. 如果要设置这些选项中的任何一个, 请确保为SocketOptionLevel optionLevel参数使用适当的值。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选项确定当前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.

另请参阅

适用于