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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/* get-url.c */
#ifdef __cplusplus
#error Be sure you are using a C compiler...
#endif
#if defined (WIN32)
#include <winsock2.h>
#elif defined (linux) || defined (_POSIX_VERSION) || defined (_POSIX2_C_VERSION)\
|| defined (_XOPEN_VERSION)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> /* close */
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close (s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#else
#error not defined for this platform
#endif
#include <stdio.h>
#include <stdlib.h>
/* macros ============================================================== */
#define PORT 80
/* constants =========================================================== */
/* types =============================================================== */
/* structures ========================================================== */
/* private data ======================================================== */
/* private functions =================================================== */
static void emission (SOCKET socket_id, char const *s)
{
send (socket_id, s, strlen (s), 0);
}
static int get_url (char const *s_host_, char const *s_file_)
{
struct hostent *host_address = gethostbyname (s_host_);
if (host_address != NULL)
{
SOCKET socket_id = socket (PF_INET, SOCK_STREAM, 0);
if (socket_id != INVALID_SOCKET)
{
struct sockaddr_in sockname;
#if 0
int optval = 1;
setsockopt (socket_id, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof optval);
#endif
sockname.sin_family = host_address->h_addrtype;
sockname.sin_port = htons (PORT);
memcpy (&sockname.sin_addr.s_addr, host_address->h_addr,
host_address->h_length);
if (connect (socket_id, (struct sockaddr *) &sockname,
sizeof (struct sockaddr_in)) != SOCKET_ERROR)
{
/* requete http GET */
emission (socket_id, "GET ");
emission (socket_id, s_file_);
emission (socket_id, " HTTP/1.1\r\nHost: ");
emission (socket_id, s_host_);
emission (socket_id, "\r\n\r\n");
/* reception de la page html */
{
char str[1024];
int l;
while ((l = recv (socket_id, str, sizeof (str) - 1, 0)) > 0)
{
str[l] = 0;
printf ("%s", str);
}
}
shutdown (socket_id, 2);
closesocket (socket_id);
}
else /* connect () */
{
perror ("connect ()");
}
}
else /* socket () */
{
perror ("socket ()");
}
}
else /* gethostbyname () */
{
perror ("gethostbyname ()");
}
return 0;
}
/* entry point ========================================================= */
/* ---------------------------------------------------------------------
--------------------------------------------------------------------- */
int main (void)
{
int ret;
#if defined (WIN32)
WSADATA wsa_data;
int err = WSAStartup (MAKEWORD (2, 2), &wsa_data);
if (!err)
{
puts ("WIN: winsock2: OK");
#else
int err;
#endif
#define HOST "c.developpez.com"
#define FILE "/"
get_url (HOST, FILE);
#if defined (WIN32)
WSACleanup ();
}
#endif
if (err)
{
ret = EXIT_FAILURE;
}
else
{
ret = EXIT_SUCCESS;
}
return ret;
} |
Partager