Skip to content

Instantly share code, notes, and snippets.

@mayank
Created November 13, 2013 02:49
Show Gist options
  • Select an option

  • Save mayank/7442853 to your computer and use it in GitHub Desktop.

Select an option

Save mayank/7442853 to your computer and use it in GitHub Desktop.
Fetching Web Page using C
#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