Problems with socket

Rolf Wester wester at
Fri Nov 10 11:49:39 CET 2000


I have a little problem with using sockets. When I call recv(n) on a
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))
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = # 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

# Echo client program
import socket

HOST = ''    # 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)
print 'Received', `data`

More information about the Python-list mailing list