Socket.SetSocketOption メソッド (SocketOptionLevel, SocketOptionName, Int32)
指定した Socket オプションを、指定した値に設定します。
Overloads Public Sub SetSocketOption( _
ByVal optionLevel As SocketOptionLevel, _ ByVal optionName As SocketOptionName, _ ByVal optionValue As Integer _)
[C#]
public void SetSocketOption(SocketOptionLeveloptionLevel,SocketOptionNameoptionName,intoptionValue);
[C++]
public: void SetSocketOption(SocketOptionLeveloptionLevel,SocketOptionNameoptionName,intoptionValue);
[JScript]
public function SetSocketOption(
optionLevel : SocketOptionLevel,optionName : SocketOptionName,optionValue : int);
パラメータ
- optionLevel
SocketOptionLevel 値の 1 つ。 - optionName
SocketOptionName 値の 1 つ。 - optionValue
オプションの値。
例外
例外の種類 | 条件 |
---|---|
SocketException | ソケットへのアクセスを試みているときにエラーが発生しました。詳細については、「解説」を参照してください。 |
ObjectDisposedException | Socket は閉じられています。 |
解説
Socket オプションは、現在の Socket の動作を決定します。 Boolean データ型のオプションの場合は、0 以外の値を指定するとオプションが有効になり、0 を指定するとオプションが無効になります。整数型のオプションの場合は、適切な値を指定します。 Socket オプションはプロトコル サポートのレベルによってグループ化されています。
このオーバーロードを使用して設定できるさまざまな Socket オプションを次に示します。これらのオプションは適切な SocketOptionLevel によってグループ化されます。これらのオプションのいずれかを設定する場合、適切な SocketOptionLevel を optionLevel パラメータに使用してください。設定するオプションは、 optionName パラメータで指定する必要があります。これらのオプションの現在の値を取得するには、 GetSocketOption メソッドを使用します。
このオーバーロードを使用して設定できる SocketOptionLevel.Socket オプション。
- Broadcast
- DontLinger
- Debug
- Error
- KeepAlive
- OutOfBandInline
- ReceiveBuffer
- ReceiveTimeout
- ReuseAddress
- SendBuffer
- SendTimeout
- Type
このオーバーロードを使用して設定できる SocketOptionLevel.IP オプション。
- HeaderIncluded
- IPOptions
- IpTimeToLive
- MulticastInterface
- MulticastLoopback
- MulticastTimeToLive
- TypeOfService
- UseLoopback
このオーバーロードを使用して設定できる SocketOptionLevel.Tcp オプション。
このオーバーロードを使用して設定できる SocketOptionLevel.Udp オプション。
これらのオプションの詳細については、 SocketOptionName 列挙体のトピックを参照してください。
メモ SocketException が発生した場合は、 SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのマニュアルから、エラーの詳細情報を確認できます。これは MSDN から入手できます。
Windows 98, Windows NT 4.0 プラットフォームに関する注意点: optionName パラメータとして AddMembership を使用する前に、 Bind メソッドを呼び出す必要があります。
使用例
[Visual Basic, C#, C++] LingerOption タイムアウト値と Send タイムアウト値を設定する例を次に示します。
'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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
Socket クラス | Socket メンバ | System.Net.Sockets 名前空間 | Socket.SetSocketOption オーバーロードの一覧 | SocketOptionName | SocketOptionLevel | GetSocketOption