Hello!
I'm trying to send UDP datagrams from iphone app and have encountered a problem I cannot overcome.
The datagrams I send always have UDP header checksum set to 0 and are refused by destination device (networked controller). I have tried both BSD sockets and CFSocket with the same result. I have tried samples from Beej's tutorial - the same.
I have also conducted an experiment on ubuntu run inside virtual box. The same sample of bsd sendto() usage works fine there (= header has a checksum).
I have sniffed packets with wireshark in both cases. What's funny - wireshark on linux found checksum and the same packet cought on mac side had 0x0000 again. I guess that something might have been lost in translation by virtualbox bridge but still is strange to me.
Here is snip of my code (without error checking):
Code:
int fd = socket(AF_INET, SOCK_DGRAM, 0);
int broadcast = 1;
setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (void *)&broadcast, sizeof(broadcast));
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = ntohs(1234);
addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
addr.sin_len = sizeof(addr);
// inet_pton(AF_INET, "10.0.1.222", &addr.sin_addr.s_addr);
int num = sendto(fd, data, sizeof(data), 0, (struct sockaddr *)&addr, sizeof(addr));
What can be wrong here? "num" returned by sendto is correct and I can see all the data inside sniffed packets.
I have also written the same code in java, where it works without any problem and my destination device reacts correctly to sent packets.