获取一个值,该值指示 Socket 是在上次 Send 还是 Receive 操作时连接到远程主机。
**命名空间:**System.Net.Sockets
**程序集:**System(在 system.dll 中)
语法
声明
Public ReadOnly Property Connected As Boolean
用法
Dim instance As Socket
Dim value As Boolean
value = instance.Connected
public bool Connected { get; }
public:
property bool Connected {
bool get ();
}
/** @property */
public boolean get_Connected ()
public function get Connected () : boolean
属性值
如果 Socket 在最近操作时连接到远程资源,则为 true;否则为 false。
备注
Connected 属性获取截止到最后的 I/O 操作时 Socket 的连接状态。当它返回 false 时,表明 Socket 要么从未连接,要么已断开连接。
Connected 属性的值反映最近操作时的连接状态。如果您需要确定连接的当前状态,请进行非阻止、零字节的 Send 调用。如果该调用成功返回或引发 WAEWOULDBLOCK 错误代码 (10035),则该套接字仍然处于连接状态;否则,该套接字不再处于连接状态。
如果调用用户数据报协议 (UDP) 套接字上的 Connect,则 Connected 属性始终返回 true;不过,此操作不更改 UDP 的内在无连接特性。
示例
下面的代码示例连接到远程终结点,请检查 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 'ConnectAndCheck
// .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: {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 );
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0