1、通过 socket函数创建原始套接字 int sock_raw_fd =socket(PF_PACKET, SOCK_RAW, ETH_P_ALL)
2、组装数据包msg(按照协议格式进行组装)
3、指定网卡 struct ifreq ethreq ; strncpy (ethreq.ifr_name , "eth0",IFNAMSIZ); if (-1 == ioctl(sock_raw_fd, SIOCGIFINDEX,ðreq)){ perror ( "ioctl") ;close ( sock_raw_fd) ;exit (-1) ; } struct sockaddr_ll sll; bzero (&sll,sizeof (sll)); sll.sll ifindex = ethreq.ifr ifindex;
4、发送数据 sendto(sock_raw_fd, msg, msg_len, 0,(struct sockaddr*)&sll, sizeof(sll));
5、关闭套接字 close ( sock_raw_fd) ;