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 / C++ / MFC
  4. Socket

Socket

Scheduled Pinned Locked Moved C / C++ / MFC
questionsysadmintutorial
7 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.
  • C Offline
    C Offline
    Castello07
    wrote on last edited by
    #1

    My question is quite simple but I can't solve it. I have create a socket tcp, how can I know when the socket go down due to external cause? For example when the network is down etc.... Use members like CAsynchSocket::OnClose, isn't correct because this member is called only when my application close the socket! ...however how can i do? Thanks to all!!

    R 1 Reply Last reply
    0
    • C Castello07

      My question is quite simple but I can't solve it. I have create a socket tcp, how can I know when the socket go down due to external cause? For example when the network is down etc.... Use members like CAsynchSocket::OnClose, isn't correct because this member is called only when my application close the socket! ...however how can i do? Thanks to all!!

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      CAsyncSocket::OnClose() is the function that should be overridden for this purpose. One of the error codes that could be represented with the nErrorCode parameter is WSAENETDOWN. I suspect that there would be some kind of a timeout before a net down state is detected. -- Roger


      It's suppose to be hard, otherwise anybody could do it!

      C 1 Reply Last reply
      0
      • R Roger Stoltz

        CAsyncSocket::OnClose() is the function that should be overridden for this purpose. One of the error codes that could be represented with the nErrorCode parameter is WSAENETDOWN. I suspect that there would be some kind of a timeout before a net down state is detected. -- Roger


        It's suppose to be hard, otherwise anybody could do it!

        C Offline
        C Offline
        Castello07
        wrote on last edited by
        #3

        I have tried to use it, and for test, I unplug the utp cable and OnClose() wasn't called...Have I to set some particular option when I create the socket?? When I start the socket I only do this: if (!m_pSocket->Create()) throw 1; // Connect to our destination. if (!m_pSocket->Connect(strAddress, port)){ throw 2;

        R 1 Reply Last reply
        0
        • C Castello07

          I have tried to use it, and for test, I unplug the utp cable and OnClose() wasn't called...Have I to set some particular option when I create the socket?? When I start the socket I only do this: if (!m_pSocket->Create()) throw 1; // Connect to our destination. if (!m_pSocket->Connect(strAddress, port)){ throw 2;

          R Offline
          R Offline
          Roger Stoltz
          wrote on last edited by
          #4

          Sorry, I misunderstood you. :-> I thought you meant that you want to detect a net down state when you're are closing the socket. I don't think it's possible to detect a net down state using sockets if you're not operating on socket. Each operation will fail if the net is down with WSAENETDOWN. Of course it could be detected if you use some kind of keep-alive messages that are periodically sent on the socket. The Send operation will fail in such case. Hope this helps -- Roger


          It's suppose to be hard, otherwise anybody could do it!

          C 1 Reply Last reply
          0
          • R Roger Stoltz

            Sorry, I misunderstood you. :-> I thought you meant that you want to detect a net down state when you're are closing the socket. I don't think it's possible to detect a net down state using sockets if you're not operating on socket. Each operation will fail if the net is down with WSAENETDOWN. Of course it could be detected if you use some kind of keep-alive messages that are periodically sent on the socket. The Send operation will fail in such case. Hope this helps -- Roger


            It's suppose to be hard, otherwise anybody could do it!

            C Offline
            C Offline
            Castello07
            wrote on last edited by
            #5

            Ok I suppose that i have to set with setsockopt the keep alive message....and after i control the nErrocode in OnSend is it correct?? however thanks a lot for the answer!!! -- modified at 5:59 Friday 5th May, 2006

            C 1 Reply Last reply
            0
            • C Castello07

              Ok I suppose that i have to set with setsockopt the keep alive message....and after i control the nErrocode in OnSend is it correct?? however thanks a lot for the answer!!! -- modified at 5:59 Friday 5th May, 2006

              C Offline
              C Offline
              Castello07
              wrote on last edited by
              #6

              I try to use setSocketOpt with options SO_KEEPALIVE in socket intialization phase. I stay in OnSend and Control the nErrorCode but OnSend is never called!!

              M 1 Reply Last reply
              0
              • C Castello07

                I try to use setSocketOpt with options SO_KEEPALIVE in socket intialization phase. I stay in OnSend and Control the nErrorCode but OnSend is never called!!

                M Offline
                M Offline
                Moak
                wrote on last edited by
                #7

                TCP does not offer you quick detection of all kind of connection interruptions... it was designed to survive network interruption, what you are looking for is a application specific keep-alive mechanism, connection test or timeout. It really depends on the application what you need. E.g. for a simple file server it could be enough to close the socket when no data has been sent/received for a while. For an interactive application you may want to test the connection in regular intervalls. Fo example have first an OnClose() to detect when a socket is reported down and secondly send periodically some bytes to the remote socket. Hope it helps. A good book to read is 'Effective TCP/IP Programming'. /M

                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