Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool complete_send(SOCKET sd, const char* data){
- int curr_send = 0;
- int total = 0;
- int bytes_left = strlen(data);
- while((unsigned)total < strlen(data)){
- curr_send = send(sd, data + total, bytes_left, 0);
- if (curr_send == -1){
- return false;
- }
- total += curr_send;
- bytes_left -= curr_send;
- }
- return true;
- }
- void web_scrape(SOCKET sd){
- struct hostent *host;
- host = gethostbyname(url_request.c_str()); //url_request is a global variable, the client sends it in
- SOCKADDR_IN site; //Socket address information
- site.sin_family = AF_INET; // address family Internet
- site.sin_port = htons (80); //Port to connect on
- site.sin_addr.s_addr = *((unsigned long*)host->h_addr);
- SOCKET s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
- if (s == INVALID_SOCKET){
- return; //Couldn't create the socket
- }
- //Try connecting...
- if (connect(s, (SOCKADDR *)&site, sizeof(site)) == SOCKET_ERROR){
- cout << "Error?" << endl; //does not come here
- return; //Couldn't connect
- }
- else{
- stringstream ss;
- ss << "GET / HTTP/1.1\r\n"
- << "Host:" + url_request + "\r\n"
- << "Connection:close\r\n" //otherwise, connection will stay alive, never stop recv'ing data
- << "\r\n";
- string request = ss.str();
- complete_send(s, request.c_str());
- //send(s, request.c_str(), request.length(), 0);
- }
- char buffer[1024];
- string buf_str;
- while(recv(s, buffer, strlen(buffer), 0) > 0){
- cout << "here" << endl; //does not come here
- buf_str += buffer;
- }
- cout << buf_str << endl; //reaches here (prints nothing)
- if(buf_str.length() <= 0){
- MESSAGE = "none"; //sends a response back to the client saying nothing was received
- send(sd, MESSAGE.c_str(), (int)(MESSAGE).size(), 0);
- closesocket(s);
- return;
- }
- cout << "here" << endl; //does not get here
- //int cont_len_start = buf_str.find("Content-Length") + 16;
- //int cont_len_end = buf_str.find(" ", cont_len_start + 1);
- //int len = cont_len_end - cont_len_start;
- //int content_length = atoi(buf_str.substr(cont_len_start, len).c_str());
- size_t strip = buf_str.find("\r\n\r\n") + 4;
- buf_str = buf_str.substr(strip);
- char buf_str_char[buf_str.length()];
- memcpy(buf_str_char, buf_str.c_str(), buf_str.size());
- complete_send(sd, buf_str_char);
- closesocket(s);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement