/* * client by getaddrinfo (multi-protocol support) * by Jun-ichiro itojun Hagino. in public domain. */ #include #include #include #include #include #include #include #include int main __P((int, char **)); int main(argc, argv) int argc; char **argv; { struct addrinfo hints, *res, *res0; ssize_t l; int s; char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; char buf[1024]; int error; /* check the number of arguments */ if (argc != 3) { fprintf(stderr, "usage: test host port\n"); exit(1); /*NOTREACHED*/ } /* resolve address/port into sockaddr */ memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; error = getaddrinfo(argv[1], argv[2], &hints, &res0); if (error) { fprintf(stderr, "%s %s: %s\n", argv[1], argv[1], gai_strerror(error)); exit(1); /*NOTREACHED*/ } /* try all the sockaddrs until connection goes successful */ for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV); if (error) { fprintf(stderr, "%s %s: %s\n", argv[1], argv[1], gai_strerror(error)); continue; } fprintf(stderr, "trying %s port %s\n", hbuf, sbuf); s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (s < 0) continue; if (connect(s, res->ai_addr, res->ai_addrlen) < 0) { close(s); s = -1; continue; } while ((l = read(s, buf, sizeof(buf))) > 0) write(STDOUT_FILENO, buf, l); close(s); exit(0); /*NOTREACHED*/ } fprintf(stderr, "test: no destination to connect to\n"); exit(1); /*NOTREACHED*/ }