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

定义

获取一个值,该值指示 Socket 是在上次 Send 还是 Receive 操作时连接到远程主机。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

注解

属性获取从最后一个 i/o 操作到Socket的的连接状态。 ConnectedThe Connected property gets the connection state of the Socket as of the last I/O operation. 当它返回falseSocket ,要么从未连接,要么不再处于连接状态。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.

适用于