detect the connection has been disconnect in tcpclient
-
hi, I use tcpclient to build client server application how to detect that the LAN is disconnected (unpluged) when we call networkstream.read()? //code while(true) { data = new byte[1024]; recv = ns.Read(data, 0, data.Length); // I unpluged the LAN but it still reading process if (recv == 0) break; Console.WriteLine( Encoding.ASCII.GetString(data, 0, recv)); ns.Write(data, 0, recv); } regards, andy
-
hi, I use tcpclient to build client server application how to detect that the LAN is disconnected (unpluged) when we call networkstream.read()? //code while(true) { data = new byte[1024]; recv = ns.Read(data, 0, data.Length); // I unpluged the LAN but it still reading process if (recv == 0) break; Console.WriteLine( Encoding.ASCII.GetString(data, 0, recv)); ns.Write(data, 0, recv); } regards, andy
-
you need to send packet from clients at regular intervals. I may call it KeepAlive. And when it is not received. It means it is disconnected. Ahsan Ullah Senior Software Engineer MCTS 2.0
hi ahsan, I have tried to sent the packet with interval 1 second each packet, but it needs 35 packet to detect that the client is disconnected. //code while (isPlugin) { Thread.Sleep(1000); try { if (isPlugin == true) { byte[] data = new byte[1024]; data = Encoding.ASCII.GetBytes("IsPlugin"); try { Log.Write("write Plugin"); sessionNetwork.Write(data, 0, 8); } catch (Exception ex) { Log.Write("Plugin is remove error : {0}", ex.Message); isPlugin = false; break; } } else { isPlugin = false; break; } } catch { Log.Write("Plugin is remove"); isPlugin = false; break; } } if (isPlugin == false) { Log.Write("Plugin is remove DisconnectOntraceSessionThread"); ClientDisconnect; }
-
hi ahsan, I have tried to sent the packet with interval 1 second each packet, but it needs 35 packet to detect that the client is disconnected. //code while (isPlugin) { Thread.Sleep(1000); try { if (isPlugin == true) { byte[] data = new byte[1024]; data = Encoding.ASCII.GetBytes("IsPlugin"); try { Log.Write("write Plugin"); sessionNetwork.Write(data, 0, 8); } catch (Exception ex) { Log.Write("Plugin is remove error : {0}", ex.Message); isPlugin = false; break; } } else { isPlugin = false; break; } } catch { Log.Write("Plugin is remove"); isPlugin = false; break; } } if (isPlugin == false) { Log.Write("Plugin is remove DisconnectOntraceSessionThread"); ClientDisconnect; }
From the snippet that you have shared i can't say for sure what your problem is. But if you could get my point. i.e. Keep track of your client(s) by sending an empty message for acknowledgment that client is alive. Ahsan Ullah Senior Software Engineer MCTS 2.0
-
hi ahsan, I have tried to sent the packet with interval 1 second each packet, but it needs 35 packet to detect that the client is disconnected. //code while (isPlugin) { Thread.Sleep(1000); try { if (isPlugin == true) { byte[] data = new byte[1024]; data = Encoding.ASCII.GetBytes("IsPlugin"); try { Log.Write("write Plugin"); sessionNetwork.Write(data, 0, 8); } catch (Exception ex) { Log.Write("Plugin is remove error : {0}", ex.Message); isPlugin = false; break; } } else { isPlugin = false; break; } } catch { Log.Write("Plugin is remove"); isPlugin = false; break; } } if (isPlugin == false) { Log.Write("Plugin is remove DisconnectOntraceSessionThread"); ClientDisconnect; }
-
hi ahsan, I have tried to sent the packet with interval 1 second each packet, but it needs 35 packet to detect that the client is disconnected. //code while (isPlugin) { Thread.Sleep(1000); try { if (isPlugin == true) { byte[] data = new byte[1024]; data = Encoding.ASCII.GetBytes("IsPlugin"); try { Log.Write("write Plugin"); sessionNetwork.Write(data, 0, 8); } catch (Exception ex) { Log.Write("Plugin is remove error : {0}", ex.Message); isPlugin = false; break; } } else { isPlugin = false; break; } } catch { Log.Write("Plugin is remove"); isPlugin = false; break; } } if (isPlugin == false) { Log.Write("Plugin is remove DisconnectOntraceSessionThread"); ClientDisconnect; }
hi, the problem is solved, I use Ping to detect the client is disconnected regards, andy