Thread: Program is hanging

  1. #1
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121

    Program is hanging

    Hello all,
    I am trying to make a program to get html code from a web site. I have this code which is partly taken from another example i found on the internet. My problem is that when i run the program it hangs... for 10 min. I just cancelled it after that.

    Can anyone tell me why the program is hanging or if maybe something in my code is wrong?

    Code:
    #include <iostream>
    #include <windows.h>
    #include <winsock2.h>
    #include <string>
    using namespace std;
    
    int main()
    {
        WSADATA wsaData;
        WORD version;
        int error;
        
        version = MAKEWORD(2, 0);
        
        error = WSAStartup(version, &wsaData);
        
        if(error != 0)
        {
            return FALSE;
        }
        
        if( LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) 
        {
            WSACleanup();
            return FALSE;
        }
        
        cout << "Winsock initialized" << endl;
        
        SOCKET client;
        client = socket(AF_INET, SOCK_STREAM, 0);
        
        if(client == INVALID_SOCKET)
        {
            cout << "Socket init failed" << endl;
            
            fflush(stdin);
            cin.get();
            WSACleanup();
            return FALSE;
        }
        
        sockaddr_in sockin;
        sockin.sin_port = htons(80);
        sockin.sin_addr.s_addr = inet_addr("216.239.136.165");
        sockin.sin_family = AF_INET;
        
        if(connect(client, (sockaddr*)&sockin, sizeof(sockin)) == SOCKET_ERROR)
        {
            cout << "Connection failed: " << WSAGetLastError();
            fflush(stdin);
            cin.get();
            WSACleanup();
            
            return FALSE;
        }
        else
        {
            cout << "Connection made to 216.239.136.165" << endl;
        }
        cout << "---------------------------" << endl;
        
        char buffer[1024];
        
        /*for(int i = 0; i < 1024; i++)
        {
            buffer[i] = '\0';
        }*/
        
        char welcome[] = "GET / HTTP/1.1\r\n";
        strcat(welcome, "Host: asdf.com\r\n");
        strcat(welcome, "Connection: close\r\n");
        //strcat(welcome, "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,/;q=0.5\r\n");
        //strcat(welcome, "Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3\r\n");
        //strcat(welcome, "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n");
        //strcat(welcome, "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3\r\n");
        //strcat(welcome, "Referer: http://pozzyx.net/\r\n");
        strcat(welcome, "\r\n");
        
        cout << "Request:: " << endl << welcome << endl;    
        
        send(client, welcome, sizeof(welcome), 0);
        
        string Source = "";
        
        int i = 0;
        do
        {
            i = recv(client, buffer, sizeof(buffer), 0);
            Source += buffer;
            
        }while (i != 0);
        
        cout << Source << endl;
        
        cout << endl << "Done." << endl;
        
        WSACleanup();
        fflush(stdin);
        cin.get();
    }
    -- Will you show me how to c++?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    // char welcome[] = "GET / HTTP/1.1\r\n";

    There isn't nearly enough room in there to fit all of your data. Suggestion: store it in an std::string.

    // i = recv(client, buffer, sizeof(buffer), 0);

    recv is going to return either the number of bytes read (could be zero!), or SOCKET_ERROR (which is normally defined as -1). So the typical strategy is to recv until the buffer is partially filled, or an error occurs. Don't forget to null terminate the buffer - recv will not do this for you!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Also in troubleshooting such matters are you able to run WireShark perhaps from your Windows client to see how you are actually connecting and insuring you are getting data back? WireShark is a free app for Windows. I use tcpdump on my Linux distros. When writing network apps, there may be nothing wrong with your code at all and now you may have to troubleshoot why something is not working on the network. WireShark can give you a ton of data as far as payload size and such.

  4. #4
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    Thank you guys. I have wireshark but didnt even think to use it last night it was so late. I will try both ideas and get back to you in a few. Thank you again!
    -- Will you show me how to c++?

  5. #5
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    So this worked...
    Code:
        string welcome = "GET /about/sites.html HTTP/1.1\r\n";
        welcome += "Host: www.craigslist.org\r\n";
        welcome += "Connection: close\r\n";
        welcome += "\r\n";
    but this did not...
    Code:
        char welcome[] = "GET /about/sites.html HTTP/1.1\r\n";
        strcat(welcome, "Host: www.craigslist.org\r\n");
        strcat(welcome, "Connection: close\r\n");
        strcat(welcome, "\r\n");
    how come?
    -- Will you show me how to c++?

  6. #6
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    Well it works now. Thank you guys alot. The problem was what i think you said Sebastiani... there wasnt enough room in the request array. So what instead of having

    Code:
    char welcome[] = "GET / HTTP/1.1";
    strcat(welcome, "Host: www.craigslist.org");
    I needed:
    Code:
    char welcome[4098] = "GET / HTTP/1.1";
    strcat(welcome, "Host: www.craigslist.org");
    I was concatenating past the end of my array.
    -- Will you show me how to c++?

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    You could of just done:

    Code:
    char welcome[] = "GET /about/sites.html HTTP/1.1\r\n"
                    "Host: www.craigslist.org\r\n"
                    "Connection: close\r\n"
                    "\r\n";

  8. #8
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    Haha, yes you are right. I cant believe i didnt think of that. Thank you
    -- Will you show me how to c++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Can't figure out what keeps hanging up my program
    By shays in forum C Programming
    Replies: 7
    Last Post: 11-12-2007, 02:59 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM