socket: connection reset by server before client gets response

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ahlongxp

    socket: connection reset by server before client gets response

    Hi, everyone,

    I'm implementing a simple client/server protocol.

    Now I've got a situation:
    client will send server command,header paires and optionally body.
    server checks headers and decides whether to accept(read) the body.
    if server decided to throw(dump) the request's body, it'll send back a
    response message, such as "resource already exists" and close the
    connection.
    the problem is, client will never get the response but a "peer reset"
    exception.


    any comments or help will be appreciated.

    --
    ahlongxp

    Software College,Northea stern University,Chin a
    ahlongxp@gmail. com


  • ahlongxp

    #2
    Re: socket: connection reset by server before client gets response

    me again.

    "Connection reset by peer" happens about one in fifth.
    I'm using python 2.5.1 and ubuntu 7.04.

    --
    ahlongxp

    Software College,Northea stern University,Chin a
    ahlon...@gmail. comhttp://www.herofit.cn


    Comment

    • Irmen de Jong

      #3
      Re: socket: connection reset by server before client gets response

      ahlongxp wrote:
      me again.
      >
      "Connection reset by peer" happens about one in fifth.
      I'm using python 2.5.1 and ubuntu 7.04.
      >
      --
      ahlongxp
      >
      Software College,Northea stern University,Chin a
      ahlon...@gmail. comhttp://www.herofit.cn
      >
      >
      Post the code.

      Without it we can only help when our magic crystal balls are back from service.


      ==irmen

      Comment

      • ahlongxp

        #4
        Re: socket: connection reset by server before client gets response

        Post the code.
        ok.
        here is the code:

        # Echo server program
        import socket

        HOST = '' # Symbolic name meaning the local host
        PORT = 50007 # Arbitrary non-privileged port
        s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
        s.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)
        s.bind((HOST, PORT))
        s.listen(1)
        conn, addr = s.accept()
        print 'Connected by', addr
        conn.settimeout (1)
        toread = 99
        retrytime = 0
        reads = 0
        while reads < toread and retrytime < 10:
        try:
        data = conn.recv(min(3 2,toread-reads))
        if not data: continue
        print data
        reads += len(data)
        except:
        retrytime += 1
        print "timeout %d" % retrytime
        continue
        if reads == toread:
        conn.send("OK")
        else:
        conn.send("NOT OK")
        conn.close()

        *************** *I'm the separate
        line*********** *************** *************** ****

        # Echo client program
        import socket

        HOST = 'localhost' # The remote host
        PORT = 50007 # The same port as used by the server
        s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
        s.connect((HOST , PORT))
        for i in range(12):
        print "time %d" % i
        s.send('0123456 789')
        #data = s.recv(1024)
        #print "data %d" %i, data
        #s.shutdown(soc ket.SHUT_WR)#no more write
        data=s.recv(102 4)
        s.close()
        print 'Received', repr(data)

        client is supposed to get the response, either "OK" or "NOT OK".
        but the fact is, client gets "Connection reset by peer" (as shown
        below) about one in fifth.
        ----------------------------------
        Traceback (most recent call last):
        File "c.py", line 10, in <module>
        s.send('0123456 789')
        socket.error: (104, 'Connection reset by peer')
        ----------------------------------

        anyway, server is doing well all the time.

        any comments on the design or implementation will be greatly
        appreciated.

        --
        ahlongxp

        Software College,Northea stern University,Chin a
        ahlongxp@gmail. com


        Comment

        • Frank Swarbrick

          #5
          Re: socket: connection reset by server before client gets response

          ahlongxp wrote:
          >Post the code.
          ok.
          here is the code:
          >
          # Echo server program
          import socket
          >
          HOST = '' # Symbolic name meaning the local host
          PORT = 50007 # Arbitrary non-privileged port
          s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
          s.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)
          s.bind((HOST, PORT))
          s.listen(1)
          conn, addr = s.accept()
          print 'Connected by', addr
          conn.settimeout (1)
          toread = 99
          retrytime = 0
          reads = 0
          while reads < toread and retrytime < 10:
          try:
          data = conn.recv(min(3 2,toread-reads))
          if not data: continue
          print data
          reads += len(data)
          except:
          retrytime += 1
          print "timeout %d" % retrytime
          continue
          if reads == toread:
          conn.send("OK")
          else:
          conn.send("NOT OK")
          conn.close()
          >
          *************** *I'm the separate
          line*********** *************** *************** ****
          >
          # Echo client program
          import socket
          >
          HOST = 'localhost' # The remote host
          PORT = 50007 # The same port as used by the server
          s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
          s.connect((HOST , PORT))
          for i in range(12):
          print "time %d" % i
          s.send('0123456 789')
          #data = s.recv(1024)
          #print "data %d" %i, data
          #s.shutdown(soc ket.SHUT_WR)#no more write
          data=s.recv(102 4)
          s.close()
          print 'Received', repr(data)
          >
          client is supposed to get the response, either "OK" or "NOT OK".
          but the fact is, client gets "Connection reset by peer" (as shown
          below) about one in fifth.
          ----------------------------------
          Traceback (most recent call last):
          File "c.py", line 10, in <module>
          s.send('0123456 789')
          socket.error: (104, 'Connection reset by peer')
          ----------------------------------
          >
          anyway, server is doing well all the time.
          >
          any comments on the design or implementation will be greatly
          appreciated.
          So, umm, what exactly are you trying to accomplish?
          Here are the results I'm getting:

          Server:
          Connected by ('127.0.0.1', 53536)
          0123456789
          0123456789
          0123456789
          0123456789
          0123456789
          0123456789
          0123456789
          0123456789
          0123456789
          012345678

          Client:
          time 0
          time 1
          time 2
          time 3
          time 4
          time 5
          time 6
          time 7
          time 8
          time 9
          time 10
          time 11
          Traceback (most recent call last):
          File "echo_c.py" , line 10, in <module>
          s.send('0123456 789')
          socket.error: (32, 'Broken pipe')

          It looks like what is happening is the server only accepts 99 bytes. It
          then does the send and the close.

          The client wants to send 120 bytes, 10 bytes at a time. By the time is
          does the 12th send the server has already finished, closing its socket
          and exiting. So when the client attempts send #12 the socket is already
          closed. Thus the error.

          I'm not sure why you are getting the 'connection reset' instead of
          'broken pipe'. Probably different OS. (I'm using Mac OS X 10.4.10.)

          Anyway, I changed your code slightly, wrapping the send in a try/except
          block like this:
          try:
          s.send('0123456 789')
          except socket.error ,e:
          print "Socket Error:", e
          break

          Now here are my results for the client:
          time 0
          time 1
          time 2
          time 3
          time 4
          time 5
          time 6
          time 7
          time 8
          time 9
          time 10
          time 11
          error: (32, 'Broken pipe')
          Received 'OK'

          The way you had it the program crashed before it could do the receive.

          Frank

          Comment

          • ahlongxp

            #6
            Re: socket: connection reset by server before client gets response

            So, umm, what exactly are you trying to accomplish?
            It looks like what is happening is the server only accepts 99 bytes. It
            then does the send and the close.
            yes. What I want is that, server sends response to client and closes
            connection when it feels recieving enough information, and make sure
            at the same time ,client will definitely get the response from server.
            The client wants to send 120 bytes, 10 bytes at a time. By the time is
            does the 12th send the server has already finished, closing its socket
            and exiting. So when the client attempts send #12 the socket is already
            closed. Thus the error.
            >
            I'm not sure why you are getting the 'connection reset' instead of
            'broken pipe'. Probably different OS. (I'm using Mac OS X 10.4.10.)
            >
            as I saied before, running results will varies. And most of the time
            they are working quite well. But that's not enough.
            Anyway, I changed your code slightly, wrapping the send in a try/except
            block like this:
            try:
            s.send('0123456 789')
            except socket.error ,e:
            print "Socket Error:", e
            break
            >
            Now here are my results for the client:
            time 0
            time 1
            time 2
            time 3
            time 4
            time 5
            time 6
            time 7
            time 8
            time 9
            time 10
            time 11
            error: (32, 'Broken pipe')
            Received 'OK'
            This really helped.
            Now I know the client will still get the response even under
            'Connection reset by peer' or 'Broken pipe'.

            The way you had it the program crashed before it could do the receive.
            >
            Frank
            Frank, thanks a lot.

            --
            ahlongxp

            Software College,Northea stern University,Chin a
            ahlongxp@gmail. com


            Comment

            • Hendrik van Rooyen

              #7
              Re: socket: connection reset by server before client gets response

              "ahlongxp" <ah...p@gmail.c omwrote:
              me again.
              >
              "Connection reset by peer" happens about one in fifth.
              I'm using python 2.5.1 and ubuntu 7.04.
              >
              Try to wait a while in the server thread, after sending the
              message before closing the connection, to give the message
              time to get transmitted.

              time.sleep(0.5) should do it...

              - Hendrik

              Comment

              • ahlongxp

                #8
                Re: socket: connection reset by server before client gets response

                Try to wait a while in the server thread, after sending the
                message before closing the connection, to give the message
                time to get transmitted.
                >
                time.sleep(0.5) should do it...
                >
                - Hendrik
                OMG, it works.
                I can't believe the problem can be solved so easily.

                Thanks very much.

                Comment

                • Hendrik van Rooyen

                  #9
                  Re: socket: connection reset by server before client gets response

                  "ahlongxp" <ah...p@gmail.c omwrote:

                  >
                  Try to wait a while in the server thread, after sending the
                  message before closing the connection, to give the message
                  time to get transmitted.

                  time.sleep(0.5) should do it...

                  - Hendrik
                  >
                  OMG, it works.
                  I can't believe the problem can be solved so easily.
                  >
                  Thanks very much.
                  It's a pleasure.

                  Sometimes I think that all would be programmers should be
                  forced to write a "Hello World" to transmit out of a serial port
                  in assembler on hardware that carries no OS - just to teach
                  them about interrupts and time.

                  I would require them to hand assemble the code too, to make
                  them appreciate what a compiler or an interpreter does.

                  And if you fail the test, you get taken out and fed to the
                  sacred crocodiles.

                  - Hendrik

                  --
                  For training with a bite, enrol now in Heavy Henry's
                  Wholesome Hackadamy for Precocious Programmers



                  Comment

                  • ahlongxp

                    #10
                    Re: socket: connection reset by server before client gets response

                    It's a pleasure.
                    >
                    Sometimes I think that all would be programmers should be
                    forced to write a "Hello World" to transmit out of a serial port
                    in assembler on hardware that carries no OS - just to teach
                    them about interrupts and time.
                    >
                    I would require them to hand assemble the code too, to make
                    them appreciate what a compiler or an interpreter does.
                    >
                    Then there will be more crocodiles than programmers.
                    And if you fail the test, you get taken out and fed to the
                    sacred crocodiles.
                    >
                    - Hendrik
                    >
                    --
                    For training with a bite, enrol now in Heavy Henry's
                    Wholesome Hackadamy for Precocious Programmers
                    I feel a little embarrassed now.


                    --
                    ahlongxp

                    Software College,Northea stern University,Chin a
                    ahlongxp@gmail. com


                    Comment

                    • Adriano Varoli Piazza

                      #11
                      Re: socket: connection reset by server before client gets response

                      Hendrik van Rooyen wrote:
                      Sometimes I think that all would be programmers should be
                      forced to write a "Hello World" to transmit out of a serial port
                      in assembler on hardware that carries no OS - just to teach
                      them about interrupts and time.
                      >
                      I would require them to hand assemble the code too, to make
                      them appreciate what a compiler or an interpreter does.
                      >
                      And if you fail the test, you get taken out and fed to the
                      sacred crocodiles.
                      >
                      - Hendrik
                      >
                      --
                      For training with a bite, enrol now in Heavy Henry's
                      Wholesome Hackadamy for Precocious Programmers
                      Gives a whole new meaning to chomp(), byte, nybble, and more :)
                      I wholeheartedly endorse this effort. I'm sick of reading about
                      students from WTF University (check thedailywtf.com if you don't know,
                      but beware. There be dragons).

                      --
                      Adriano
                      Once bitten, one arm less to code snafus with.

                      Comment

                      • ahlongxp

                        #12
                        Re: socket: connection reset by server before client gets response

                        On Jul 9, 4:30 pm, Adriano Varoli Piazza <mora...@gmail. comwrote:
                        >
                        Gives a whole new meaning to chomp(), byte, nybble, and more :)
                        I wholeheartedly endorse this effort. I'm sick of reading about
                        students from WTF University (check thedailywtf.com if you don't know,
                        but beware. There be dragons).
                        >
                        --
                        Adriano
                        Once bitten, one arm less to code snafus with.
                        I feel officially offended.

                        Comment

                        • Adriano Varoli Piazza

                          #13
                          Re: socket: connection reset by server before client gets response

                          ahlongxp wrote:
                          I feel officially offended.
                          I didn't intend to offend you, I was joking. I apologise in any case.
                          There's a few things to be said, though:

                          As per your message in another thread, it isn't that you don't express
                          yourself clearly in English, but that you were too quick to claim a
                          standard function was buggy without first thinking if your own code
                          could be buggy instead. That presumption isn't related to the language.

                          Second, if you do say that you aren't comfortable in English, try to
                          assume that people aren't trying to insult you by default. I was
                          speaking generallyin my message.

                          Third, this is usenet. Although probably not in this newsgroup, if you
                          continue to make the assumptions I and others pointed out, you will
                          receive this kind of answer. Perhaps with much more insult than right
                          now. So it is a great idea to grow a thick skin.

                          --
                          Saludos
                          Adriano

                          Comment

                          • ahlongxp

                            #14
                            Re: socket: connection reset by server before client gets response

                            On Jul 9, 7:03 pm, Adriano Varoli Piazza <mora...@gmail. comwrote:
                            ahlongxp wrote:
                            I feel officially offended.
                            >
                            I didn't intend to offend you, I was joking. I apologise in any case.
                            There's a few things to be said, though:
                            >
                            As per your message in another thread, it isn't that you don't express
                            yourself clearly in English, but that you were too quick to claim a
                            standard function was buggy without first thinking if your own code
                            could be buggy instead. That presumption isn't related to the language.
                            >
                            Second, if you do say that you aren't comfortable in English, try to
                            assume that people aren't trying to insult you by default. I was
                            speaking generallyin my message.
                            >
                            Third, this is usenet. Although probably not in this newsgroup, if you
                            continue to make the assumptions I and others pointed out, you will
                            receive this kind of answer. Perhaps with much more insult than right
                            now. So it is a great idea to grow a thick skin.
                            >
                            --
                            Saludos
                            Adriano
                            You don't have to apologise. I was joking too.

                            But I know I have to change my way of thinking and questioning.
                            I'll consider more carefully before speaking.
                            I hope I can talk like you very soon.

                            Thanks.

                            --
                            ahlongxp

                            Software College,Northea stern University,Chin a
                            ahlongxp@gmail. com


                            Comment

                            • Hendrik van Rooyen

                              #15
                              Re: socket: connection reset by server before client gets response


                              "ahlongxp" <ahlongxp@....l .comwrote:
                              I feel a little embarrassed now.
                              There is nothing to be embarrassed about.
                              Experience is a thing that is hard won.

                              As someone once said:

                              "no Pain, no Gain"

                              - Hendrik

                              Comment

                              Working...