Socket.SetSocketOption 方法

定義

設定 Socket 選項。Sets a Socket option.

多載

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, Int32)

將指定 Socket 選項設定為指定的整數值。Sets the specified Socket option to the specified integer value.

SetSocketOption(SocketOptionLevel, SocketOptionName, Object)

將指定的 Socket 選項設定為指定值,表示為物件。Sets the specified Socket option to the specified value, represented as an object.

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 選項會決定目前的行為 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. 如果您想要設定其中任何選項,請務必針對參數使用適當的 SocketOptionLeveloptionLevelIf you intend to set any of these options, be sure to use the appropriate SocketOptionLevel value for the optionLevel parameter. 您選擇要設定的選項必須在參數中指定 optionNameThe 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 選項會決定目前的行為 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.

另請參閱

適用於

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 選項會決定目前的行為 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. 您選擇要設定的選項必須在參數中指定 optionNameThe 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, 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 選項會決定目前的行為 SocketSocket options determine the behavior of the current Socket. 您可以使用這個多載來設定 LingerAddMembershipDropMembership Socket 選項。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.

另請參閱

適用於