Advertisement
asqapro

Socket Web Scrape

Jan 2nd, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. bool complete_send(SOCKET sd, const char* data){
  2.     int curr_send = 0;
  3.     int total = 0;
  4.     int bytes_left = strlen(data);
  5.     while((unsigned)total < strlen(data)){
  6.         curr_send = send(sd, data + total, bytes_left, 0);
  7.         if (curr_send == -1){
  8.             return false;
  9.         }
  10.         total += curr_send;
  11.         bytes_left -= curr_send;
  12.     }
  13.     return true;
  14. }
  15.  
  16. void web_scrape(SOCKET sd){
  17.     struct hostent *host;
  18.     host = gethostbyname(url_request.c_str()); //url_request is a global variable, the client sends it in
  19.  
  20.     SOCKADDR_IN site; //Socket address information
  21.  
  22.     site.sin_family = AF_INET; // address family Internet
  23.     site.sin_port = htons (80); //Port to connect on
  24.     site.sin_addr.s_addr = *((unsigned long*)host->h_addr);
  25.  
  26.     SOCKET s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
  27.     if (s == INVALID_SOCKET){
  28.         return; //Couldn't create the socket
  29.     }
  30.  
  31.     //Try connecting...
  32.  
  33.     if (connect(s, (SOCKADDR *)&site, sizeof(site)) == SOCKET_ERROR){
  34.         cout << "Error?" << endl; //does not come here
  35.         return; //Couldn't connect
  36.     }
  37.     else{
  38.         stringstream ss;
  39.         ss << "GET / HTTP/1.1\r\n"
  40.            << "Host:" + url_request + "\r\n"
  41.            << "Connection:close\r\n" //otherwise, connection will stay alive, never stop recv'ing data
  42.            << "\r\n";
  43.         string request = ss.str();
  44.         complete_send(s, request.c_str());
  45.         //send(s, request.c_str(), request.length(), 0);
  46.     }
  47.  
  48.     char buffer[1024];
  49.     string buf_str;
  50.     while(recv(s, buffer, strlen(buffer), 0) > 0){
  51.         cout << "here" << endl; //does not come here
  52.         buf_str += buffer;
  53.     }
  54.     cout << buf_str << endl; //reaches here (prints nothing)
  55.  
  56.     if(buf_str.length() <= 0){
  57.         MESSAGE = "none"; //sends a response back to the client saying nothing was received
  58.         send(sd, MESSAGE.c_str(), (int)(MESSAGE).size(), 0);
  59.     closesocket(s);
  60.         return;
  61.     }
  62.     cout << "here" << endl; //does not get here
  63.     //int cont_len_start = buf_str.find("Content-Length") + 16;
  64.     //int cont_len_end = buf_str.find(" ", cont_len_start + 1);
  65.     //int len = cont_len_end - cont_len_start;
  66.     //int content_length = atoi(buf_str.substr(cont_len_start, len).c_str());
  67.     size_t strip = buf_str.find("\r\n\r\n") + 4;
  68.     buf_str = buf_str.substr(strip);
  69.  
  70.     char buf_str_char[buf_str.length()];
  71.     memcpy(buf_str_char, buf_str.c_str(), buf_str.size());
  72.     complete_send(sd, buf_str_char);
  73.     closesocket(s);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement