0
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?

2
Contributors
1
Reply
15
Views
3 Years
Discussion Span
Last Post by rubberman
0

Any reason why you are using C for this? It isn't simple to do in C. I would recommend that you get the Mozilla Firefox 2.x code base (written in C/C++) and study how they do it. Also, I am doing much of what you are, but am using PHP instead of C/C++ because it has some decent HTTP plugins to do all this stuff and supports HTML nicely. Yes, usually PHP is used in conjunction with a web server such as Apache, but the 5.5+ versions of PHP can serve as a stand-alone web server. I use it that way to emulate a cell phone browser for performance testing purposes.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.