I am trying to write a simple Webserver in C. As of now I can receive connections and receive messages in full. However, in accordance with the HTTP/1.0 protocol, I want to be able to send information back to the client when the "\r\n\r\n" sequence is encountered. However, when using Telnet to test my server, when I enter "\r\n\r\n", the server does nothing until I hit "^]" on the client. I tested this against Apache and Apache does not have this problem. So I was hoping to some information on how to mimic the Apache behavior. My code is added below but please keep in mind I am no where near done nor have I implemented a lot of error checking. Thanks!
main(){
int sock_fd = 0;
int client_fd = 0;
struct sockaddr_in socket_struct;
/*Creates the socket*/
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
exit(EXIT_FAILURE);
}/*Ends the socket creation*/
/*Populates the socket address structure*/
socket_struct.sin_family = AF_INET;
socket_struct.sin_addr.s_addr=INADDR_ANY;
socket_struct.sin_port =htons(port);
if (bind(sock_fd, (struct sockaddr*) &socket_struct, sizeof(socket_struct)) < 0)
{
fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
exit(EXIT_FAILURE);
}//Ends the binding.
if (listen(sock_fd, 5) <0)
{
fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
exit(EXIT_FAILURE);
}//Ends the listening function
if ( (client_fd = accept(sock_fd, NULL, NULL)) <0)
{
fprintf(stderr, "%s: %s\n", getprogname(), strerror(errno));
exit(EXIT_FAILURE);
}//Ends the accepting.
while ( (size = read(client_fd, msg, 1024)) > 0)
{
//size = recv(client_fd, msg, 1024, MSG_PEEK|MSG_WAITALL);
if ( (msg[size-4] == 13) && (msg[size-3] == 10)&&(msg[size-2] == 13) && (msg[size-1] == 10) )
{
char* buffer = (char *)malloc(sizeof("The msg was: ")+ sizeof(msg));
sprintf(buffer, "The msg was: %s", msg);
send(client_fd, buffer, sizeof("The msg was: ")+ sizeof(msg), MSG_OOB);
}
}//ends the while loop for receiving data
close(client_fd);
}
printf()and see if it enters theif()condition inside thewhileloop. Seems like there is a problem @send()