Export (0) Print
Expand All

Socket.SetSocketOption Method (SocketOptionLevel, SocketOptionName, Object)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Sub SetSocketOption( _
   ByVal optionLevel As SocketOptionLevel, _
   ByVal optionName As SocketOptionName, _
   ByVal optionValue As Object _
)
[C#]
public void SetSocketOption(
 SocketOptionLevel optionLevel,
 SocketOptionName optionName,
 object optionValue
);
[C++]
public: void SetSocketOption(
 SocketOptionLevel optionLevel,
 SocketOptionName optionName,
 Object* optionValue
);
[JScript]
public function SetSocketOption(
   optionLevel : SocketOptionLevel,
 optionName : SocketOptionName,
 optionValue : Object
);

Parameters

optionLevel
One of the SocketOptionLevel values.
optionName
One of the SocketOptionName values.
optionValue
A LingerOption or MulticastOption containing the value of the option.

Exceptions

Exception Type Condition
ArgumentNullException optionValue is a null reference (Nothing in Visual Basic).
SocketException An error occurred when attempting to access the socket. See the Remarks section for more information.
ObjectDisposedException The Socket has been closed.

Remarks

Socket options determine the behavior of the current Socket. Use this overload to set the Linger, AddMembership, and DropMembership Socket 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 SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.

Windows 98, Windows NT 4.0 Platform Note:  You must call the Bind method before using AddMembership as the optionName parameter.

Example

[Visual Basic, C#, C++] The following example sets the LingerOption and Send time out values.

[Visual Basic] 
'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)


[C#] 
// 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);


[C++] 
// 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 = new LingerOption(true, 10);
s->SetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Linger, lingerOption);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Socket Class | Socket Members | System.Net.Sockets Namespace | Socket.SetSocketOption Overload List | SocketOptionName | SocketOptionLevel | GetSocketOption

Show:
© 2015 Microsoft