socket: connection reset by server before client gets response

Discussion in 'Python' started by ahlongxp, Jul 7, 2007.

  1. ahlongxp

    ahlongxp Guest

    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,Northeastern University,China

    http://www.herofit.cn
    ahlongxp, Jul 7, 2007
    #1
    1. Advertising

  2. ahlongxp

    ahlongxp Guest

    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,Northeastern University,China
    ://www.herofit.cn
    ahlongxp, Jul 7, 2007
    #2
    1. Advertising

  3. 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,Northeastern University,China
    > ://www.herofit.cn
    >
    >


    Post the code.

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


    ==irmen
    Irmen de Jong, Jul 8, 2007
    #3
  4. ahlongxp

    ahlongxp Guest

    > 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(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 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(32,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(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    for i in range(12):
    print "time %d" % i
    s.send('0123456789')
    #data = s.recv(1024)
    #print "data %d" %i, data
    #s.shutdown(socket.SHUT_WR)#no more write
    data=s.recv(1024)
    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('0123456789')
    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,Northeastern University,China

    http://www.herofit.cn
    ahlongxp, Jul 8, 2007
    #4
  5. 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(socket.AF_INET, socket.SOCK_STREAM)
    > s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 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(32,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(socket.AF_INET, socket.SOCK_STREAM)
    > s.connect((HOST, PORT))
    > for i in range(12):
    > print "time %d" % i
    > s.send('0123456789')
    > #data = s.recv(1024)
    > #print "data %d" %i, data
    > #s.shutdown(socket.SHUT_WR)#no more write
    > data=s.recv(1024)
    > 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('0123456789')
    > 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('0123456789')
    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('0123456789')
    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
    Frank Swarbrick, Jul 8, 2007
    #5
  6. ahlongxp

    ahlongxp Guest


    > 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('0123456789')
    > 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,Northeastern University,China

    http://www.herofit.cn
    ahlongxp, Jul 8, 2007
    #6
  7. "ahlongxp" <> wrote:

    > 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
    Hendrik van Rooyen, Jul 8, 2007
    #7
  8. ahlongxp

    ahlongxp Guest


    > 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.
    ahlongxp, Jul 8, 2007
    #8
  9. "ahlongxp" <> wrote:


    >
    > > 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
    Hendrik van Rooyen, Jul 9, 2007
    #9
  10. ahlongxp

    ahlongxp Guest

    > 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,Northeastern University,China

    http://www.herofit.cn
    ahlongxp, Jul 9, 2007
    #10
  11. 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.
    Adriano Varoli Piazza, Jul 9, 2007
    #11
  12. ahlongxp

    ahlongxp Guest

    On Jul 9, 4:30 pm, Adriano Varoli Piazza <> wrote:

    >
    > 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.
    ahlongxp, Jul 9, 2007
    #12
  13. 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
    Adriano Varoli Piazza, Jul 9, 2007
    #13
  14. ahlongxp

    ahlongxp Guest

    On Jul 9, 7:03 pm, Adriano Varoli Piazza <> wrote:
    > 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,Northeastern University,China

    http://www.herofit.cn
    ahlongxp, Jul 9, 2007
    #14
  15. "ahlongxp" <ahlongxp@....l.com> wrote:

    > 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
    Hendrik van Rooyen, Jul 10, 2007
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Klemitz
    Replies:
    1
    Views:
    957
  2. Helene Unterwieser
    Replies:
    14
    Views:
    3,664
    Esmond Pitt
    Jul 3, 2007
  3. James Edward Gray II

    Socket#gets and Socket#puts

    James Edward Gray II, Nov 25, 2004, in forum: Ruby
    Replies:
    1
    Views:
    121
    James Edward Gray II
    Nov 25, 2004
  4. Scott Cole
    Replies:
    3
    Views:
    247
    Brian Candler
    Sep 28, 2010
  5. loial
    Replies:
    1
    Views:
    86
    Andrew Berg
    May 21, 2013
Loading...

Share This Page