I have a simple file transfer socket program where one socket sends file data and another socket receives the data and writes to a file
I need to send an acknowledgment once transfer is finished from the destination to the source
Code for destination
s.accept()
f = s.makefile()
f.read(1024)
Code for source
s.connect(('localhost',6090))
f = s.makefile()
f.write('abcd')
f.flush()
Here comes the problem, since “abcd” is not 1024 bytes, the destination will block till 1024 bytes are received
The solution would be to close() the socket, but since I need a confirmation from destination, I cannot close the socket.
How do I tell the destination to stop blocking? I was thinking of writing an EOF character
I read online that “\x04” is EOF, but it doesn’t work.
Also since the data could be binary I don’t want to use the readline() method.
Design a protocol (an agreement between client and server) on how to send messages. One simple way is “the first byte is the length of the message, followed by the message”. Rough example:
Client
Server