qfedu-network-advanced-level/day6/n1.c

23 lines
453 B
C
Raw Permalink Normal View History

2023-09-11 20:58:01 +08:00
/*创建链路层的原始套接字*/
#include <netinet/ether.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
// 创建原始套接字
int sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sock_fd < 0)
{
perror("raw socket");
return -1;
}
printf("原始套接字创建成功\n");
close(sock_fd);
return 0;
}