/* Example Client Code */ #include #include #include #include #include #include #include #include #include int connect_host_port(const char *host, const char *service) { struct addrinfo *ai, *runp; struct addrinfo hints; int e, sock = -1; memset(&hints, '\0', sizeof (hints)); hints.ai_flags = AI_ADDRCONFIG; hints.ai_socktype = SOCK_STREAM; e = getaddrinfo(host, service, &hints, &ai); if (e != 0) { /* error(EXIT_FAILURE, 0, "getaddrinfo: %s", gai_strerror(e)); */ return -1; } for (runp = ai; runp != NULL; runp = runp->ai_next) { sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol); if (sock == -1) continue; e = connect(sock, runp->ai_addr, runp->ai_addrlen); if (e != 0) { close(sock); sock = -1; continue; } break; } freeaddrinfo(ai); if (sock == -1) { /*error(0, 0, "cannot contact %s", host);*/ return -1; } return sock; }