- 论坛徽章:
- 0
|
这么快就回复了:wink:
但是AF_PACKET套接字好像可以不bind,这时dev就等于NULL,相当于匹配所有设备,
可以试下这个程序
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netpacket/packet.h>
- #include <net/ethernet.h>
- #include <errno.h>
- #define err(str)\
- do{\
- perror(str);\
- exit(EXIT_FAILURE);\
- }while(0);
- int main()
- {
- int i,fd;
- size_t recvlen;
- unsigned char buf[2000];
- struct sockaddr_ll addr;
- fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
- if(fd < 0)
- err("socket");
- while(1)
- {
- recvlen = recv(fd, buf, 2000, 0);
- if(recvlen < 0)
- err("recv");
- printf("%d bytes received\n", recvlen);
- }
- return 0;
- }
复制代码 |
|