Skip to content

Instantly share code, notes, and snippets.

@ayourtch
Created January 20, 2011 17:24
Show Gist options
  • Save ayourtch/788223 to your computer and use it in GitHub Desktop.
Save ayourtch/788223 to your computer and use it in GitHub Desktop.
getaddrinfo one-family test
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include <assert.h>
int main(int argc, char *argv[]) {
struct addrinfo hints;
struct addrinfo *result, *rp;
int s, j;
int sock = -1;
assert((argc>2) || "Need 2 arguments: integer for address family and a host name" == 0);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = atoi(argv[1]);
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
s = getaddrinfo(argv[2], "http", &hints, &result);
if (s != 0) {
printf("getaddrinfo: %s\n", gai_strerror(s));
exit(1);
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
printf("AF: %d, socktype: %d, proto: %d\n",
rp->ai_family, rp->ai_socktype, rp->ai_protocol);
}
if(result) {
freeaddrinfo(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment