python blocking sockets, send returns immediately

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-03-12T12:58:47Z Indexed on 2010/03/13 0:47 UTC
Read the original article Hit count: 616

Filed under:
|
|
|
|

Hi,

I am writing a multithreaded socket application in Python using the socket module. the server listens for connections and when it gets one it spawns a thread for that socket.

the server thread sends some data to the client. but the client is not yet ready to receive it. I thought this would have caused the server to wait until the client starts recv but instead returns immediately

the client then calls recv which is blocking and no data is ever received.

client socket constructor

self.__clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.__clientSocket.connect((server, port))

server socket constructor

        self.servSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.servSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        #self.servSock.settimeout(None)
        self.servSock.setblocking(1)
        self.servSock.bind((self.addr,self.port))
        self.servSock.listen(5)

listening accept thread

    try:
        (cs, address) = self.servSock.accept()
    except socket.timeout:
        return
    threadName = '\r\nClient %s:%s\r\n' % (cs, address)
    print threadName
    clientSocketHandler = ClientSocket()
    clientSocketHandler.setClientSocket(cs)
    self.clients.newThread(self.clientFunc, {clientSocketHandler : "1"}, threadName).start()

server and clients send/rec methods from inside ClientSocket

receivedData = self.__clientSocket.recv(1024*1024)

self.__clientSocket.send(s)

any ideas why send() is returning straight away?

© Stack Overflow or respective owner

Related posts about python

Related posts about socket