🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Python sendto sizes.

Started by
1 comment, last by Uncivil 19 years, 10 months ago
I have a quickie Python program that sends test data to a running C application over a network. Specifically I'm sending 3 floats using a SOCK_DGRAM socket like this:

data = struct.pack('!3f', f1, f2, f3)
sock.sendto(data, addr)


The length of data is 12. But the C application receives 16 bytes. The C code also uses a SOCK_DGRAM and looks like this:

sockaddr from;
socklen_t len = 0;
int buffsize = 12;
char buff[buffsize];
recvfrom(sock, (void*)&buff, buffsize, 0, &from, &len);

The first 12 bytes that is received is the correct data so it seems an extra 4 bytes is tacked on somewhere along the way. By the way only 12 bytes are received if Python is used to receive the data.
Advertisement
How is that C code receiving 16 bytes when you tell it to receive 12? Do you receive the EMSGSIZE error code? If you're thinking about the 'len' parameter, that's telling you how long the sender's address is, not how long the data sent is. A generic socket address is indeed 16 bytes long. The amount of data read however is the return value from recvfrom().
Thanks, that makes sense. I guess that's what I get for not carefully reading the documentation.

This topic is closed to new replies.

Advertisement