property bool Connected { bool get(); };
public bool Connected { get; }
member this.Connected : bool
Public ReadOnly Property Connected As Boolean
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; 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!" ); 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; 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"); 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 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") 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 會反映線上狀態,如同最近一次的作業一樣。 如果您需要判斷連線的目前狀態,請建立非封鎖、零位元組的 Send 呼叫。 如果呼叫成功傳回或擲回 WAEWOULDBLOCK 錯誤碼, (10035) ,則通訊端仍已連線;否則,通訊端已不再連線。

如果您在 UDP) 通訊端 (使用者資料包通訊協定上呼叫 Connect ,屬性 Connected 一律會傳回 true ;不過,此動作不會變更 UDP 固有的無連線本質。