(original) (raw)
#include #include #include #include #include #include #include #include #include #include #include int main() { int s, n = 0; struct addrinfo *res, *rp; struct addrinfo hints; char buffer[1024]; memset(&hints, '0', sizeof(struct addrinfo)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = 0; hints.ai_flags = 0; int sockfd = 0; char port[5] = "8000"; port[5] = '\0'; const char *host = "localhost"; s = getaddrinfo(host, port, &hints, &res); if(s != 0) { printf("getaddrinfo: %s\n", gai_strerror(s)); exit(1); } for(rp = res; rp != NULL; rp = rp->ai_next) { sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if(sockfd == -1) { continue; } if(connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1) { break; } close(sockfd); } if(rp == NULL) { puts("Could not connect"); exit(1); } freeaddrinfo(res); const char *headers = "GET / HTTP/1.1\r\nHost: localhost:8000\r\n"; int header_length = strlen(headers); int send_data = send(sockfd, headers, header_length, 0); if(send_data == -1) { puts("We could not send the data Scotty."); return 1; } puts("Sending the data."); printf("%s\r\n", headers); while((n = recv(sockfd, &buffer, 1024, hints.ai_flags)) > 0) { printf("TEST: %d \r\n", n); printf("Buffer: %s \r\n", buffer); } close(sockfd); puts("Shutting down."); return 0; }