Winsock HTML Page Source Dump...

This is a discussion on Winsock HTML Page Source Dump... within the Networking/Device Communication forums, part of the General Programming Boards category; Code: ofstream dparse ("d2jsp.txt"); do { dRecv = recv(D2JSP, dbuf, 512, 0); dbuf[dRecv] = '

Thread: Winsock HTML Page Source Dump...

'; if (dRecv == SOCKET_ERROR) { ...

  1. #16
    Registered User
    Join Date
    Nov 2007
    Posts
    17
    Code:
        ofstream dparse ("d2jsp.txt");
        do {
           dRecv = recv(D2JSP, dbuf, 512, 0);
           dbuf[dRecv] = '\0';
           
           if (dRecv == SOCKET_ERROR) {
           cout<<"Failed to recieve data through D2JSP... " << WSAGetLastError() << endl;
           shutdown(D2JSP, 2); closesocket(D2JSP); WSACleanup();
           }
           else {
                if (!dparse.is_open()) { cout<<"Failed to open d2jsp.txt...\n"; break; }
                else if (dparse.is_open()) dparse << dbuf;
                }
                   
           } while (dRecv > 0);
                           
        dparse.close();
        system("pause");
        
        shutdown(D2JSP, 2); closesocket(D2JSP); WSACleanup();
        return 0;
    }

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
           dRecv = recv(D2JSP, dbuf, 512, 0);
           dbuf[dRecv] = '\0';
    if recv fills the array, then you write one past the end of the array.
    Code:
                if (!dparse.is_open()) { cout<<"Failed to open d2jsp.txt...\n"; break; }
                else if (dparse.is_open()) dparse << dbuf; /* unneeded */

  3. #18
    Registered User
    Join Date
    Nov 2007
    Posts
    17
    huh? Anyways, is there anyone out there that can answer my question instead of trying to correct everything I write? Clearly, this isn't solving my problem.. Thanks...

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You have bugs in your program. How do you know that those bugs aren't causing your problems? You'll have to fix these things anyway.

  5. #20
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Anyways, is there anyone out there that can answer my question instead of trying to correct everything I write? Clearly, this isn't solving my problem..
    Huh? Do you expect incorrect code to function correctly? robwhit's advice was on the mark - I'd only add to it that you should check for error conditions before using recv()'s return value to append a 0 to the end of your array.
    Otherwise, how is correcting your code not solving your problem? If you post your code, as it stands now, we can tell you what's wrong with it (or what we need to know to help). If our suggestions don't work, then repeat the cycle - sometimes there's more than one bug to flush out.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #21
    Registered User
    Join Date
    Nov 2007
    Posts
    17
    I would like to know how any of this is incorrect or is "buggy"...
    Code:
    void dSend (int D2JSP, char* request) {
         
         if (send(D2JSP, request, strlen(request), 0) == SOCKET_ERROR) {
                         cout<<"Failed to send request to D2JSP... " << WSAGetLastError() << endl;
                         shutdown(D2JSP, 2); closesocket(D2JSP); WSACleanup(); exit(0);
                         }
                         cout<< request << endl;
    }
    
    int main()
    {  
        WSADATA WsaDat;
        SOCKET D2JSP;
        sockaddr_in D2;
        
        hostent* dHost;
        
        char *dIP, *request, dbuf[512];
        int dRecv;
        unsigned short dline = 0, cwrite = 0;
        request = "GET /index.php?showforum=168 HTTP/1.1\r\nHost: forums.d2jsp.org\r\n\r\n";
        
        if (WSAStartup(MAKEWORD(2, 0), &WsaDat) != 0) {
                                   cout<<"WSAStartup failed to initalize...\n"; return 0;
                                   }
        D2JSP = socket(AF_INET, SOCK_STREAM, 0);
        if (D2JSP == INVALID_SOCKET) {
                  cout<<"Failed to make D2JSP socket... " << WSAGetLastError() << endl;
                  WSACleanup(); return 0;
                  }
        
        dHost = gethostbyname("forums.d2jsp.org");
        dIP = inet_ntoa (*(in_addr*) dHost->h_addr);
        cout<<"D2JSP IP: " << dIP << endl;
        
        D2.sin_family = AF_INET;
        D2.sin_addr.s_addr = inet_addr (dIP);
        D2.sin_port = htons (80);
        
        if (connect(D2JSP, (sockaddr*) &D2, sizeof(D2)) == INVALID_SOCKET) {
                           cout<<"Failed to connect to D2JSP socket... " << WSAGetLastError() << endl;
                           shutdown(D2JSP, 2); closesocket(D2JSP); WSACleanup(); return 0;
                           }
        
        dSend(D2JSP, request);
        
        ofstream dparse ("d2jsp.txt");
        do {
    dRecv = recv(D2JSP, dbuf, 512, 0);       
    if (dRecv == SOCKET_ERROR) {
           cout<<"Failed to recieve data through D2JSP... " << WSAGetLastError() << endl;
           break;
           }       
           dbuf[dRecv] = '\0';
                if (!dparse.is_open()) { cout<<"Failed to open d2jsp.txt...\n"; break; }
                else if (dparse.is_open()) dparse << dbuf;
                }
                   
           } while (dRecv > 0);
                           
        dparse.close();
        shutdown(D2JSP, 2); closesocket(D2JSP); WSACleanup();
        return 0;
    }
    For the changes that was made thanks to the other guys, it has the same behavior but dont think i dont appericate the clean up but i have the simple winsock function and i keep gettin cut lines.
    Last edited by blake_; 11-18-2007 at 07:28 PM.

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    dbuf[dRecv] is an out of bounds access.

    0-511 are valid, 512 is not.

    also, when you copy a pointer, it doesn't copy the string.
    Code:
    dIP = inet_ntoa (*(in_addr*) dHost->h_addr);

  8. #23
    Registered User
    Join Date
    Nov 2007
    Posts
    17
    i dont know what you mean by dIP.. it has that same line in the examples on the winsock reference. it works just fine and for dbuf its not giving me any runtime errors so it must not be appending it on 512 but i dont know i could dRecv-1

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by blake_ View Post
    i dont know what you mean by dIP..
    that's your code. you should make it an array and strcpy it instead. Well, that's according to the docs at msdn. see post #8.
    i could dRecv-1
    good idea. make sure to change recv too, or you'll lose a byte.

  10. #25
    Registered User
    Join Date
    Nov 2007
    Posts
    17
    ok so now what do i do so i stop getting incomplete lines?

  11. #26
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Post the modified code, please.

  12. #27
    Registered User
    Join Date
    Nov 2007
    Posts
    17
    Code:
    int main()
    { 
        WSADATA WsaDat;
        SOCKET D2JSP;
        sockaddr_in D2;
        
        hostent* dHost;
        
        char *dIP, *request, dbuf[513];
        int dRecv;
        unsigned short dline = 0, cwrite = 0;
        request = "GET /index.php?showforum=168 HTTP/1.1\r\nHost: forums.d2jsp.org\r\n\r\n";
        
        if (WSAStartup(MAKEWORD(2, 0), &WsaDat) != 0) {
                                   cout<<"WSAStartup failed to initalize...\n";
                                   }
        D2JSP = socket(AF_INET, SOCK_STREAM, 0);
        if (D2JSP == INVALID_SOCKET) {
                  cout<<"Failed to make D2JSP socket... " << WSAGetLastError() << endl;
                  WSACleanup();
                  }
        
        dHost = gethostbyname("forums.d2jsp.org");
        dIP = inet_ntoa (*(in_addr*) dHost->h_addr);
        cout<<"D2JSP IP: " << dIP << endl;
        
        D2.sin_family = AF_INET;
        D2.sin_addr.s_addr = inet_addr (dIP);
        D2.sin_port = htons (80);
        
        if (connect(D2JSP, (sockaddr*) &D2, sizeof(D2)) == INVALID_SOCKET) {
                           cout<<"Failed to connect to D2JSP socket... " << WSAGetLastError() << endl;
                           shutdown(D2JSP, 2); closesocket(D2JSP); WSACleanup();
                           }
        
        dSend(D2JSP, request);
        
        ofstream dparse ("d2jsp.txt");
        do {
           dRecv = recv(D2JSP, dbuf, 512, 0);
           if (dRecv == SOCKET_ERROR) {
              cout<<"Failed to recieve data through D2JSP... " << WSAGetLastError() << endl;
              break;
              }
              dbuf[dRecv+1] = '\0';
              if (!dparse.is_open()) { cout<<"Failed to open d2jsp.txt...\n"; break; }
              else dparse << dbuf;
           } while (dRecv > 0);
                           
        dparse.close();
        shutdown(D2JSP, 2); closesocket(D2JSP); WSACleanup();
        return 0;
    }

  13. #28
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    dbuf[dRecv+1] = '\0';
    No!

    This does the same thing as you did before.

    http://www.cprogramming.com/tutorial/c/lesson8.html

    and put the returns/exits back.

  14. #29
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    The salient lines, which you still seem to be troubling over
    Code:
    char dbuf[513];
    
    dRecv = recv(D2JSP, dbuf, sizeof dbuf - 1, 0); /* -1 to allow a \0 to be added later */
    
    // if successful, do this to make a proper string
    dbuf[dRecv] = '\0';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #30
    Registered User
    Join Date
    Nov 2007
    Posts
    17
    How is your way any different then my way? You're pretty much just -1 by 513 which equals to 512 like I kept in my recv, so it doesnt add anything on 513 so once it hits 512 then it will add a \0 on 513.. And on your way it looks like ill still lose a byte on 512 since you're adding it on 512 if it reaches the end..
    Last edited by blake_; 11-19-2007 at 09:29 AM.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 08:42 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. requesting html source from a server
    By threahdead in forum Linux Programming
    Replies: 2
    Last Post: 08-01-2003, 08:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21