Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. detect the connection has been disconnect in tcpclient

detect the connection has been disconnect in tcpclient

Scheduled Pinned Locked Moved C#
sysadmintutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eyeshield21
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • E eyeshield21

      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

      A Offline
      A Offline
      AhsanS
      wrote on last edited by
      #2

      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

      E 1 Reply Last reply
      0
      • A AhsanS

        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

        E Offline
        E Offline
        eyeshield21
        wrote on last edited by
        #3

        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; }

        A L E 3 Replies Last reply
        0
        • E eyeshield21

          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; }

          A Offline
          A Offline
          AhsanS
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • E eyeshield21

            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; }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Have you tried to flush? Your "packet" (which isn't a packet, remember this is TCP, you're working with a stream) is so small that it's probably just going to be buffered. Btw, reserving 1KB and then just throwing it away doesn't help in any way.

            1 Reply Last reply
            0
            • E eyeshield21

              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; }

              E Offline
              E Offline
              eyeshield21
              wrote on last edited by
              #6

              hi, the problem is solved, I use Ping to detect the client is disconnected regards, andy

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups