Socket.Connected Socket.Connected Socket.Connected Socket.Connected Property

定義

最後に実行された Send 操作または Receive 操作の時点で、Socket がリモート ホストに接続されていたかどうかを示す値を取得します。Gets a value that indicates whether a Socket is connected to a remote host as of the last Send or Receive operation.

public:
 property bool Connected { bool get(); };
public bool Connected { get; }
member this.Connected : bool
Public ReadOnly Property Connected As Boolean

プロパティ値

最後に実行された操作の時点で、Socket がリモート リソースに接続されていた場合は true。それ以外の場合は falsetrue if the Socket was connected to a remote resource as of the most recent operation; otherwise, false.

次のコード例では、リモートエンドポイントに接続Connectedし、プロパティをチェックして、接続の現在の状態を確認します。The following code example connects to a remote endpoint, checks the Connected property, and checks the current state of the connection.

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 );
// .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);
    ' .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

注釈

プロパティConnectedは、 Socket最後の i/o 操作時のの接続状態を取得します。The Connected property gets the connection state of the Socket as of the last I/O operation. が返さfalseれた場合Socket 、が接続されていないか、接続されていません。When it returns false, the Socket was either never connected, or is no longer connected.

Connectedプロパティの値は、最新の操作での接続の状態を反映します。The value of the Connected property reflects the state of the connection as of the most recent operation. 接続の現在の状態を確認する必要がある場合は、非ブロッキングのゼロバイトの送信呼び出しを行います。If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. 呼び出しが正常に返された場合、または WAEWOULDBLOCK エラーコード (10035) をスローした場合、ソケットはまだ接続されています。それ以外の場合、ソケットは接続されなくなります。If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.

ユーザーデータグラムプロトコルConnect (udp) ソケットでを呼び出すと、プロパティConnectedは常にtrueを返します。ただし、この操作では、udp の固有のコネクションレス特性は変更されません。If you call Connect on a User Datagram Protocol (UDP) socket, the Connected property always returns true; however, this action does not change the inherent connectionless nature of UDP.

適用対象