simple web server not working correctly

Hi experts,

I modified some code in order to create a simple webserver for my testing. The problem  is that the server accepts the connection correctly and send the response. But the client cannot read correctly from the socket once he gets the response.
Can you figure out what I'm doing wrong by looking at the code below?
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include <unistd.h>

#define PORT 80
#define MLEN 1000
#define BUFSIZE 8192

int main(int argc, char *argv [])
{

        int listenfd, connfd;
        int number, message, numbytes;
        int h, i, j;
		socklen_t  alen; 
        int nread;
        struct sockaddr_in servaddr; 
        struct sockaddr_in cliaddr;
        FILE *in_file, *out_file, *fp;
        char buf[8192];



        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        if (listenfd < 0)
                 fprintf(stderr,"listen error") ;

        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family      = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port        = htons(PORT);

        if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
                fprintf(stderr,"bind error") ;

		listen(listenfd, 5);


        alen = sizeof(struct sockaddr);

		
		
        while ((connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &alen)) > 0)
        {
		

	        printf("accept one client from %s!\n", inet_ntoa(cliaddr.sin_addr));



			if (fork() == 0)
			{
					close(listenfd);
				printf("reading from socket");
				bzero(buf, BUFSIZE);
				numbytes = read (connfd, buf, BUFSIZE);
				if (numbytes < 0) printf("error reading from socket");
				sleep(1);

				bzero(buf, BUFSIZE);


		 		fp = fopen (argv [1], "r"); // open file stored in server
				if (fp == NULL) {
				        printf("\nfile NOT exist");
				}

				//Sending file
				while(!feof(fp)){

				        numbytes = fread(buf, sizeof(char), sizeof(buf), fp);
				        printf("fread %d bytes, ", numbytes);
				        numbytes = write(connfd, buf, numbytes);
				        printf("Sending %d bytes\n",numbytes);
				}

				fclose (fp);    
				sleep(5);
				close(connfd);
					exit(0);
        		}
			close(connfd);
	}
        	
        return 0;
}

Open in new window

LVL 2
bachra04Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
A shot in the dark, but it seems you're not sending any HTTP headers. Try sending

"Content-type: text/plain\n\n"

Open in new window


before actually sending the file's contents.
0
jkrCommented:
BTW, see also http://en.wikipedia.org/wiki/HTTP_header

The simple header I posted above is the least you have to send with a web server's response (given that this response ist "text/plain").
0
bachra04Author Commented:
I'm reading the http contents from a file and I could see it in the wireshark. But it looks something is not done correctly on the socket side.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

jkrCommented:
So, does that start with "Content-type: text/plain\n\n"? (Or the appropriate header?)
0
bachra04Author Commented:
I have used the following code as well , I can see the response but my client is failing:
write(connfd, "HTTP/1.1 200 OK\n", 16);
        write(connfd, "Content-length: 46\n", 19);
        write(connfd, "Content-Type: text/html\n\n", 25);
        write(connfd, "<html><body><H1>Hello world</H1></body></html>",46);
0
jkrCommented:
What error code are you gettng (and, please use 'strlen()' for determning the length of each header line - or, even beter, a 'std::string')
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bachra04Author Commented:
I added some debug info and it turns out my client is failing the response because of the missing \r\n in the header, every header has to end with \r\n instead of \n only. after replacing \n by \r\n it is now working fine.
0
bachra04Author Commented:
I just have one extra question:
When I read from file using fread
The following :

HTTP/1.1 200 OK\r\n

is converted to :

HTTP/1.1 200 OK\\r\\n

is there a way to avoid that ?
0
jkrCommented:
If you were on Windows - yes. But you seem to be un UN*X, and that should not happen there. Are you sure the duplicate backslash isn't already there? Maybe you could check that with a hex editor.
0
bachra04Author Commented:
That's weird,

It is under ubuntu. the only work around I 've found was to remove \r\n from the file.
Then when I saved it under gedit it gave me the option to save it with Line ending as windows and like that the problem
was solved.
0
jkrCommented:
Weird, indeed :-/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.