Program


 
Thread Tools Search this Thread
Top Forums Programming Program
# 1  
Old 03-05-2010
Program

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:
  1. contacts the server (hostname, the format like "133.47.122.487")
  2. access the web page (pathname) of that hostname
  3. read the data sent by the server, extract the html file from the data, and save it as a file named output.html.
  4. after finish reading the file, close the connection.
my pro :
Code:
// 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; 
}


Last edited by jim mcnamara; 03-10-2010 at 07:57 AM.. Reason: eased up on the formatting, added code tagsl code tags not php
Net Star
# 2  
Old 03-05-2010
Code:
if (MESSAGESIZE < 0)

How will that ever be true ?

You might want to reconsidder this part
Code:
bzero(msg, MESSAGESIZE); 
numbytes = read(sockfd, msg, MESSAGESIZE); 
if (MESSAGESIZE < 0)  
perror("ERROR reading from socket"); 
printf("Recieve from server: %s", msg);

you might want to do a loop, where you read from the socket, then write to your output file, when the numbytes is 0 or less you're done reading and it's time to close the output file..

I'm not sure if this has anything todo with the error, but one failure I see is:
Code:
fwrite(msg, sizeof(msg[MESSAGESIZE]), sizeof(msg)/sizeof(msg[0]), out_file);

considder what the sizeof(msg[MESSAGESIZE]) will be, if the read part dosn't fill in the entire msg arrray.

And the part where you open the output fie for writing isn't correct, you open it, try to write to it, then has an open issue trying if it is capable of beeing opened.

Last edited by redhead; 03-05-2010 at 07:22 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

2. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

3. Programming

Calling c program from another c program

Hi All, Probably this is a repeated question. My knowledge in this is limited and i got confused on all those materials i got in google search. We use #include <> to include a predefined library like stdio.h i saw somewhere that #include "" includes a man made module(another C program). IS... (2 Replies)
Discussion started by: jisha
2 Replies

4. Programming

Python program faster than C++ program.

I wrote a simple program that generates a random word 10,000,000 times. I wrote it in python, then in C++ and compared the two completion times. The python script was faster! Is that normal? Why would the python script be faster? I was under the impression that C++ was faster. What are some of... (2 Replies)
Discussion started by: cbreiny
2 Replies

5. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

6. Programming

A program to trace execution of another program

Hi, I wanted to know if i can write a program using switches and signals, etc to trace execution of other unix program which calls c program internally. If yes how? If not with signals and switches then are there any other methods apart from debugging with gdb/dbx. (3 Replies)
Discussion started by: jiten_hegde
3 Replies

7. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

8. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies

9. Programming

executing a program within a program

Read the title: how do i do it? (4 Replies)
Discussion started by: Gekko
4 Replies

10. Programming

Please need help with C program!

My program only ouputs the correct magic square sum totals for the number 15.If I enter any odd number smaller than 15 my sum totals are incorrect. I have define "size" to 15. How or what do I change so that my program will output the magic square results for the odd numbers 1 through 15 without... (1 Reply)
Discussion started by: JJJ
1 Replies
Login or Register to Ask a Question