How to get the length of received UDP packet? Using wireshark I could see the correct length of datagram. How can I print this value in my simple udp server program? I am receiving binary data (Non printable ascii characters as a data) So I can not use strlen(buf) which throws incorrect length.
Code:
if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)!=-1){
error = ioctl(s, FIONREAD, &value);
printf(" from ioctl UDP packet length is : %d error is : %d\n", value, error);
}
udp packet length is always '0' from the above code. Any comments?
I also tried like below
Code:
if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)!=-1){
unsigned short iphdrlen;
struct iphdr* iph = (struct iphdr*)buf;
iphdrlen =iph->ihl*4;
printf("IP version :%d\n", ((unsigned int)((iph->version))));
printf("protocol .. %d\n", ((unsigned int)(iph->protocol)));
printf("total len .. %d\n", (ntohs(iph->tot_len)));
}
The above code always returns me wrong values from ip header? Any comments?