Sei sulla pagina 1di 2

//Creation of sockets for HTTP web page upload and download

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netdb.h>
#include <string.h>

#include <unistd.h>

char http[] = "GET / HTTP/1.1\nAccept: */*\nHost: www.google.com\nAccept-Charset:


utf-8\nConnection: keep-alive\n\n";

char page[BUFSIZ];

int main(int argc, char **argv)

{
struct addrinfo hint, *res, *res0;

char *address = "www.google.com";


char *port = "80";

int ret, sockfd;

memset(&hint, '\0', sizeof(struct addrinfo));

hint.ai_family = AF_INET;

hint.ai_socktype = SOCK_STREAM;

/* hint.ai_protocol = IPPROTO_TCP; */

if((ret = getaddrinfo(address, port, &hint, &res0)) < 0)

{
perror("getaddrinfo()");
exit(EXIT_FAILURE);
}

for(res = res0; res; res = res->ai_next)


{

sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

if(-1 == sockfd)
{
perror("socket()");
continue;

ret = connect(sockfd, res->ai_addr, res->ai_addrlen);

if(-1 == ret)
{

perror("connect()");

sockfd = -1;

continue;
}

break;

if(-1 == sockfd)
{

printf("Can't connect to the server...");

exit(EXIT_FAILURE);
}

send(sockfd, http, strlen(http), 0);

recv(sockfd, page, 1023, 0);

printf("%s\n", page);

return 0;
}

Potrebbero piacerti anche