socket: connection reset by server before client gets response

Frank Swarbrick infocat at earthlink.net
Sun Jul 8 08:59:55 CEST 2007


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



More information about the Python-list mailing list