I’m trying to send more than one image file from a client to the server with sockets.
My problem is that I can’t get the stoppbit (or word) working.
client:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("server",port))
#grab image ...
s.send(imagedata)
s.send("#FINISH#")
s.close()
server:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(("",port))
s.listen(1)
c,a = s.accept()
while True:
imagedata = c.recv(1024)
if not imagedata or "#FINISH#" in imagedata:
komm.close()
break
image = image + imagedata
s.close()
#remove stoppword and save image ...
This is only a simple example but you can see the problem. I can’t fetch the “#FINISH#” because the second send (client) appends the string (sometimes) to the imagedata. I know it’s because of the bufsize of recv. How should I do it?
Don’t send a “stop bit”, instead first send the length of the data in a fixed-length field.