Created
November 13, 2013 02:49
-
-
Save mayank/7442853 to your computer and use it in GitHub Desktop.
Fetching Web Page using C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/socket.h> | |
| #include <sys/types.h> | |
| #include <netdb.h> | |
| #include <inet/in.h> | |
| int main(int argc, char* argv[]) | |
| { | |
| struct addrinfo *p, hints, *res; | |
| char request[100]; | |
| char response[3000]; | |
| int socket_fd; | |
| snprintf( request, "GET %s HTTP/1.1\nhost: %s\n\n",argv[1],argv[2]); | |
| memset( &hints, 0, sizeof( hints ) ); | |
| hints.ai_family = AF_UNSPEC; | |
| hints.ai_socktype = SOCK_STREAM; | |
| getaddrinfo( argv[2], PORT, &hints, &res ); | |
| for( p = res; p; p = p->ai_next ) | |
| { | |
| socket_fd = socket( p->ai_family, p->ai_socktype, p->protocol ); | |
| connect( socket_fd, p->ai_addr, p->ai_addrlen ); | |
| } | |
| send( socket_fd, request, strlen(request), 0 ); | |
| recv( socket_fd, response, 3000 , 0 ); | |
| close( socket_fd ); | |
| printf( " %s ", response ); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment