使用Socket.Connected属性 在发送数据之前,可以使用TCPClient的Socket属性的Connected属性来检查连接是否已经建立。如果Connected属性返回true,则表示连接已经建立,可以继续发送数据。如果返回false,则表示连接已经断开。 示例代码: 代码语言:csharp 复制 if (tcpClient.Client.Con
public bool Connected { get; } 属性值 Boolean 如果Client 套接字与截止到最近的操作时的远程资源连接,则为 true;否则为 false。 示例 下面的代码示例连接到远程终结点,然后验证连接。 C# 复制 static void GetConnected() { // Find out whether the socket is connected to the remote // host. Conso...
= client.Connected; if (connected) { try { if (client.Client.Poll(0, SelectMode.SelectRead)) { byte[] buff = new byte[1]; if (client.Client.Receive(buff, SocketFlags.Peek) == 0) { connected = false; } } } catch (SocketException) { connected = false; } } return connected; ...
connected = false; exception = null; Thread thread = new Thread(new ThreadStart(BeginConnect)); thread.IsBackground = true; // 作为后台线程处理 // 不会占用机器太长的时间 thread.Start(); // 等待如下的时间 thread.Join(_timeout_milliseconds); if (connected == true) { // 如果成功就返回...
connected = false; exception = null; Thread thread = new Thread(new ThreadStart(BeginConnect)); thread.IsBackground = true; // 作为后台线程处理 // 不会占用机器太长的时间 thread.Start(); // 等待如下的时间 thread.Join(_timeout_milliseconds); ...
没有连接上Connected这个状态一般应该是false,不过这个状态没有收发数据的时候不准,不要太依赖于这个属性。 另外,你的代码有问题,没有使用EndConnect,用上后如果连接不上应该会抛异常的,把你的代码改成这样试试。 public void ConnectCallback(IAsyncResult result) { TcpClient client = (TcpClient)result.AsyncState...
50. if (clientSocket.Connected == false) 51. break; 52. 53. int length = clientSocket.Receive(data); 54. message = Encoding.UTF8.GetString(data, 0, length); 55. //chatLabel.text += "\n" + message; 56. } 57. } 58.
staticvoidGetConnected(){// Find out whether the socket is connected to the remote// host.Console.WriteLine("Connected value is {0}", t.Connected); } Remarks TheConnectedproperty gets the connection state of theClientsocket as of the last I/O operation. When it returnsfalse, theClientsocket...
if (taskStarted == false) { taskStarted = true; startTCPClient(); } } } /** * 检查client连接 */ void doTCPClientTick(){ //检查是否断开,断开后重连 if(WiFi.status() != WL_CONNECTED) return; if (!client.connected()) {//断开重连 ...
{ get; set; } AutoRefreshHtmlLog log; public bool isConnected = false; public event Action ChannelActive; public event Action ReadComplete; public event Action Read; public event Action ExceptionOccured; private string destination=null; private int port; AutoResetEvent do_connect = new AutoReset...