Socket.Connected 속성

정의

Socket이 마지막으로 Send 또는 Receive 작업을 수행할 때 원격 호스트에 연결되었는지 여부를 나타내는 값을 가져옵니다.

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

속성 값

가장 최근 작업에서 Socket이 원격 리소스에 연결되었으면 true이고, 그렇지 않으면 false입니다.

예제

다음 코드 예제는 원격 엔드포인트에 연결하고, 속성을 확인하고 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 );
// .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

설명

속성은 Connected 마지막 I/O 작업 기준으로 의 Socket 연결 상태를 가져옵니다. 를 반환 false할 때 가 Socket 연결되지 않았거나 더 이상 연결되지 않았습니다. Connected가 스레드로부터 안전하지 않습니다. 가 다른 스레드에서 연결이 끊어지면 작업이 중단된 Socket 후에 반환 true 될 수 있습니다.

속성 값 Connected 은 가장 최근 작업의 연결 상태를 반영합니다. 연결의 현재 상태를 결정해야 하는 경우 비 차단, 0바이트 보내기 호출을 합니다. 호출이 성공적으로 반환되거나 WAEWOULDBLOCK 오류 코드(10035)를 throw하는 경우 소켓은 여전히 연결됩니다. 그렇지 않으면 소켓이 더 이상 연결되지 않습니다.

UDP(사용자 데이터그램 프로토콜) 소켓에서 를 호출 Connect 하는 경우 속성은 Connected 항상 를 반환 true합니다. 그러나 이 작업은 UDP의 고유 연결 없는 특성을 변경하지 않습니다.

적용 대상