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?
Sorry about the delay i fixed the problem shortly after asking this question. @Lee thanks for your answer it pointed me in the right direction. the solution was to send a 4byte int specifying the size of the data to follow. the client would always receive these four bytes and then the size of the data.
all send() does is fill the network buffer and return the ammount of bytes sent.
if you want a send that blocks just recv an acknowledgement message from the client.
The client doesn't have to be ready to receive data - data will queue up in the socket's receive buffer until you are ready to recv() it. Send returns instantly because the send buffer isn't full - if it was full, send() would block until there was room for the data you wanted to send.
Most of the time you'll never fill it - hence what you are experiencing. On a side, you probably don't want a recv call with 1024*1024 in it - that's a little on the high side.