how to know if socket is still connected

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nephish@xit.net

    how to know if socket is still connected

    lo there,
    i have a simple app that connects to a socket to get info from a server

    i looks like this

    serverhost = 'xxx.xxx.xxx.xx x'
    serverport = 9520
    aeris_sockobj = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    aeris_sockobj.c onnect((serverh ost,serverport) )

    while 1:
    do this or that with socket,
    send and receive info.
    yadda yadda yadda


    works well, but sometimes the server drops the connection.
    so, what i need is something that will let me know if the connection
    is still ok, if not will reconnect.

    what i thought, since it only lets you connect on a certain port one at
    a time,
    that i could use a try-except to connect every time, if it could not
    connect (because it already is) then i would just continue on. But if
    it is not connected, it would reconnect.
    that is what brings me here. Seems like it would work, but is there a
    better way ? this kinda seems like a dirty hack.

    any opinions ?

    thanks.

  • Grant Edwards

    #2
    Re: how to know if socket is still connected

    On 2006-07-16, nephish@xit.net <nephish@xit.ne twrote:
    serverhost = 'xxx.xxx.xxx.xx x'
    serverport = 9520
    aeris_sockobj = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    aeris_sockobj.c onnect((serverh ost,serverport) )
    >
    while 1:
    do this or that with socket,
    send and receive info.
    yadda yadda yadda
    >
    works well, but sometimes the server drops the connection. so,
    what i need is something that will let me know if the
    connection is still ok, if not will reconnect.
    If the server has closed the connection, then a recv() on the
    socket will return an empty string "", and a send() on the
    socket will raise an exception.
    what i thought, since it only lets you connect on a certain
    port one at a time, that i could use a try-except to connect
    every time, if it could not connect (because it already is)
    then i would just continue on. But if it is not connected, it
    would reconnect. that is what brings me here. Seems like it
    would work, but is there a better way?
    I don't see why the normal send() and recv() semantics aren't
    sufficient.

    --
    Grant Edwards grante Yow! I'm an East Side
    at TYPE...
    visi.com

    Comment

    • nephish@xit.net

      #3
      Re: how to know if socket is still connected


      Grant Edwards wrote:
      On 2006-07-16, nephish@xit.net <nephish@xit.ne twrote:
      >
      serverhost = 'xxx.xxx.xxx.xx x'
      serverport = 9520
      aeris_sockobj = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
      aeris_sockobj.c onnect((serverh ost,serverport) )

      while 1:
      do this or that with socket,
      send and receive info.
      yadda yadda yadda

      works well, but sometimes the server drops the connection. so,
      what i need is something that will let me know if the
      connection is still ok, if not will reconnect.
      >
      If the server has closed the connection, then a recv() on the
      socket will return an empty string "", and a send() on the
      socket will raise an exception.
      >
      what i thought, since it only lets you connect on a certain
      port one at a time, that i could use a try-except to connect
      every time, if it could not connect (because it already is)
      then i would just continue on. But if it is not connected, it
      would reconnect. that is what brings me here. Seems like it
      would work, but is there a better way?
      >
      I don't see why the normal send() and recv() semantics aren't
      sufficient.
      >
      --
      Grant Edwards grante Yow! I'm an East Side
      at TYPE...
      visi.com


      like this ?
      databack = aeris_sockobj.r ecv(2048)
      if databack:
      view_msg = 'caught request acknowlage %s bytes \n' %
      len(databack)
      else:
      view_msg = 'fail to recieve data from aeris server\n'

      then put the reconnect in the else: block ?

      thanks


      thanks

      Comment

      • Grant Edwards

        #4
        Re: how to know if socket is still connected

        On 2006-07-16, nephish@xit.net <nephish@xit.ne twrote:
        >If the server has closed the connection, then a recv() on the
        >socket will return an empty string "", and a send() on the
        >socket will raise an exception.
        like this ?
        databack = aeris_sockobj.r ecv(2048)
        if databack:
        view_msg = 'caught request acknowlage %s bytes \n' %
        len(databack)
        else:
        view_msg = 'fail to recieve data from aeris server\n'
        >
        then put the reconnect in the else: block ?
        Yes, if the problem is that the host closes the connection,
        that should work. Modulo the broken indentation and
        line-wrapping. ;)

        --
        Grant Edwards grante Yow! My mind is a potato
        at field...
        visi.com

        Comment

        • nephish@xit.net

          #5
          Re: how to know if socket is still connected

          cool enough, thanks !

          -sk


          Grant Edwards wrote:
          On 2006-07-16, nephish@xit.net <nephish@xit.ne twrote:
          >
          If the server has closed the connection, then a recv() on the
          socket will return an empty string "", and a send() on the
          socket will raise an exception.
          >
          like this ?
          databack = aeris_sockobj.r ecv(2048)
          if databack:
          view_msg = 'caught request acknowlage %s bytes \n' %
          len(databack)
          else:
          view_msg = 'fail to recieve data from aeris server\n'

          then put the reconnect in the else: block ?
          >
          Yes, if the problem is that the host closes the connection,
          that should work. Modulo the broken indentation and
          line-wrapping. ;)
          >
          --
          Grant Edwards grante Yow! My mind is a potato
          at field...
          visi.com

          Comment

          • Cameron Laird

            #6
            Re: how to know if socket is still connected

            In article <12blgl1t9egsqb 8@corp.supernew s.com>,
            Grant Edwards <grante@visi.co mwrote:
            >On 2006-07-16, nephish@xit.net <nephish@xit.ne twrote:
            >
            >serverhost = 'xxx.xxx.xxx.xx x'
            >serverport = 9520
            >aeris_sockob j = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
            >aeris_sockobj. connect((server host,serverport ))
            >>
            >while 1:
            > do this or that with socket,
            > send and receive info.
            > yadda yadda yadda
            >>
            >works well, but sometimes the server drops the connection. so,
            >what i need is something that will let me know if the
            >connection is still ok, if not will reconnect.
            >
            >If the server has closed the connection, then a recv() on the
            >socket will return an empty string "", and a send() on the
            >socket will raise an exception.
            >
            >what i thought, since it only lets you connect on a certain
            >port one at a time, that i could use a try-except to connect
            >every time, if it could not connect (because it already is)
            >then i would just continue on. But if it is not connected, it
            >would reconnect. that is what brings me here. Seems like it
            >would work, but is there a better way?
            >
            >I don't see why the normal send() and recv() semantics aren't
            >sufficient.

            Comment

            • Grant Edwards

              #7
              Re: how to know if socket is still connected

              On 2006-07-17, Cameron Laird <claird@lairds. uswrote:
              >>works well, but sometimes the server drops the connection. so,
              >>what i need is something that will let me know if the
              >>connection is still ok, if not will reconnect.
              >>
              >>If the server has closed the connection, then a recv() on the
              >>socket will return an empty string "", and a send() on the
              >>socket will raise an exception.
              >>
              >>what i thought, since it only lets you connect on a certain
              >>port one at a time, that i could use a try-except to connect
              >>every time, if it could not connect (because it already is)
              >>then i would just continue on. But if it is not connected, it
              >>would reconnect. that is what brings me here. Seems like it
              >>would work, but is there a better way?
              >>
              >>I don't see why the normal send() and recv() semantics aren't
              >>sufficient.
              .
              .
              .
              Often normal send() and recv() semantics have been mistaught.
              An alert alien, looking at other common APIs in isolation,
              might reasonably wonder whether there is some sort of
              still_ok_to_use () sort of check as part of TCP. As it happens,
              of course, that doesn't fit with the rest of socket networking,
              which takes the "modernist" approach of trying send() or recv(),
              and reporting any exception.
              On most Unices there are some obscure API features that can be
              used to generate a SIGPIPE under some vaguely specified error
              conditions (e.g. TCP keepalive timeout). I've only read about
              them and never tried to use them, since I couldn't see anything
              in the description of the features that was any benefit over
              the nomral send() and recv() usage.

              --
              Grant Edwards grante Yow! Xerox your lunch
              at and file it under "sex
              visi.com offenders"!

              Comment

              • nephish@xit.net

                #8
                Re: how to know if socket is still connected

                hey there, i have a question about this solution.
                if i have a
                message = socket.recv()
                in the script, and the socket connection drops, will the
                socket.recv() just wait forever for something to come across
                the internet port? or will it know if the connection is dropped?
                thanks.
                -sk


                Grant Edwards wrote:
                On 2006-07-17, Cameron Laird <claird@lairds. uswrote:
                >
                >works well, but sometimes the server drops the connection. so,
                >what i need is something that will let me know if the
                >connection is still ok, if not will reconnect.
                >
                >If the server has closed the connection, then a recv() on the
                >socket will return an empty string "", and a send() on the
                >socket will raise an exception.
                >
                >what i thought, since it only lets you connect on a certain
                >port one at a time, that i could use a try-except to connect
                >every time, if it could not connect (because it already is)
                >then i would just continue on. But if it is not connected, it
                >would reconnect. that is what brings me here. Seems like it
                >would work, but is there a better way?
                >
                >I don't see why the normal send() and recv() semantics aren't
                >sufficient.
                .
                .
                .
                Often normal send() and recv() semantics have been mistaught.
                An alert alien, looking at other common APIs in isolation,
                might reasonably wonder whether there is some sort of
                still_ok_to_use () sort of check as part of TCP. As it happens,
                of course, that doesn't fit with the rest of socket networking,
                which takes the "modernist" approach of trying send() or recv(),
                and reporting any exception.
                >
                On most Unices there are some obscure API features that can be
                used to generate a SIGPIPE under some vaguely specified error
                conditions (e.g. TCP keepalive timeout). I've only read about
                them and never tried to use them, since I couldn't see anything
                in the description of the features that was any benefit over
                the nomral send() and recv() usage.
                >
                --
                Grant Edwards grante Yow! Xerox your lunch
                at and file it under "sex
                visi.com offenders"!

                Comment

                • Grant Edwards

                  #9
                  Re: how to know if socket is still connected

                  On 2006-07-17, nephish@xit.net <nephish@xit.ne twrote:
                  hey there, i have a question about this solution.
                  if i have a
                  message = socket.recv()
                  in the script, and the socket connection drops, will the
                  socket.recv() just wait forever for something to come across
                  the internet port? or will it know if the connection is dropped?
                  As I said before, if the socket is closed by the remote host,
                  recv() will return "".

                  I don't know what you mean by "drops" and "dropped" in this
                  context. If you want a useful answer to your question, you'll
                  have to define your terms precisely.

                  --
                  Grant Edwards grante Yow! I'm CONTROLLED by
                  at the CIA!! EVERYONE is
                  visi.com controlled by the CIA!!

                  Comment

                  • nephish@xit.net

                    #10
                    Re: how to know if socket is still connected

                    oh, sorry, what i mean by dropped is that the server i am connecting to
                    can close the connection. If that happens, i need to know about it.
                    i also need to know about it if the server i am connecting to just
                    dies.

                    if recv() returns "" is that the same as NONE ?
                    again, sorry, i am still kinda new at this.
                    I mean can the value be tested true or false?

                    thanks
                    -sk


                    Grant Edwards wrote:
                    On 2006-07-17, nephish@xit.net <nephish@xit.ne twrote:
                    >
                    hey there, i have a question about this solution.
                    if i have a
                    message = socket.recv()
                    in the script, and the socket connection drops, will the
                    socket.recv() just wait forever for something to come across
                    the internet port? or will it know if the connection is dropped?
                    >
                    As I said before, if the socket is closed by the remote host,
                    recv() will return "".
                    >
                    I don't know what you mean by "drops" and "dropped" in this
                    context. If you want a useful answer to your question, you'll
                    have to define your terms precisely.
                    >
                    --
                    Grant Edwards grante Yow! I'm CONTROLLED by
                    at the CIA!! EVERYONE is
                    visi.com controlled by the CIA!!

                    Comment

                    • Grant Edwards

                      #11
                      Re: how to know if socket is still connected

                      On 2006-07-17, nephish@xit.net <nephish@xit.ne twrote:
                      oh, sorry, what i mean by dropped is that the server i am
                      connecting to can close the connection.
                      Then recv() will return "" and send() will raise an exception.
                      If that happens, i need to know about it. i also need to know
                      about it if the server i am connecting to just dies.
                      Again, you have to define what you mean by "server just dies".

                      If the server _application_ crashes or exits, then the OS will
                      close the socket and recv() will return "". If somebody powers
                      down the server without warning, or if the server OS crashes,
                      or if the Ethernet cable between the Internet and the server is
                      cut, then the socket will not be closed, and recv() will wait
                      forever[1].
                      if recv() returns "" is that the same as NONE ?
                      No. It's the empty string (also spelt '').
                      I mean can the value be tested true or false?
                      Yes. The empty string is false, all non-empty strings are
                      true.

                      [1] Unless you've enabled the TCP Keepalive feature, in which
                      case the socket will timeout in a couple hours and recv()
                      will return "".

                      --
                      Grant Edwards grante Yow! Now I am depressed...
                      at
                      visi.com

                      Comment

                      • nephish@xit.net

                        #12
                        Re: how to know if socket is still connected

                        If the server _application_ crashes or exits, then the OS will
                        close the socket and recv() will return "". If somebody powers
                        down the server without warning, or if the server OS crashes,
                        or if the Ethernet cable between the Internet and the server is
                        cut, then the socket will not be closed, and recv() will wait
                        forever[1].
                        Ok, yes all of the above is what i mean. Actually I am not too
                        concerned about a server os crash, or the cable being cut. But I have
                        had them close the connection on me, after which i just reconnect
                        (whenever i discover that its happened)
                        >[1] Unless you've enabled the TCP Keepalive feature, in which
                        case the socket will timeout in a couple hours and recv()
                        will return "".
                        if this is something that must be enabled, or is not enabled by
                        default, then it is not enabled.
                        so i should be pretty good.

                        thanks for all of your help, gents !
                        -sk




                        Grant Edwards wrote:
                        On 2006-07-17, nephish@xit.net <nephish@xit.ne twrote:
                        >
                        oh, sorry, what i mean by dropped is that the server i am
                        connecting to can close the connection.
                        >
                        Then recv() will return "" and send() will raise an exception.
                        >
                        If that happens, i need to know about it. i also need to know
                        about it if the server i am connecting to just dies.
                        >
                        Again, you have to define what you mean by "server just dies".
                        >
                        If the server _application_ crashes or exits, then the OS will
                        close the socket and recv() will return "". If somebody powers
                        down the server without warning, or if the server OS crashes,
                        or if the Ethernet cable between the Internet and the server is
                        cut, then the socket will not be closed, and recv() will wait
                        forever[1].
                        >
                        if recv() returns "" is that the same as NONE ?
                        >
                        No. It's the empty string (also spelt '').
                        >
                        I mean can the value be tested true or false?
                        >
                        Yes. The empty string is false, all non-empty strings are
                        true.
                        >
                        [1] Unless you've enabled the TCP Keepalive feature, in which
                        case the socket will timeout in a couple hours and recv()
                        will return "".
                        >
                        --
                        Grant Edwards grante Yow! Now I am depressed...
                        at
                        visi.com

                        Comment

                        • Grant Edwards

                          #13
                          Re: how to know if socket is still connected

                          On 2006-07-17, nephish@xit.net <nephish@xit.ne twrote:
                          >If the server _application_ crashes or exits, then the OS will
                          >close the socket and recv() will return "". If somebody powers
                          >down the server without warning, or if the server OS crashes,
                          >or if the Ethernet cable between the Internet and the server is
                          >cut, then the socket will not be closed, and recv() will wait
                          >forever[1].
                          >
                          Ok, yes all of the above is what i mean. Actually I am not too
                          concerned about a server os crash, or the cable being cut. But I have
                          had them close the connection on me, after which i just reconnect
                          (whenever i discover that its happened)
                          >
                          >>[1] Unless you've enabled the TCP Keepalive feature, in which
                          > case the socket will timeout in a couple hours and recv()
                          > will return "".
                          >
                          if this is something that must be enabled, or is not enabled by
                          default, then it is not enabled.
                          On all OSes with which I'm familiar it's disabled by default.
                          You use a socket object's setsockopt method to enable it:

                          s.setsockopt(so cket.SOL_TCP,so cket.SO_KEEPALI VE,True)

                          --
                          Grant Edwards grante Yow! Wow! Look!! A stray
                          at meatball!! Let's interview
                          visi.com it!

                          Comment

                          • nephish@xit.net

                            #14
                            Re: how to know if socket is still connected

                            ok, yeah, thats in my book.
                            thanks, and no, it isn't enabled.
                            thanks again for everything
                            -sk


                            Grant Edwards wrote:
                            On 2006-07-17, nephish@xit.net <nephish@xit.ne twrote:
                            If the server _application_ crashes or exits, then the OS will
                            close the socket and recv() will return "". If somebody powers
                            down the server without warning, or if the server OS crashes,
                            or if the Ethernet cable between the Internet and the server is
                            cut, then the socket will not be closed, and recv() will wait
                            forever[1].
                            Ok, yes all of the above is what i mean. Actually I am not too
                            concerned about a server os crash, or the cable being cut. But I have
                            had them close the connection on me, after which i just reconnect
                            (whenever i discover that its happened)
                            >[1] Unless you've enabled the TCP Keepalive feature, in which
                            case the socket will timeout in a couple hours and recv()
                            will return "".
                            if this is something that must be enabled, or is not enabled by
                            default, then it is not enabled.
                            >
                            On all OSes with which I'm familiar it's disabled by default.
                            You use a socket object's setsockopt method to enable it:
                            >
                            s.setsockopt(so cket.SOL_TCP,so cket.SO_KEEPALI VE,True)
                            >
                            --
                            Grant Edwards grante Yow! Wow! Look!! A stray
                            at meatball!! Let's interview
                            visi.com it!

                            Comment

                            • Laszlo Nagy

                              #15
                              Re: how to know if socket is still connected

                              nephish@xit.net írta:
                              ok, yeah, thats in my book.
                              thanks, and no, it isn't enabled.
                              thanks again for everything
                              -sk
                              >
                              Another hint: use select.select() on the socket before reading. It will
                              tell you if recv() will block or not. This way you can implement your
                              own async timeout and do something else in your program while waiting
                              for the connection to be available.

                              Best,

                              Laszlo

                              Comment

                              Working...