Hi,

I am writing code for a webserver and am trying to send a html file called index.html over a TCP socket. How would I do this?

At the moment I am trying to read the contents of the file and then send them over the connection. However, the page is not being received correctly. I suspect that I am using the wrong function to read and something to do with the data being read as ASCII and not being converted back. But am not sure what else to do =s and cannot find anything online. Please help!

while(!feof(sendFile)){

fgets(send_buffer, MAX_LEN, sendFile);

send(new_fd,send_buffer,sizeof(send_buffer),0);
}

I have tried using fgetc and fread but they do not work. Fgets seems to be the only function that actually seems to read and transmit the data but on the browser it shows up as jibberish :s

Number one...are you checking to see if the file opened correctly?

If so then try:

#define MAXLINE 4096

char recvline[MAXLINE];
int connfd;
FILE *fd;

while ((write(connfd, recvline, fread(recvline, 1, MAXLINE, fd))) > 0)
{}

Thanks but I am error checking to see if the file opens or not. Here is my code for one of the functions..ignore the extra printfs - they're there to help me keep track of whats happening:

int Parse_request(int new_fd){

	FILE *fread;
	FILE *sendFile;
	char request_page[MAX_MSG];
	char send_buffer[MAX_LEN];
	struct stat st;
	int i=0;
	int len;

	if((fread = fopen("Incoming.txt","r"))<0){
		printf("Could not read file: Incoming.txt");
		exit(1);
	}
	fscanf(fread, "%s", request_page);

	//check if the request is a GET request
	if(strcmp(request_page, GET)==0){
		fscanf(fread, "%s", request_page);
		printf("The request page is %s\n",request_page);

		//Remove preceeding "/" from the filename
		for(i=0;i<(sizeof(request_page));i++){
			request_page[i] = request_page[i+1];
		}
        //search for the requested file on the system
		if(stat(request_page, &st) == 0){
			printf("File found\n");
            send(new_fd,"HTTP/1.1 200 OK\n",strlen("HTTP/1.1 200 OK\n"),0);
			send(new_fd,"Connection: Keep alive\n",strlen("Connection: Keep alive\n"),0);
			if(strncmp(request_page,".jpg",4)==0){
				send(new_fd,"Content-Type: jpg\n",strlen("Content-Type: jpg\n"),0);
			} else if(strncmp(request_page,".html",4)==0){
				send(new_fd,"Contect-Type: html\n",strlen("Contect-Type: html\n"),0);
			}
			send(new_fd,"\n",strlen("\n"),0);
			sendFile = fopen(request_page,"r");
			while(!feof(sendFile)){
				fgets(send_buffer, MAX_LEN, sendFile);
				if(send(new_fd,send_buffer,sizeof(send_buffer),0)!=sizeof(send_buffer)){
					printf("Sending %s failed\n",request_page);
				}
			}

		} else {
            len = strlen("HTTP/1.1 404 Not Found\n");
			send(new_fd, "HTTP/1.1 404 Not Found\n", len, 0);
			send(new_fd,"Connection: Keep Alive\n",strlen("Connection: Keep Alive\n"),0);
			send(new_fd,"Content-Type: html\n",strlen("Content-Type: html\n"),0);
			send(new_fd,"\n",strlen("\n"),0);
			//read and send the contents of the 404.html file
			if((sendFile = fopen("404.html","r"))<0){
				printf("FILE DID NOT OPEN!\n");
				exit(1);
			}
			while(!feof(sendFile)){
				fgets(send_buffer, MAX_LEN, sendFile);
				if(send(new_fd,send_buffer,sizeof(send_buffer),0)!=sizeof(send_buffer)){
					printf("Sending 404.html Failed\n");
					break;
				}
			}
			printf("Sent file\n");
		}

	} else if(strcmp(request_page, POST)==0){
		// THIS IS WHERE YOU CAN TACKLE POST MESSAGES
	}

	fclose(fread);
	bzero(request_page,MAX_MSG);
	bzero(send_buffer,MAX_MSG);
	remove("Incoming.txt");


    return 0;




}

Number one...are you checking to see if the file opened correctly?

If so then try:

#define MAXLINE 4096

char recvline[MAXLINE];
int connfd;
FILE *fd;

