ssize_t sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len);
In the above sendto()
synopsis, 3rd field represents length
which is ideally be the size of message
being sent in bytes.
What if the length
field is wrong and not equivalent to size of message
?
Will sendto()
succeeds sending only length bytes mentioned. If yes, what happens if the length is bigger compared to real valid message
size.
sendto()
has no concept of the type of buffer you use for your messages. It only knows about the parameter values you give it, so it is your responsibility to make sure they are accurate.
If the length
is smaller than the actual message, length
number of bytes are sent for UDP, and up to length
bytes are sent for TCP.
If the length
is larger than the actual message, the behavior is undefined. At best, you will likely send random data from surrounding memory. At worse, you will crash your code when trying to send bytes from invalid memory.