Very simple webserver code

I am making a very simple webserver -it does not need to have any sort of cross-platform compliance, it simply needs to work with IE6.  I have the following code which is simply supposed to read in a web page stored on the disk and serve it to the client.  Code follows (I cannot figure out how to format the text.. it's ugly):

void SendLogin(int clientNum){

ifstream file(".\\HTM\\login.htm");

if(file==NULL) return;

int pointer=0;

char ch;

char buf[2000];

pointer = strlen(buf);

while(!file.eof()){

file.get(ch);

if(ch=='\0') break;

if(pointer>=2000) break;

if(ch=='\n') {

buf[pointer++]='\r';

buf[pointer++]='\n';

buf[pointer]='\0';

}

else{

buf[pointer++]=ch;

buf[pointer]='\0';

}

}

buf[strlen(buf)-1]='\0';

cout<<buf<<"\n";

send(client[clientNum],buf,strlen(buf),NULL);

}

 

the problem is that IE displays a "Cannot find Server" error.  Telnet has shown me that IE is receiving the HTML, it just isn't displaying it.



Answer this question

Very simple webserver code

  • chubalot

    if that is indeed the case, then I am at a loss. What would be the proper way to send that information

    Should I send it in a seperate send(), or should I just strcat the HTML onto the end of the HTTP/1.1 200 OK

    In addition, what parts of the reply do I need Of everything listed, which is nessesary and which is not (I recognize that some of it is instance specific, like the content length, date, server)

    HTTP/1.1 200 OK\r\n

    Cache-Control: private\r\n

    Content-Type: text/html\r\n

    Content-Encoding: gzip\r\n

    Server: GWS/2.1\r\n

    Content-Length: 1621\r\n

    Date: Tue, Jan 2006 17:43:42 GMT\r\n

    \r\n

    Content-encoded entity body (gzip)


  • Karlo47

    Obsidian Shadow wrote:
    tried that -I even fired up Ethereal and watched each packet to see what was actually being said back and forth

    So you do not see anything unusual in the data flow

    You could try using a different browser just to be sure it's not an IE specific issue.



  • davidc20

    First of all, how'd you get your code so clean!! it's all together, instead of broken apart like mine :( (kudos for that, lol)

    I have just tried what you suggested with your sent confirmation (I used the actual HTML in place of your stuff in the H1 braces. Still get an error. Ironically, I found a program I wrote in college that does exactly this, and it is coded almost identically.

    I don't really want to write a webserver, this is only meant to be a quick hack that functions via web browser, and this seemed like the easiest way to do it (since I wouldn't have to write a client). I tried studying the RFC, but was unable to find any useful information that wouldn't take me a few years to finish reading.


  • David.Dong

    The only difference between communication between my server and www.google.ca is that mine does not send:

    HTTP/1.1 200 OK\r\n

    Cache-Cntrol: private\r\n

    Content-Type: text/html\r\n

    ...etc

    It just jumps straight to the HTML code. Might that be the problem


  • Pinei

    specifically, I type the following:

     

    o 127.0.0.1 80 (open localhost on port 80)

    then in a different segment of code, it blocks and waits for input, terminated by either a nullbyte or a \n, which works

    I then type GET / http/1.1 (although at this point that does not matter, as I am hardcoding the returned page to make debugging faster)

    After which, it prints out the HTML exactly as it is in the file.  It then closes the thread (and the socket) and waits for a new connection.


  • Mahesh B. Mane

     Obsidian Shadow wrote:

    if that is indeed the case, then I am at a loss.  What would be the proper way to send that information

    Should I send it in a seperate send(), or should I just strcat the HTML onto the end of the HTTP/1.1 200 OK

    In addition, what parts of the reply do I need   Of everything listed, which is nessesary and which is not (I recognize that some of it is instance specific, like the content length, date, server)

    HTTP/1.1 200 OK\r\n

    Cache-Control: private\r\n

    Content-Type: text/html\r\n

    Content-Encoding: gzip\r\n

    Server: GWS/2.1\r\n

    Content-Length: 1621\r\n

    Date: Tue, Jan 2006 17:43:42 GMT\r\n

    \r\n

    Content-encoded entity body (gzip)

    Here's a quickly put together piece of code that simulates a web server. It uses MFC sockets though - but it should give you an idea.

    CSocket server;
    if(server.Create(900))
    {
     if(server.Listen())
     {
      CSocket client;
      server.Accept(client);
      char buff[100];
      int n = 0;
      while(n = client.Receive(buff,99))
      {     
       if(n < 99)
       {      
        break;
       }
      };    
      strcpy(buff,"HTTP/1.1 200 OK\r\n\n<h1>Hello There</h1>");
      client.Send(buff,strlen(buff));
     }
    }

    Note that if you really want to write a web server, you should brush up on Winsock a little more and also study the HTTP RFC properly.



  • Aquilax

    tried that -I even fired up Ethereal and watched each packet to see what was actually being said back and forth
  • puso

    Obsidian Shadow wrote:

    specifically, I type the following:

    o 127.0.0.1 80 (open localhost on port 80)

    then in a different segment of code, it blocks and waits for input, terminated by either a nullbyte or a \n, which works

    I then type GET / http/1.1 (although at this point that does not matter, as I am hardcoding the returned page to make debugging faster)

    After which, it prints out the HTML exactly as it is in the file. It then closes the thread (and the socket) and waits for a new connection.

    Looks like IE is sending some request that your app is not correctly handling, and thus it may not be sending anything back to IE, and after a while IE is timing out. You should set breakpoints in your code, where the socket connection is established and trace through it.



  • Jolo

    What do you do with telnet To simulate what IE does, you need to telnet to port 80, and give a GET request for that specific page.

  • fooguru

    Obsidian Shadow wrote:

    The only difference between communication between my server and www.google.ca is that mine does not send:

    HTTP/1.1 200 OK\r\n

    Cache-Cntrol: private\r\n

    Content-Type: text/html\r\n

    ...etc

    It just jumps straight to the HTML code. Might that be the problem

    Yes, that is the problem. The browser needs a 200 response to know that the GET request has been successfully processed.



  • Very simple webserver code