Export (0) Print
Expand All

Socket.Connected Property

Gets a value indicating whether a Socket is connected to a remote host.

[Visual Basic]
Public ReadOnly Property Connected As Boolean
public bool Connected {get;}
public: __property bool get_Connected();
public function get Connected() : Boolean;

Property Value

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.

Because the Connected property only reflects the state of the connection as of the most recent operation, to determine whether a connected socket is still connected, you should attempt to send or receive a message. Once the message send fails, this property will no longer return true. Note that this behavior is by design. You cannot reliably test the state of the connection because, in the time between the test and a send/receive, the connection could have been lost. Your code should assume the socket is connected, and gracefully handle failed transmissions.


[Visual Basic, C#, C++] The following example connects to a remote endpoint and then verifies the connection.

[Visual Basic] 
If Not aSocket.Connected Then
    Console.WriteLine("Winsock error: " _
       + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()))
End If

if (!aSocket.Connected) {
   Console.WriteLine("Winsock error: "
      + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));

if (!aSocket->Connected) {
   Console::WriteLine(S"Winsock error: {0}", Convert::ToString(System::Runtime::InteropServices::Marshal::GetLastWin32Error()));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Socket Class | Socket Members | System.Net.Sockets Namespace

© 2014 Microsoft