1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <string.h>
#include <unistd.h>
#define HOST "nsnous1.freehostia.com"
#define PORT 80
#define FILE "/"
int main (void)
{
WSADATA WSAData;
WSAStartup(MAKEWORD(2,0), &WSAData);
struct hostent *host_address = gethostbyname (HOST);
if (host_address != NULL)
{
int socket_id = socket (AF_INET, SOCK_STREAM, 0);
if (socket_id != -1)
{
struct sockaddr_in sockname;
sockname.sin_family = (AF_INET);
host_address->h_addrtype;
sockname.sin_port = htons (PORT);
memcpy (&sockname.sin_addr.s_addr,host_address->h_addr,(size_t)host_address->h_length);
if (connect (socket_id, (struct sockaddr *) &sockname,sizeof sockname) != -1)
{
char str[1024];
int l;
write (socket_id, "GET ", sizeof "GET " - 1);
write (socket_id, FILE, sizeof FILE - 1);
write (socket_id, " HTTP/1.0\r\nHOST: ",sizeof " HTTP/1.0\r\nHOST:" - 1);
write (socket_id, HOST, sizeof HOST - 1);
write (socket_id, "\r\n\r\n", sizeof "\r\n\r\n" - 1);
while ((l = read(socket_id, str, sizeof str - 1)))
{
str[l] = 0;
printf ("%s", str);
fflush (stdout);
}
close (socket_id), socket_id = -1;
}
else /* connect () */
{
perror ("connect ()");
}
}
else /* socket () */
{
perror ("socket ()");
}
}
else /* gethostbyname () */
{
perror ("gethostbyname ()");
}
return 0;
} |
Partager