Socket recv(1) seems to block instead of returning end of file.

Grant Edwards grante at visi.com
Thu Aug 23 10:03:52 EDT 2007


On 2007-08-23, Hendrik van Rooyen <mail at microcorp.co.za> wrote:

> While doing a netstring implementation I noticed that if you
> build a record up using socket's recv(1), then when you close
> the remote end down, the recv(1) hangs,

I don't see that behavior running 2.4 on Gentoo.

> despite having a short time out of 0.1 set.

What time out?  A socket's recv method doesn't do timeouts.

> If however, you try to receive more than one char, (I tested
> with 3, did not try 2), then when you shut the remote end down
> you do not get a time out, but an empty string - the normal
> end of file, I suppose.
>
> Has anybody else seen this behaviour?

No.  recv(1) works fine for me (Python 2.4 under Gentoo).
Perhaps you could post a minimal example that doesn't work for
you?

------------------------------------------------------------------------
#!/usr/bin/python

#reader
import socket

HOST = ''                 # Symbolic name meaning the local host
PORT = 8765               # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1)
    print "rx:",len(data)
    if not data: break
conn.close()
------------------------------------------------------------------------


------------------------------------------------------------------------
#!/usr/bin/python

# writer
import socket,random

HOST = ''                 # Symbolic name meaning the local host
PORT = 8765               # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
print 'Connected to',((HOST,PORT))
for i in range(10):
    data = "abcdefghijklmnopqrstuvwxyz"[:random.randint(1,20)]
    s.send(data)
    print "tx:",len(data)
conn.close()
------------------------------------------------------------------------

-- 
Grant Edwards                   grante             Yow! Loni Anderson's hair
                                  at               should be LEGALIZED!!
                               visi.com            



More information about the Python-list mailing list