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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Networking

Networking

Scheduled Pinned Locked Moved C#
sysadminquestion
4 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.
  • A Offline
    A Offline
    Alper Camel
    wrote on last edited by
    #1

    I have a tcp server, when a client connects, then let its cable unplugged! Then How can server detect that it is disconnected? Thanks all!

    D B 2 Replies Last reply
    0
    • A Alper Camel

      I have a tcp server, when a client connects, then let its cable unplugged! Then How can server detect that it is disconnected? Thanks all!

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      It can't. The only thing it can do is assume that the client dropped the connection without closing it, because of a complete lack of communication for a specified timeout period. Or, with a little more thought, the server can try and PING the client every so often to see if it is still alive. This has it's problems too considering the client might be behind a firewall that won't let ICMP packets through. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      1 Reply Last reply
      0
      • A Alper Camel

        I have a tcp server, when a client connects, then let its cable unplugged! Then How can server detect that it is disconnected? Thanks all!

        B Offline
        B Offline
        BambooMoon
        wrote on last edited by
        #3

        It isn't really true that the server can't detect that the client disconnected. But the server can't detect it just by sitting there. I had some code somewhere that managed this by having the server periodically switch the socket to nonblocking mode and try to do a read in peek mode. Note that the receive in peek mode might return WSAEWOULDBLOCK if the socket is still live and there is nothing to read on it. But if the socket is closed, it will not return an exception. That is why I preload the bytes[] array with some data that I can then compare the read result against, of course assuming that I would never expect the read to contain that exact byte pattern. There might be a better way, but this worked fine for me. try { // Try doing a dummy read to see if the socket is still alive bytes[0] = 0xf; bytes[1] = 0xe; bytes[2] = 0xd; bytes[3] = 0xc; socket.Blocking = false; socket.Receive(bytes, 0, 4, SocketFlags.Peek); socket.Blocking = true; if (bytes[0] == 0xf && bytes[1] == 0xe && bytes[2] == 0xd && bytes[3] == 0xc) { // The sender has shut down the socket bexitreadloop = true; socket.Close(); form1.DisplaySocketStatusMessage("Lost connection with sender"); break; } } catch (SocketException e) { socket.Blocking = true; int code = e.ErrorCode; if (e.ErrorCode != WSAEWOULDBLOCK) { bexitreadloop = true; socket.Close(); form1.DisplaySocketStatusMessage("Socket error on read"); break; } }

        A 1 Reply Last reply
        0
        • B BambooMoon

          It isn't really true that the server can't detect that the client disconnected. But the server can't detect it just by sitting there. I had some code somewhere that managed this by having the server periodically switch the socket to nonblocking mode and try to do a read in peek mode. Note that the receive in peek mode might return WSAEWOULDBLOCK if the socket is still live and there is nothing to read on it. But if the socket is closed, it will not return an exception. That is why I preload the bytes[] array with some data that I can then compare the read result against, of course assuming that I would never expect the read to contain that exact byte pattern. There might be a better way, but this worked fine for me. try { // Try doing a dummy read to see if the socket is still alive bytes[0] = 0xf; bytes[1] = 0xe; bytes[2] = 0xd; bytes[3] = 0xc; socket.Blocking = false; socket.Receive(bytes, 0, 4, SocketFlags.Peek); socket.Blocking = true; if (bytes[0] == 0xf && bytes[1] == 0xe && bytes[2] == 0xd && bytes[3] == 0xc) { // The sender has shut down the socket bexitreadloop = true; socket.Close(); form1.DisplaySocketStatusMessage("Lost connection with sender"); break; } } catch (SocketException e) { socket.Blocking = true; int code = e.ErrorCode; if (e.ErrorCode != WSAEWOULDBLOCK) { bexitreadloop = true; socket.Close(); form1.DisplaySocketStatusMessage("Socket error on read"); break; } }

          A Offline
          A Offline
          Alper Camel
          wrote on last edited by
          #4

          This is Great, Now I'm working on it. Thanks for this great reply... Alper

          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