unsigned short checksum(unsigned short *buf, int nword)
{
unsigned long sum;
for(sum = 0; nword > 0; nword--)
{
sum += htons(*buf);
buf++;
}
sum = (sum>>16) + (sum&0xffff);
sum += (sum>>16);
return ~sum;
}
运行结果如下:
源代码下载:
------------------------------------------分割线------------------------------------------
具体下载目录在 /2015年资料/4月/13日/Linux 网络编程——原始套接字实例:发送 UDP 数据包/
------------------------------------------分割线------------------------------------------