I am using python 3.x and the socket module. The server is running on an ipv4 address and using tcp. I read some tutorials on how to send and receive data. For the server or client to make sure the entire message was sent you can simply check if the amount of sent data is equals the size of the message:
def mysend(self, msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError("socket connection broken")
totalsent = totalsent + sent
Source: https://docs.python.org/3/howto/sockets.html#socket-howto
And for the client to make sure the entire response has been received this tutorial recommends to add the size of the response at the beginning of the response.
My questions:
- How can I make sure I receive the first part of the message indicating the size of the message (assuming my message contains 1000 characters I would need four characters to indicate the size)?
- Why can't I just add a specified symbol like '<' at the begging of the message and '>' at the end so I know where it start and ends?
Edit:
- When I use
sock.recv(1024)and my messages just have a size of 500 to 1000 characters doesn't that make sure I receive all of them?