Hi guys
I so upset today
i have this question about socket and i made the program do you know what wrong with it
this the question :
Write one TCP socket program (named webclient.cpp) to simulate a web browser by using HTTP/1.1.
The client should take three arguments, hostname, pathname, and port to access a web server with name as provided as the first argument:
- contacts the server (hostname, the format like "133.47.122.487")
- access the web page (pathname) of that hostname
- read the data sent by the server, extract the html file from the data, and save it as a file named output.html.
- after finish reading the file, close the connection.
my pro :
// header files
#include <stdio.h> //for printf(), ...
#include <sys/types.h> //for data types
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h> //for socket(), connect(), ...
#include <unistd.h> //for close()
#include <netinet/in.h> //for internet address family
#include <netdb.h> // for gethostbyname
#include <arpa/inet.h> //for sockaddr_in, inet_addr()
#include <errno.h> //for system error numbers
#define MESSAGESIZE 500// max buffer size
int main(int argc, char *argv[])
{
FILE *out_file ;
int sockfd; // main socket file descriptor
unsigned int PORT; // port number
int numbytes; // # of bytes recieved by recv() system call
char msg[MESSAGESIZE]; //buffer which contains data coming in from recv() system call
struct hostent *hp; /* Here we store information about host*/
struct sockaddr_in server_addr; // connector's address information
// ---------------------------------------------------------
/* socket: create the socket */
// ---------------------------------------------------------
//if user doesn't provide the clients hostname and port number
if (argc != 3) {
fprintf(stderr,"Please provide client hostname and port number without / \n");
exit(1);
}
// ---------------------------------------------------------
// get the ip address for the hostname that provided by arg[1]
// ---------------------------------------------------------
if ((hp=gethostbyname(argv[1])) == NULL) {
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// ---------------------------------------------------------
/* build the server's Internet address */
// ---------------------------------------------------------
PORT = atoi(argv[2]); // Convert string to integer
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT); // short, network byte order
server_addr.sin_addr = *((struct in_addr *)hp->h_addr);
memset(&(server_addr.sin_zero), '\0', 8); // zero the rest of the struct
// ---------------------------------------------------------
//connect to thy host
// ---------------------------------------------------------
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
{
perror("connect");
exit(1);
}
printf("Connected to %s\n", inet_ntoa(*((struct in_addr *)hp->h_addr)));
// ---------------------------------------------------------
// Message Preparation to server
// ---------------------------------------------------------
printf( "Client: initializing message and sending\n");
strcpy(msg, "GET ");
strcat(msg, argv[2]); // path
strcat(msg, " HTTP/1.1\r\n");
// ---------------------------------------------------------
/* get message line from the user */
// ---------------------------------------------------------
printf("Please type a short message\r\n",msg);
bzero(msg, MESSAGESIZE);
fgets(msg, MESSAGESIZE, stdin);
// ---------------------------------------------------------
/* send the message line to the server */
// ---------------------------------------------------------
numbytes = write(sockfd, msg, strlen(msg));
if (numbytes < 0)
perror("ERROR writing to socket");
// ---------------------------------------------------------
/* print the server's reply */
// ---------------------------------------------------------
bzero(msg, MESSAGESIZE);
numbytes = read(sockfd, msg, MESSAGESIZE);
if (MESSAGESIZE < 0)
perror("ERROR reading from socket");
printf("Recieve from server: %s", msg);
// ---------------------------------------------------------
// extract the header part: Content-Length
// -------------------------------------------------------
numbytes = read(sockfd,msg, MESSAGESIZE);
write (sockfd,msg,500);
out_file=fopen("output.htm","wb");
fwrite(msg, sizeof(msg[MESSAGESIZE]), sizeof(msg)/sizeof(msg[0]), out_file);
if ((out_file=fopen("output.htm","wb")) == NULL) {
fprintf(stderr,"Can not open output file\n");
exit (1);
}
fclose(out_file);
close(sockfd);
return 0;
}