23 struct addrinfo hints = {0};
25 hints.ai_family = AF_UNSPEC;
26 hints.ai_socktype = SOCK_STREAM;
29 struct addrinfo *p, *servinfo;
32 rv = getaddrinfo(addr, port, &hints, &servinfo);
34 fprintf(stderr,
"Failed to open socket (getaddrinfo): %s\n", gai_strerror(rv));
39 for(p = servinfo; p != NULL; p = p->ai_next) {
40 sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
41 if (sockfd == -1)
continue;
44 rv = connect(sockfd, p->ai_addr, p->ai_addrlen);
54 freeaddrinfo(servinfo);
58 if (sockfd != -1) fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK);
60 if (sockfd != INVALID_SOCKET) {
62 ioctlsocket(sockfd, FIONBIO, &iMode);
72 if (sockfd != -1) ioctl(sockfd, FIONBIO, &on);