#include <string.h>
#include <stdio.h>
#include <netdb.h>
int main(){
char buff[50];
char port[5];
printf("Enter address to lookup: ");
fgets(buff, 50, stdin);
printf("Enter Port: ");
fgets(port, 5, stdin);
struct addrinfo* res;
struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_flags= AI_PASSIVE;
__auto_type error = getaddrinfo(buff, port, &hints, &res);
if (error < 0 )
return 4;
printf("Host IP: %s", buff);
error = getnameinfo(res->ai_addr, res->ai_addrlen, buff, 50, 0, 0, 0);
if (error < 0)
return 5;
printf("Host IP: %s", buff);
freeaddrinfo(res);
}
运行此代码 导致 getaddrinfo( ) 由于分段错误而终止程序。编辑:检查输入的返回值后,错误似乎来自 getaddrinfo()getaddrinfo
只是
www.google.com(还测试了一堆其他地址)443(也用端口 80 尝试过)