Socket.Connected プロパティ
最後に実行された Send 操作または Receive 操作の時点で、Socket がリモート ホストに接続されていたかどうかを示す値を取得します。
名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文

Connected プロパティは、I/O 操作が最後に実行された時点での Socket の接続状態を取得します。false を返した場合、Socket は一度も接続されていないか、接続が切断されています。
Connected プロパティの値は、最後に実行された操作の時点の接続の状態を反映しています。接続の現在の状態を確認する必要がある場合は、非ブロッキングで 0 バイトの Send 呼び出しを実行します。呼び出しが正常終了するか WAEWOULDBLOCK エラー コード (10035) がスローされる場合は、ソケットがまだ接続中です。それ以外の場合は、ソケットは接続されていません。
ユーザー データグラム プロトコル (UDP: User Datagram Protocol) ソケットで Connect を呼び出す場合、Connected プロパティは、常に true を返します。ただし、この操作を行っても UDP の本質的なコネクションレスの性質は変わりません。

リモート エンドポイントに接続し、Connected プロパティをチェックして、接続の現在の状態を確認するコード例を次に示します。
' .Connect throws an exception if unsuccessful client.Connect(anEndPoint) ' This is how you can determine whether a socket is still connected. Dim blockingState As Boolean = client.Blocking Try Dim tmp(0) As Byte client.Blocking = False client.Send(tmp, 0, 0) Console.WriteLine("Connected!") Catch e As SocketException ' 10035 == WSAEWOULDBLOCK If e.NativeErrorCode.Equals(10035) Then Console.WriteLine("Still Connected, but the Send would block") Else Console.WriteLine("Disconnected: error code {0}!", e.NativeErrorCode) End If Finally client.Blocking = blockingState End Try Console.WriteLine("Connected: {0}", client.Connected) End Sub 'ConnectAndCheck
// .Connect throws an exception if unsuccessful client.Connect(anEndPoint); // This is how you can determine whether a socket is still connected. bool blockingState = client.Blocking; try { byte [] tmp = new byte[1]; client.Blocking = false; client.Send(tmp, 0, 0); Console.WriteLine("Connected!"); } catch (SocketException e) { // 10035 == WSAEWOULDBLOCK if (e.NativeErrorCode.Equals(10035)) Console.WriteLine("Still Connected, but the Send would block"); else { Console.WriteLine("Disconnected: error code {0}!", e.NativeErrorCode); } } finally { client.Blocking = blockingState; } Console.WriteLine("Connected: {0}", client.Connected);
client->Connect( anEndPoint ); if ( !client->Connected ) { Console::WriteLine( "Winsock error: {0}", Convert::ToString( System::Runtime::InteropServices::Marshal::GetLastWin32Error() ) ); } // This is how you can determine whether a socket is still connected. bool blockingState = client->Blocking; try { array<Byte>^tmp = gcnew array<Byte>(1); client->Blocking = false; client->Send( tmp, 0, static_cast<SocketFlags>(0) ); Console::WriteLine( L"Connected!" ); } catch ( SocketException^ e ) { // 10035 == WSAEWOULDBLOCK if ( e->NativeErrorCode.Equals( 10035 ) ) { Console::WriteLine( "Connected from an exception!" ); } else { Console::WriteLine( "Disconnected: {0}!", e->NativeErrorCode ); } } finally { client->Blocking = blockingState; } Console::WriteLine( "Connected: {0}", client->Connected );

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Socket.Connected プロパティのページへのリンク