写一个具体的c语言程序用来查询IP地址范围
时间: 2023-02-08 13:55:00 浏览: 114
使用C#编写查询IP段功能的程序.txt
以下是一个用来查询IP地址范围的简单 C 程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s hostname\n", argv[0]);
return 1;
}
struct addrinfo hints, *res;
int errcode;
char addrstr[100];
void *ptr;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
errcode = getaddrinfo(argv[1], NULL, &hints, &res);
if (errcode != 0) {
printf("getaddrinfo: %s\n", gai_strerror(errcode));
return 1;
}
printf("IP addresses for %s:\n\n", argv[1]);
for (struct addrinfo *p = res; p != NULL; p = p->ai_next) {
inet_ntop(p->ai_family, p->ai_addr->sa_data, addrstr, sizeof(addrstr));
switch (p->ai_family) {
case AF_INET:
ptr = &((struct sockaddr_in *) p->ai_addr)->sin_addr;
break;
case AF_INET6:
ptr = &((struct sockaddr_in6 *) p->ai_addr)->sin6_addr;
break;
default:
printf("unknown address family\n");
continue;
}
printf("%s\n", addrstr);
}
freeaddrinfo(res);
return 0;
}
```
使用方法: 编译后在命令行中运行 ./program_name example.com
程序将会输出该域名的 IP 地址范围。
阅读全文