Sign in to follow this  
Uncivil

Python sendto sizes.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this