Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Connected Property

Socket::Connected Property

Gets a value that indicates whether a Socket is connected to a remote host as of the last Send or Receive operation.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

property bool Connected {
	bool get ();

Property Value

Type: System::Boolean
true if the Socket was connected to a remote resource as of the most recent operation; otherwise, false.

The Connected property gets the connection state of the Socket as of the last I/O operation. When it returns false, the Socket was either never connected, or is no longer 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. 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.

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.

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;
   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 );
   client->Blocking = blockingState;

Console::WriteLine( "Connected: {0}", client->Connected );

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
© 2015 Microsoft