1 int sockbind(char *host, char *serv, int prot, int type)
3 struct addrinfo hint, *addr;
6 /* initialize hint structure */
7 memset(&hint, 0, sizeof(struct addrinfo));
8 hint.ai_flags = AI_PASSIVE; /* address for binding */
9 hint.ai_family = PF_UNSPEC; /* generic address (IPv4 or IPv6) */
10 hint.ai_protocol = prot; /* protocol */
11 hint.ai_socktype = type; /* socket type */
12 res = getaddrinfo(host, serv, &hint, &addr); /* calling getaddrinfo */
13 if (res != 0) { /* on error exit */
14 printf("sockbind cannot resolve host %s, service %s, ", host, serv);
15 printf("protocol %d: %s\n", prot, gai_strerror(res));
19 sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
21 printf("sockconn cannot create socket\n");
24 /* connect the socket */
25 res = bind(sock, addr->ai_addr, addr->ai_addrlen);
27 printf("sockconn cannot bind socket\n");
30 freeaddrinfo(addr); /* done, release memory */