while(1){
new=accept(socket, (struct sockaddr*)&peer_addr,&peer_addrlen);
if (new<0){
break;
}
else{
recv(new,mesg,sizeof(mesg),0);
if (strstr(mesg, "GET") != NULL){
printf("\n%s",mesg);
FILE *write = fdopen(new, "w");
fprintf(write, "HTTP/1.1 200 OK\nContent-length: 47\nContent-Type: text/html\n\n<html><body>< H1>HTTP/1.1 200 OK</H1></body></html>");
fflush(write);
}
else if (strstr(mesg, "POST") != NULL){
strcpy(resp,"POST\nContent-length: 47\nContent-Type: text/html\n\n<html><body><H1>POST</H1></body></html>");
c=send(new,&resp,sizeof(resp),0);
printf("%c",c);
}
else{
strcpy(resp,"HTTP/1.1 500 Server Interval Error\nContent-length: 47\nContent-Type: text/html\n\n<html><body><H1>HTTP/1.1 Server Interval Error</H1></body></html>");
c=send(new,&resp,sizeof(resp),0);
printf("%c",c);
}
}
}
I am trying to do a http server that will connect with a browser and sends to it html code for a directory. The theme is that i've got no idea about how i will send the html code through the socket. I thoought that it could be something like this:
fprintf(write,"\n\n<html><head><title>My first page</title><link rel="stylesheet" href="kl.css" type="text/css"></head><body> Hello world!</body></html>");
But obviously it doesn't work, any idea?