#include #include #include #include #include int main (void) { // string pointing to an HTTP server (DNS name or IP) char str [] = "ozark.hendrix.edu"; //char str [] = "150.208.12.199"; // structures used in DNS lookups struct addrinfo *host_info, hints; // set address family (IPv4), socket type, and protocol memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = 0; hints.ai_protocol = IPPROTO_TCP; // Do DNS lookup of str, setting the port to 80 and using the hints for other parameters int ret = getaddrinfo(str, "80", &hints, &host_info); if( ret != 0 ){ printf("getaddrinfo: %s\n", gai_strerror(ret)); return -1; } // Open TCP socket int sock = socket(host_info->ai_family, host_info->ai_socktype, host_info->ai_protocol); if (sock == -1){ printf("socket() error:%s\n", strerror(errno)); return -1; } // Connect to server on port 80 ret = connect(sock, host_info->ai_addr, host_info->ai_addrlen); if( ret == -1 ){ printf("connect() error:%s\n", strerror(errno)); return -1; } printf ("Successfully connected to server\n"); // Free dynamically allocated memory that is no longer needed freeaddrinfo(host_info); // send HTTP requests here // close the socket to this server; open again for the next one close (sock); }