Bagikan melalui


Socket.Connected Properti

Definisi

Mendapatkan nilai yang menunjukkan apakah tersambung Socket ke host jarak jauh pada operasi atau Receive terakhirSend.

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

Nilai Properti

true Socket jika terhubung ke sumber daya jarak jauh pada operasi terbaru; jika tidak, false.

Contoh

Contoh kode berikut tersambung ke titik akhir jarak jauh, memeriksa Connected properti , dan memeriksa status koneksi saat ini.

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

Keterangan

Properti Connected mendapatkan status koneksi per Socket operasi I/O terakhir. Ketika kembali false, Socket tidak pernah tersambung, atau tidak lagi tersambung. Connected tidak aman untuk utas; ini dapat kembali true setelah operasi dibatalkan ketika Socket terputus dari utas lain.

Nilai Connected properti mencerminkan status koneksi pada operasi terbaru. Jika Anda perlu menentukan status koneksi saat ini, lakukan panggilan Kirim tanpa blokir dan nol byte. Jika panggilan berhasil dikembalikan atau melempar kode kesalahan WAEWOULDBLOCK (10035), maka soket masih tersambung; jika tidak, soket tidak lagi terhubung.

Jika Anda memanggil Connect soket Protokol Datagram Pengguna (UDP), Connected properti selalu mengembalikan true; namun, tindakan ini tidak mengubah sifat UDP tanpa koneksi yang melekat.

Berlaku untuk