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. But am not sure what else to do =s. Please help!
At the end of the code I'm closing and clearing out all the files and buffers.
while(!feof(sendFile)){
fgets(send_buffer, MAX_LEN, sendFile);
send(new_fd,send_buffer,sizeof(send_buffer),0);
}
Here is the function I am trying to implement. This is just to return the http 404 error 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);
//read and send the contents of the 404.html file
//open file
if((sendFile = fopen("404.html","r"))<0){
printf("FILE DID NOT OPEN!\n");
exit(1);
}
//obtain file size
fseek (sendFile , 0 , SEEK_END);
Fsize = ftell (sendFile);
rewind (sendFile);
/* // allocate memory to contain the whole file:
send_buffer = (char*) malloc (sizeof(char)*Fsize);
if(send_buffer == NULL){
printf("Memory error");
exit (1);
}
// copy the file into the buffer:
result = fread (send_buffer,1,Fsize,sendFile);
if(result != Fsize) {
printf("Reading error");
exit (1);
}
*/
send(new_fd,"Content-Length: ",strlen("Content-Length: "),0);
send(new_fd,int(Fsize),4,0); //this line is causing errors!!!!
send(new_fd,"\n",strlen("\n"),0);
while(!feof(sendFile)){
bzero(send_buffer,MAX_MSG);
fgets(send_buffer, sizeof(send_buffer), sendFile);
//result = send(new_fd,send_buffer,strlen(send_buffer),0);
if(send(new_fd,send_buffer,sizeof(send_buffer),0)!=sizeof(send_buffer)){
printf("Sending 404.html Failed\n");
break;
}
}
fclose(sendFile);
printf("Sent file\n");
}
} else if(strcmp(request_page, POST)==0){
// THIS IS WHERE YOU CAN TACKLE POST MESSAGES
}