日期:2014-05-16 浏览次数:20751 次
//send.c #include <stdio.h> #include <stdlib.h> //perror #include <string.h> //strcpy,memset #include <sys/socket.h> //socket #include <sys/ioctl.h> //ioctl #include <net/if.h> //ifreq #include <linux/if_packet.h> //sockaddr_sll #include <linux/if_ether.h> //ETH_P_ALL #define IFRNAME0 "eth0" #define IFRNAME2 "eth2" #define BUF_SIZE 2048 char buf[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int main(int argc, char *argv[]) { int i, sfd, len; struct sockaddr_ll sll; struct ifreq ifr; if ((sfd=socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) { perror("socket"); return 0; } memset(&ifr, 0, sizeof(ifr)); strcpy(ifr.ifr_name, IFRNAME0); if ((ioctl(sfd, SIOCGIFINDEX, &ifr)) == -1) { strcpy(ifr.ifr_name, IFRNAME2); if ((ioctl(sfd, SIOCGIFINDEX, &ifr)) == -1) { perror("ioctl 1"); close(sfd); return 0; } } memset(&sll, 0, sizeof(sll)); sll.sll_family = PF_PACKET; sll.sll_ifindex = ifr.ifr_ifindex; sll.sll_protocol = htons(ETH_P_ALL); if ((bind(sfd, (struct sockaddr *)&sll, sizeof(sll))) == -1) { perror("bind"); close(sfd); return 0; } while (1) { len = sizeof(buf); sendto(sfd, buf, len, 0, (struct sockaddr *)&sll, sizeof(sll)); printf("send data2: %d\n", len); for(i=0; i<len; i++) printf("%x", buf[i]); printf("\n"); } close(sfd); return 1; }
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); tsRevSockPara.sin_family = AF_INET; tsRevSockPara.sin_port = htons(port); tsRevSockPara[i].sin_addr.s_addr = htonl(INADDR_ANY); bind(sockfd, (struct sockaddr*)&tsRevSockPara, sizeof(tsRevSockPara));
------解决方案--------------------
是数据链路层的4字节CRC,
C R C字段用于帧内后续字节差错的循环冗余码检验(检验和)(它也被称为F C S或帧检验
序列)。
8 0 2 . 3标准定义的帧和以太网的帧都有最小长度要求。8 0 2 . 3规定数据部分必须至少为3 8字
节,而对于以太网,则要求最少要有4 6字节。为了保证这一点,必须在不足的空间插入填充
(p a d)字节
------解决方案--------------------
原始套接字工作在网络层。可以直接取得原始数据,发送的时候没看见你定义任何头部,没看见你的效验和计算,没看见你接受的时候做任务协议的解包过程!
------解决方案--------------------