linux UDP服务器主动发送数据

tcp udp的连接:

1,tcp服务器:写执行程序板子服务器自己的地址和端口号

tcp客户端:绑定对方(服务器)的IP地址和端口号

2,udp服务器:绑定自己的地址和端口号

udp客户端:绑定对方(服务器)的IP地址和端口号

客户端自己的端口号和IP地址与服务器是不一样的

 

比如使用udp:

端口号是一样的,客户端以服务器为准

客户端是192.168.1.22,电脑虚拟机是192.168.1.29

客户端(powerpc):设置为1.29

 

端口号:端口号可以写成不一样

linux UDP服务器主动发送数据

#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

//MAC地址可以乱写,但不能不写

unsigned short checksum(unsigned short *buf, int nword);//校验和函数

int main(int argc, char *argv[])

{

//1.创建通信用的原始套接字

int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

//2.根据各种协议首部格式构建发送数据报

unsigned char send_msg[1024] =

{

//--------------组MAC--------14------

0xb8, 0x88, 0xe3, 0x7c, 0x55, 0xe4, //dst_mac(电脑网口MAC地址): B8-88-E3-7C-55-E4

0x00, 0x0e, 0xc6, 0xdd, 0xd1, 0xa1, //src_mac(linux开发板MAC地址): 00:0e:c6:dd:d1:a1

// 0x00,0x0c,0x29,0xad,0x9e,0x68, //36

0x08, 0x00, //类型:0x0800 IP协议

//--------------组IP---------20------

0x45, 0x00, 0x00, 0x00, //版本号:4, 首部长度:20字节, TOS:0, --总长度--:

0x00, 0x00, 0x00, 0x00, //16位标识、3位标志、13位片偏移都设置0

0x80, 17, 0x00, 0x00, //TTL:128、协议:UDP(17)、16位首部校验和

192, 168, 1, 22, //src_ip(电脑网口助手): 10.221.20.11

192, 168, 1, 29, //dst_ip(开发板linux): 10.221.20.10

//--------------组UDP--------8+78=86------

0x1f, 0x90, 0x1f, 0x90, //src_port电脑的端口号:0x1f90(8080), dst_port(开发板linux端口号):0x1f90(8080)

0x00, 0x00, 0x00, 0x00, //#--16位UDP长度--30个字节、#16位校验和

};

int len = sprintf(send_msg+42, "%s", "this is for the udp test");

if(len % 2 == 1)//判断len是否为奇数

{

len++;//如果是奇数,len就应该加1(因为UDP的数据部分如果不为偶数需要用0填补)

}

*((unsigned short *)&send_msg[16]) = htons(20+8+len);//IP总长度 = 20 + 8 + len

*((unsigned short *)&send_msg[14+20+4]) = htons(8+len);//udp总长度 = 8 + len

//3.UDP伪头部

unsigned char pseudo_head[1024] =

{

//------------UDP伪头部--------12--

192, 168, 1, 29, //src_ip: 10.221.20.11

192, 168, 1, 36, //dst_ip: 10.221.20.10

0x00, 17, 0x00, 0x00, //0,17,#--16位UDP长度--20个字节

};

*((unsigned short *)&pseudo_head[10]) = htons(8 + len);//为头部中的udp长度(和真实udp长度是同一个值)

//4.构建udp校验和需要的数据报 = udp伪头部 + udp数据报

memcpy(pseudo_head+12, send_msg+34, 8+len);//--计算udp校验和时需要加上伪头部--

//5.对IP首部进行校验

*((unsigned short *)&send_msg[24]) = htons(checksum((unsigned short *)(send_msg+14),20/2));

//6.--对UDP数据进行校验--

*((unsigned short *)&send_msg[40]) = htons(checksum((unsigned short *)pseudo_head,(12+8+len)/2));

//6.发送数据

struct sockaddr_ll sll; //原始套接字地址结构

struct ifreq req; //网络接口地址

strncpy(req.ifr_name, "eth0", IFNAMSIZ); //指定网卡名称

if(-1 == ioctl(sock_raw_fd, SIOCGIFINDEX, &req)) //获取网络接口

{

perror("ioctl");

close(sock_raw_fd);

exit(-1);

}

/*将网络接口赋值给原始套接字地址结构*/

bzero(&sll, sizeof(sll));

sll.sll_ifindex = req.ifr_ifindex;

len = sendto(sock_raw_fd, send_msg, 14+20+8+len, 0 , (struct sockaddr *)&sll, sizeof(sll));

if(len == -1)

{

perror("sendto");

}

return 0;

}

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;

}