Problems with socket

Rolf Wester wester at ilt.fhg.de
Fri Nov 10 11:49:39 CET 2000


Hi,

I have a little problem with using sockets. When I call recv(n) on a
socketobject
with n > (number of bytes sent by the client), recv(n) returns. When I
choose n to be
smaller than the number of bytes sent by the client recv(n) returns with
n bytes read.
When I repeat calling recv(n) until there are no more data to be read,
recv(n) doesn't return.
Can I change this behaviour, so that recv(n) returns even when it is
called in case that there
are no more bytes to be read. Below you will find a simple server and
client that demonstrate
taht behaviour (copied from the Python-docs and changed a little bit).

-------------------------------------------------------------------------------------

# 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.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
#conn.setsockopt(socket.SOMAXCONN)
print 'Connected by', addr
while 1:
    data = conn.read(5) # returns Hello
    print data
    data = conn.recv(5) # returns , word
    print data
    data = conn.recv(5) # return ld
    print data
    data = conn.recv(5) # at this point recv(5) doesn't return
    print data
    if not data: break
    conn.send(data)
conn.close()

-------------------------------------------------------------
# Echo client program
import socket

HOST = '137.226.47.136'    # 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))
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', `data`




More information about the Python-list mailing list