#include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int rc; char *host; char *portstr = "8083"; struct addrinfo hints, *res, *info; char hostb[NI_MAXHOST]; char servb[NI_MAXSERV]; if (argc != 2) { fprintf(stderr, "Usage: testic hostname\n"); return 2; } host = argv[1]; memset(&hints, 0, sizeof(hints)); // hints.ai_flags = AI_PASSIVE; rc = getaddrinfo(host, portstr, &hints, &res); if (rc) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(rc)); return 1; } for (info = res; info != NULL; info = info->ai_next) { rc = getnameinfo(info->ai_addr, info->ai_addrlen, hostb, NI_MAXHOST, servb, NI_MAXSERV, NI_NUMERICSERV); if (rc) { fprintf(stderr, "getnameinfo error: %s\n", gai_strerror(rc)); return 1; } printf("host %s port %s\n", hostb, servb); } freeaddrinfo(res); return 0; }