This documentation is archived and is not being maintained.

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)

public bool Connected { 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.

// .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);
client.Connect(anEndPoint);
if (!client.Connected) 
{
  Console.WriteLine("Winsock error: "
     + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
} 

 // 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("Connected from an exception!");
   else
   {
     Console.WriteLine("Disconnected: {0}!", e.NativeErrorCode);
   }
 }
 finally
 {
   client.Blocking = blockingState;
 }

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

[STAThread]
int main()
{
#if FOOZ
  Socket* s = new Socket(AddressFamily::InterNetwork, 
			 SocketType::Stream,
			 ProtocolType::Tcp);

    string host = "localhost";
    int port = 80;

    IPHostEntry hostEntry = Dns::Resolve(host);
    IPEndPoint EPHost = new IPEndPoint(hostEntry->AddressList[0], port);

    ConnectAndCheck(s, EPHost);
#endif
}

#if FOOZ
public class Sample
{
  static void ConnectAndCheck(Socket client, EndPoint anEndPoint)
  {
    client.Connect(anEndPoint);
    if (!client.Connected) 
    {
      Console.WriteLine("Winsock error: "
         + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
    } 

     // 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("Connected from an exception!");
       else
       {
         Console.WriteLine("Disconnected: {0}!", e.NativeErrorCode);
       }
     }
     finally
     {
       client.Blocking = blockingState;
     }

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Show: