当前位置: 移动技术网 > 网络运营>服务器>Linux > 《Linux网络编程》: 原始套接字发送UDP报文

《Linux网络编程》: 原始套接字发送UDP报文

2020年07月15日  | 移动技术网网络运营  | 我要评论

校验和函数:

/*******************************************************
功能:
	校验和函数
参数:
	buf: 需要校验数据的首地址
	nword: 需要校验数据长度的一半
返回值:
	校验和
*******************************************************/
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;
}

这里是在 ubuntu 下通过原始套接字组一个 udp 数据包,给 PC 机的网络调试助手发送信息:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>				//struct ifreq
#include <sys/ioctl.h>			//ioctl、SIOCGIFADDR
#include <sys/socket.h>
#include <netinet/ether.h>		//ETH_P_ALL
#include <netpacket/packet.h>	//struct sockaddr_ll
 
 
unsigned short checksum(unsigned short *buf, in

本文地址:https://blog.csdn.net/yexiangCSDN/article/details/85988515

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网