Results 1 to 8 of 8

Thread: Building a webserver (VC++/MFC)

  1. #1
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    350

    Post Building a webserver (VC++/MFC)

    I am trying to build a webserver. I know the basics of HTTP headers and how a client/server situation works. I know that if I have my server running (and it is working correctly), then when IE connects to 127.0.0.1, then the server replies with HTML. I cannot get my server to send HTML...or even get IE to display a different error page.

    I need help on how to send HTTP headers with VC++. I have included the "OnStart()" function. My dialogue right now is stripped of nothing but this button really. When you click it, it creates a socket, listens on the socket, and accepts any connections. I think my problem actually lies in Accepting the socket, because that part seems to fail if I use:

    Code:
    if(!m_server.Accept(m_server2))
    {
          MessageBox("Error Creating Socket!");
    }
    Here is the function:

    Code:
    void CWebserverDlg::OnStart() 
    {
    	// TODO: Add your control notification handler code here
    	
    	CString m_peer;
    	UINT m_port;
    	char m_head[150] = "HTTP/1.1 200 OK\nCache-Control:·private\nContent-Type:·text/html\nServer:·God/0.5";
    	char m_html[50] = "<html>Biotch!</html>";
    
    	if(!m_server.Create())
    	{
    		MessageBox("Could not create connection!");
    	}
    	else
    	{
    		m_server.Listen();
    		m_server.Accept(m_server2);
    		m_server2.Send(m_head,151);
    		m_server2.Send(m_html, 51);
    	}
    		
    }
    Geek isn't just a four-letter word; it's a six-figure income.

  2. #2
    Senior Member
    Join Date
    Dec 2004
    Posts
    3,171
    If this helps...great...

    http://www.google.ca/search?client=f...=Google+Search
    Google Search: how to send HTTP headers with VC++.

    if not...I hope someone else can.

  3. #3
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    350
    tried that one, along with other similar queries. thanks though.

    A_T
    Geek isn't just a four-letter word; it's a six-figure income.

  4. #4
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    In reference to IE not displaying the right error page, I think IE just uses its own. I have the same problem with other site, I like to see their error page, but IE gives me its own . I tried to make a webserver in C not long ago, I got it to work somewhat, but left it to sit for some other time to make it better :P. I don't know anything about VC++ or MFC though, just wanted to add that about IE, so use firefox or something when you're checking your error pages.

  5. #5
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    Originally posted here by h3r3tic
    In reference to IE not displaying the right error page, I think IE just uses its own. I have the same problem with other site, I like to see their error page, but IE gives me its own .
    Turn off "Friendly HTTP error messages" in the Advanced options of IE.

    As for the headers; you're missing an empty line between the headers and the html. The return code is also wrong.

    It should return (atleast):
    Code:
    200 OK
    Content-Type: text/html
    
    <html>test</html>
    You also need to use "\r\n" (carriage return AND linefeed) at the end of the lines.

    Checking should be done with telnet or some other program. Neither IE nor FF will show the headers. Just telnet to 127.0.0.1 port 80 and type HEAD / HTTP/1.0 and hit enter twice.

    Oliver's Law:
    Experience is something you don't get until just after you need it.

  6. #6
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Let us focus on your webserver. It looks to me from your code-snippet
    that you are using CAsyncSocket[1]. Note, that this is a wrapper of the
    WinSock-API[2] only (I do not recommend CSocket):

    Code:
    .Create()   : combination of socket() and bind()
    .Listen()   : listen()
    .Accept()   : accept()
    .Send()     : send()
    .Receive()  : recv()

    What is the corresponding Client-Server process

    - The server is listening on port 80
    - The client sends a request to port 80 on the server machine
    - The server is parsing that request and sends back an answer
    - The client is receiving that answer, parses it and "displays" it

    In detail:
    The server accepts a connection and creates a new socket "socket_client".
    It then receives that message. This seems to be missing in your program.
    Finally, the server sends an appropriately formatted answer to the "socket_client"!

    An example of a webserver

    Find attached a working webserver (link with WSock32.lib). I have written it
    quickly in order to illustrate the meaning of the client-server process, and
    not to eliminate possible security issues (buffer overflows). It is not using
    MFC and CAsyncSocket, but WinSock, because it allows for a simpler step-by-step
    debugging. But by applying the wrapper-definitions I have given above, you can
    easily port that to your application. Check in detail the receiving/sending logic.

    Have fun!

    Cheers


    [1] http://msdn.microsoft.com/library/de...a3a.listen.asp
    [2] http://msdn.microsoft.com/library/de..._functions.asp
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  7. #7
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    350
    Thanks for the replies. I plan to do some more work on this in the upcoming week or two. I'll let you all know how it works out.

    sec_ware - where did you learn winsocks? I couldn't find too many decent tutorials on it, which is why I went with MFC. Maybe you could write a short tutorial for C++?

    A_T
    Geek isn't just a four-letter word; it's a six-figure income.

  8. #8
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi A_T

    I really cannot recall how I learned this kind of programming.
    Find some references here[1]. Maybe try to learn with an as
    simple setup as possible (command line program rather than
    MFC's). Here, you can focus on the real issue, without having
    to fight with Visual components. After you have "mastered" that,
    it is not so difficult to expand onto CAsyncSocket and MFC etc.

    Maybe you could write a short tutorial for C++?
    I'll think about that

    Cheers

    [1] http://www.antionline.com/showthread...r=1#post802703
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •