如何获取收到的UDP数据包的长度?使用wireshark我可以看到正确的数据报长度.如何在我的简单udp服务器程序中打印此值?我正在接收二进制数据(不可打印的ascii字符作为数据)所以我不能使用strlen(buf),它会抛出不正确的长度.
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);
}
Run Code Online (Sandbox Code Playgroud)
udp数据包长度始终为上述代码中的"0".任何意见?
我也尝试过如下
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)));
}
Run Code Online (Sandbox Code Playgroud)
上面的代码总是从ip header返回错误的值?任何意见?
原始C文件包含在此处.
#include "udp_common.h"
int main(void)
{
struct sockaddr_in si_me, …Run Code Online (Sandbox Code Playgroud)