Editare

Partajați prin


Socket.SetSocketOption Method

Definition

Sets a Socket option.

Overloads

SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean)

Sets the specified Socket option to the specified Boolean value.

SetSocketOption(SocketOptionLevel, SocketOptionName, Byte[])

Sets the specified Socket option to the specified value, represented as a byte array.

SetSocketOption(SocketOptionLevel, SocketOptionName, Int32)

Sets the specified Socket option to the specified integer value.

SetSocketOption(SocketOptionLevel, SocketOptionName, Object)

Sets the specified Socket option to the specified value, represented as an object.

SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

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)

Parameters

optionLevel
SocketOptionLevel

One of the SocketOptionLevel values.

optionName
SocketOptionName

One of the SocketOptionName values.

optionValue
Boolean

The value of the option, represented as a Boolean.

Exceptions

The Socket object has been closed.

An error occurred when attempting to access the socket.

Examples

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);

Remarks

Socket options determine the behavior of the current Socket. Set optionValue to true to enable the option, or to false to disable the option.

Socket options are grouped by level of protocol support.

Listed below are the various Socket options that can be set using this overload. These options are grouped by the appropriate SocketOptionLevel value. If you intend to set any of these options, be sure to use the appropriate SocketOptionLevel value for the optionLevel parameter. The option you choose to set must be specified in the optionName parameter. If you want to get the current value of any of the options listed, use the GetSocketOption method.

SocketOptionLevel.Socket options that can be set using this overload.

SocketOptionLevel.IP options that can be set using this overload.

SocketOptionLevel.Tcp options that can be set using this overload.

SocketOptionLevel.Udp options that can be set using this overload.

For more information on these options, refer to the SocketOptionName enumeration.

Note

If you receive a SocketException exception, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Applies to

SetSocketOption(SocketOptionLevel, SocketOptionName, Byte[])

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

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())

Parameters

optionLevel
SocketOptionLevel

One of the SocketOptionLevel values.

optionName
SocketOptionName

One of the SocketOptionName values.

optionValue
Byte[]

An array of type Byte that represents the value of the option.

Exceptions

An error occurred when attempting to access the socket.

The Socket has been closed.

Examples

The following code example sets the LingerOption and Send time-out values.

// 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);
// The socket will linger for 10 seconds after Socket.Close is called.
var lingerOption = new LingerOption(true, 10);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
' 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)

Remarks

Socket options determine the behavior of the current Socket. Use this overload to set those Socket options that require a byte array as an option value.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

See also

Applies to

SetSocketOption(SocketOptionLevel, SocketOptionName, Int32)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

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)

Parameters

optionLevel
SocketOptionLevel

One of the SocketOptionLevel values.

optionName
SocketOptionName

One of the SocketOptionName values.

optionValue
Int32

A value of the option.

Exceptions

An error occurred when attempting to access the socket.

The Socket has been closed.

Examples

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);
// Send operations will time-out if confirmation
// is not received within 1000 milliseconds.
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
'Send operations will time-out if confirmation is
' not received within 1000 milliseconds.
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000)

Remarks

Socket options determine the behavior of the current Socket. 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 options are grouped by level of protocol support.

Listed below are the various Socket options that can be set using this overload. These options are grouped by the appropriate SocketOptionLevel. If you intend to set any of these options, be sure to use the appropriate SocketOptionLevel for the optionLevel parameter. The option you choose to set must be specified in the optionName parameter. If you want to get the current value of any of the options listed, use the GetSocketOption method.

SocketOptionLevel.Socket options that can be set using this overload.

SocketOptionLevel.IP options that can be set using this overload.

SocketOptionLevel.Tcp options that can be set using this overload.

SocketOptionLevel.Udp options that can be set using this overload.

SocketOptionLevel.IPv6 options that can be set using this overload.

For more information about these options, refer to the SocketOptionName enumeration.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

See also

Applies to

SetSocketOption(SocketOptionLevel, SocketOptionName, Object)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

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)

Parameters

optionLevel
SocketOptionLevel

One of the SocketOptionLevel values.

optionName
SocketOptionName

One of the SocketOptionName values.

optionValue
Object

A LingerOption or MulticastOption that contains the value of the option.

Exceptions

optionValue is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Examples

The following code example sets the LingerOption and Send time out values.

// 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);
// The socket will linger for 10 seconds after Socket.Close is called.
var lingerOption = new LingerOption(true, 10);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
' 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)

Remarks

Socket options determine the behavior of the current Socket. Use this overload to set the Linger, AddMembership, and DropMembershipSocket options. For the Linger option, use Socket for the optionLevel parameter. For AddMembership and DropMembership, use IP. If you want to get the current value of any of the options listed above, use the GetSocketOption method.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

See also

Applies to