while ((write(connfd, recvline, fread(recvline, 1, MAXLINE, fd))) > 0)
{}

Thanks but I am error checking to see if the file opens or not. Here is my code for one of the functions..ignore the extra printfs - they're there to help me keep track of whats happening:

int Parse_request(int new_fd){

	FILE *fread;
	FILE *sendFile;
	char request_page[MAX_MSG];
	char send_buffer[MAX_LEN];
	struct stat st;
	int i=0;
	int len;

	if((fread = fopen("Incoming.txt","r"))<0){
		printf("Could not read file: Incoming.txt");
		exit(1);
	}
	fscanf(fread, "%s", request_page);

	//check if the request is a GET request
	if(strcmp(request_page, GET)==0){
		fscanf(fread, "%s", request_page);
		printf("The request page is %s\n",request_page);

		//Remove preceeding "/" from the filename
		for(i=0;i<(sizeof(request_page));i++){
			request_page[i] = request_page[i+1];
		}
        //search for the requested file on the system
		if(stat(request_page, &st) == 0){
			printf("File found\n");
            send(new_fd,"HTTP/1.1 200 OK\n",strlen("HTTP/1.1 200 OK\n"),0);
			send(new_fd,"Connection: Keep alive\n",strlen("Connection: Keep alive\n"),0);
			if(strncmp(request_page,".jpg",4)==0){
				send(new_fd,"Content-Type: jpg\n",strlen("Content-Type: jpg\n"),0);
			} else if(strncmp(request_page,".html",4)==0){
				send(new_fd,"Contect-Type: html\n",strlen("Contect-Type: html\n"),0);
			}
			send(new_fd,"\n",strlen("\n"),0);
			sendFile = fopen(request_page,"r");
			while(!feof(sendFile)){
				fgets(send_buffer, MAX_LEN, sendFile);
				if(send(new_fd,send_buffer,sizeof(send_buffer),0)!=sizeof(send_buffer)){
					printf("Sending %s failed\n",request_page);
				}
			}

		} else {
            len = strlen("HTTP/1.1 404 Not Found\n");
			send(new_fd, "HTTP/1.1 404 Not Found\n", len, 0);
			send(new_fd,"Connection: Keep Alive\n",strlen("Connection: Keep Alive\n"),0);
			send(new_fd,"Content-Type: html\n",strlen("Content-Type: html\n"),0);
			send(new_fd,"\n",strlen("\n"),0);
			//read and send the contents of the 404.html file
			if((sendFile = fopen("404.html","r"))<0){
				printf("FILE DID NOT OPEN!\n");
				exit(1);
			}
			while(!feof(sendFile)){
				fgets(send_buffer, MAX_LEN, sendFile);
				if(send(new_fd,send_buffer,sizeof(send_buffer),0)!=sizeof(send_buffer)){
					printf("Sending 404.html Failed\n");
					break;
				}
			}
			printf("Sent file\n");
		}

	} else if(strcmp(request_page, POST)==0){
		// THIS IS WHERE YOU CAN TACKLE POST MESSAGES
	}

	fclose(fread);
	bzero(request_page,MAX_MSG);
	bzero(send_buffer,MAX_MSG);
	remove("Incoming.txt");


    return 0;




}

A bit off topic , but still one suggestion is to avoid using 'fread' as the fp. fread is already a clibrary function and will create confusion. I think its not a good practice.

True I agree. I tried using fread as included in the library but it was creating a lot more drama.

Sorry in my code I've named the file descriptor I've used to open files as fread lol. Prolly a bad choice in naming I admit..

A bit off topic , but still one suggestion is to avoid using 'fread' as the fp. fread is already a clibrary function and will create confusion. I think its not a good practice.

A bit off topic , but still one suggestion is to avoid using 'fread' as the fp. fread is already a clibrary function and will create confusion. I think its not a good practice.

So you think we shouldn't use C library functions in a C program?

while(!feof(sendFile)){
    fgets(send_buffer, MAX_LEN, sendFile);
    send(new_fd,send_buffer,sizeof(send_buffer),0);
}

fgets reads a line. Most likely it is shorter than MAX_LEN. However, send() will transmit MAX_LEN bytes anyway, including all the garbage beyond the end of line.
Change sizeof(send_buffer) to the actual amount of data, e.g. strlen(send_buffer).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.