#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_MSG 100
#define SUCCESS 0
#define ERROR 1
#define MAX_STR_LEN 512
#define BUFFER_SIZE 16384 // 16K
/*Parses an url into hostname, port number and resource identifier.*/
parse_URL(char *url, char *hostname, int *port, char *identifier)
{
char protocol[MAX_STR_LEN], scratch[MAX_STR_LEN], *ptr=0, *nptr=0;
strcpy(scratch, url);
ptr = (char *)strchr(scratch, ':');
if (!ptr)
{
fprintf(stderr, "Wrong url: no protocol specified\n");
exit(ERROR);
}
strcpy(ptr, "\0");
strcpy(protocol, scratch);
if (strcmp(protocol, "http"))
{
fprintf(stderr, "Wrong protocol: %s\n", protocol);
exit(ERROR);
}
strcpy(scratch, url);
ptr = (char *)strstr(scratch, "//");
if (!ptr)
{
fprintf(stderr, "Wrong url: no server specified\n");
exit(ERROR);
}
ptr += 2;
strcpy(hostname, ptr);
nptr = (char *)strchr(ptr, ':');
if (!nptr)
{
*port = 80; /* use the default HTTP port number */
nptr = (char *)strchr(hostname, '/');
}
else
{
sscanf(nptr, ":%d", port);
nptr = (char *)strchr(hostname, ':');
}
if (nptr)
*nptr = '\0';
nptr = (char *)strchr(ptr, '/');
if (!nptr)
{
fprintf(stderr, "Wrong url: no file specified\n");
exit(ERROR);
}
strcpy(identifier, nptr);
}
int main( int argc, char *argv[ ])
{
char url[MAX_STR_LEN],hostname[MAX_STR_LEN], identifier[MAX_STR_LEN], buf[MAX_STR_LEN], line[MAX_MSG], buffer[BUFFER_SIZE];
int port, sd, rc, i, len;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
if (argc != 2)
{
printf("\n
Usage: wwwclient
exit(ERROR);
}
else
{
strcpy(url, argv[1]);
}
parse_URL(url, hostname, &port, identifier);
printf("\n-- Hostname = %s , Port = %d , Identifier = %s\n", hostname, port, identifier);
//Form HTTP Request
sprintf(buffer, "GET %s HTTP/1.1\n\nHOST: %s\n\n", identifier,hostname);
h = gethostbyname(hostname);
if(h==NULL)
{
printf("unknown host: %s \n ", hostname);
exit(ERROR);
}
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
servAddr.sin_port = htons(port);
// create socket
printf("-- Create socket... ");
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0)
{
perror("cannot open socket ");
exit(ERROR);
}
// connect to server
printf("Connect to server...\n");
rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
if(rc<0)
{
perror("cannot connect ");
exit(ERROR);
}
// send request
printf("-- Send HTTP request:\n\n%s", buffer);
rc=send(sd, buffer, strlen(buffer), 0);
if(rc<0)
{
perror("cannot send data ");
close(sd);
exit(ERROR);
}
// display response
printf("-- Recieved response:\n\tfrom server: %s, IP = %s,\n\n",url, inet_ntoa(servAddr.sin_addr));
rc = 0;
int bytes_read;
do
{
bzero(buffer, sizeof(buffer));
bytes_read = recv(sd, buffer, sizeof(buffer), 0);
if ( bytes_read > 0 )
printf("%s", buffer);
rc=rc+bytes_read;
}
while ( bytes_read > 0 );
printf("\nTotal recieved response bytes: %d\n",rc);
close(sd);
return SUCCESS;
}
Copyright © 2011 - All Rights Reserved - Softron.in
Template by Softron Technology