Thread: Socket web browser

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    21

    Socket web browser

    I am trying to create a simple web browser, but I have not found any tutorials. I have read a socket tutorial (but it is not on creating a web browser), and the code I give below is what I have come with. I do not know why it does not work at all. It kind of stops at the receive part. I used a packet sniffer and saw that I was sending some data to the site address, but I am not getting the webpage HTML code back. Can somebody tell me what I am doing wrong?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <winsock.h>
    
    WSADATA wsaData;
    char buf[100];
    int socketsrc, bytesRecv, bytesSent;
    struct sockaddr_in dest_addr;
    struct hostent *h;
    char *msg = "GET / HTTP/1.1";
    
    int main(int argc, char *argv[])
    {
        if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
    {
        fprintf(stderr, "WSAStartup failed.\n");
        exit(1);
        }
        if ((socketsrc = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
         perror("socket");
         exit(1);
         }
        printf("\nsuccess: socket");
    
         if (argc != 2) { // error check the command line
         fprintf(stderr,"usage: getip address\n");
         exit(1);
         }
         if ((h=gethostbyname(argv[1])) == NULL) { // get the host info
         printf("\nNo Host for %s", argv[1]);
         exit(1);
         }
        printf("\nsuccess: hostname");
        dest_addr.sin_family = AF_INET; // host byte order
        dest_addr.sin_port = htons(80); // short, network byte order
        dest_addr.sin_addr.s_addr = inet_addr("216.239.57.99");
        memset(&(dest_addr.sin_zero), 0, 8); // zero the rest of the struct
        
        if (connect(socketsrc, (struct sockaddr *)&dest_addr,
        sizeof(struct sockaddr)) == -1) {
        perror("connect");
        exit(1);
        }
        printf("\nsuccess: connect");
            
        bytesSent = send(socketsrc, msg, strlen(msg), 0);
        printf( "\nBytes Sent: %d", bytesSent );
    
        //and this is where I think the program just stops
    
        while((bytesRecv = recv( socketsrc, buf, 99, 0 )) != -1 ) {
          if ( bytesRecv == 0) {
           printf( "\nConnection Closed.");
           break;
          }
        printf( "\nBytes Recv: %d", bytesRecv );
        }
    
        printf("\nsuccess: recieve");
        closesocket(socketsrc);
    
        WSACleanup();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Moved to the Networking Board.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Perhaps your request (char* msg) should be more like "GET / HTTP/1.1\r\nHost: www.thesitehere.com\r\n\r\n" at a bare minimum. \r\n is a crlf (google), like pressing enter in a telnet/ssh client. Otherwise this looks pretty clean. I am pretty fond of this tutorial for your basic winsock blocking client (C++): http://madwizard.org/view.php?page=t...pter6&lang=cpp whereas your code is pure C off MSDN Hope changing your HTTP request works! Also, the strings you recv from your socket are not null-terminated (don't want 00 lying about image data). If you wish to print them, you might for instance:

    Code:
    	recvBuffer[iRet] = 0x00;
    	std::cout << recvBuffer;
    Lines up so perfectly.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    21

    Smile

    Wow! Thanks, now I am finally getting some results!
    I didn't even know I was just writing in pure C -.-

    Unfortunately, since I am kind of a beginner to C, I do not really understand your code. I can hardly even understand my code Well like I can tell what the second line does, which is display the string, but I do not know what 0x00 means, and not even how to get the string from the recv().

    I am improving though, at least I hope so. I will try to follow the tutorial that you suggested, since it seems that it is what I am trying to do. Maybe I will be able to understand all this stuff soon.

    Thanks for the quick reply!

    I also have a really really noob question:
    How do I get the cin and cout to work? It just says that it is undeclared...

    I am trying to use this code:
    Code:
    cout<<Starting Up;
    but this doesn't work either
    Code:
    std::cout<<Starting Up;
    I think you can already tell I do not know too much about c++.
    I've been too spoiled by PHP.. -.-
    Plus I never used classes in PHP cause I never found any use for them.

    I hope I haven't scared you away with so many questions
    The only things I think I need is why cout doesn't work and what recvBuffer[iRet] = 0x00 is. I will try to figure out the rest with the tutorial you gave.

    Many Thanks!

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    '\0' == 0 == 0x00 == \x00 == \000 It is all exactly the same. I chose 0x00 to make it line up nicely and make it look cool. I was very into that. To use cout in your app, you would #include <iostream>. C++ headers do not end in a .h Check out this brief tutorial: http://cplus.about.com/od/beginnerct.../aa022302a.htm but don't feel obligated to throw yourself into C++, if you're comfortable (or somewhat more so) with C at the time-being.
    Last edited by Tonto; 09-21-2005 at 11:19 PM.

  6. #6
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Quote Originally Posted by digitaltsai
    Code:
    cout<<Starting Up;
    How can "Starting Up" be a single variable? That isn't a legal variable name, notice the space in between. If it is two different variables you need to add an extra "<<" in between the two names. Maybe that's why that "cout" code wasn't working?
    Try something like this:
    Code:
    cout<<Starting_Up;
    //or if they are two variables try this
    cout<<Starting<<Up;
    Just wanted to point that out.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    O bleh.
    I meant to put quotes around that
    the include iostream works!
    I don't see why we need to include all these files. Why not just include like one file that is a combination of everything?

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    How do I set the temporary buffer in the receive to be the size of the incoming data?
    [edit]
    Nevermind, figured that it doesn't matter

    Now I just have to figure out why the last buffer is so weird...
    Last edited by digitaltsai; 09-22-2005 at 09:31 PM.

  9. #9
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    How is it weird, and what are you storing into it? Which buffer are you talking about?
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    buffer of the recv
    It give me back the correct stuff, then a 0, then repeats part of the correct stuff again. <-all this is in the last buffer

  11. #11
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Might we see the new code you're using? We're not psychic!

  12. #12
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Well have you tried clearing that buffer's memory? Maybe it's just appending it each time it receives. That's my best guess without code to look at.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    I found the problem!
    This is kind of dumb, since Tonto made a statement about the recv buffer not being null-terminated. I did not know what a string not being null-terminated meant before so I thought he meant something about the while loop never ending since it will never receive NULL. Now that you are thoroughly confused about what I was saying, here was my problem:
    Code:
    buf[bytesRecv] == 0;
    Here is my code if someone finds this thread and wants to create something to get the HTML from a site:
    http://lelements.net/files/gethtml.txt
    *Use at your own risk, just because it works for me doesn't mean it works for you.

    How would I read the individual bytes of the buffer I receive in HEX?
    How would I send a packet in HEX also?

    Thanks for all your helps!

  14. #14
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Do you mean bytes or characters? Like you receive a buffer full of different characters and want to pick out a single one, is that what you mean?
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Please, If you are a beginner at C, then don't attempt to write this. You started out by saying there are no tutorials on building web browsers. This is true because writing a web browser is increidbly complex. If you are capable of writing a web browser then you don't need a tutorial.
    Sockets also have zero to do with C. If you are new to C then you shouldn't be writing a socket application. Learn the language first, then learn third party things, such as sockets.
    By this point you are ready to say to me: I've started this application and have made some headway in this program. After I finish this I'll go back and learn C properly. Besides, I learn things better by jumping in over my head.
    My response to that? Bull. Without a strong grasp of C you are just going to be asking a lot of annoying questions to get yoru program to work. Such as:
    How would I read the individual bytes of the buffer I receive in HEX?
    How would I send a packet in HEX also?
    These questions are silly.
    Go learn C, come back later. I know you don't want to hear that but hey, that's life, deal with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Web browser based program
    By Lauris in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2007, 05:01 PM
  2. HTTP Response from web browser
    By zort15 in forum C# Programming
    Replies: 0
    Last Post: 06-25-2007, 08:35 AM
  3. Web Browser Control Help
    By dcboy in forum C# Programming
    Replies: 0
    Last Post: 10-22-2006, 09:25 AM
  4. Amaya web browser
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-10-2005, 08:17 PM