Winsock Loading Yahoo faster than Google

Started by
0 comments, last by TheAdmiral 16 years, 11 months ago
I am using the socket class from http://www.adp-gmbh.ch/win/misc/sockets.html I have simplified to what I only required at below, this program is basically to download the html source of any website domain. I set user-agent to Firefox latest specification to get more reliable result. But when I tried to download www.yahoo.com it took just a few seconds, but when I tried www.google.com and others website it takes like more than one minute to load the html source. it is weird, I think the recv() . And also the html file downloaded is slightly lesser than what I actually viewed from my Firefox browser for www.yahoo.com. Any ideas why ? Thanks...so much to even read at this post. Main function

#include "Socket.h"

#include <iostream>

using namespace std;

int main() {
  try {
    SocketClient s("www.yahoo.com", 80);

	string header = "GET / HTTP/1.1\nHost: www.yahoo.com\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3\n\n";
    s.SendBytes(header);
	cout << header;
	cout << s.receiveAll() << endl;
	cout << "Finished";
  }
  catch (const char* s) {
    cerr << s << endl;
  }
  catch (std::string s) {
    cerr << s << endl;
  } 
  catch (...) {
    cerr << "unhandled exception\n";
  }

  return 0;
}
socket class

#include "Socket.h"
#include <iostream>

using namespace std;

int Socket::nofSockets_= 0;

void Socket::Start() {
  if (!nofSockets_) {
    WSADATA info;
    if (WSAStartup(MAKEWORD(2,0), &info)) {
      throw "Could not start WSA";
    }
  }
  ++nofSockets_;
}

void Socket::End() {
  WSACleanup();
}

Socket::Socket() : s_(0) {
  Start();
  // UDP: use SOCK_DGRAM instead of SOCK_STREAM
  s_ = socket(AF_INET,SOCK_STREAM,0);

  if (s_ == INVALID_SOCKET) {
    throw "INVALID_SOCKET";
  }

  refCounter_ = new int(1);
}

Socket::~Socket() {
  if (! --(*refCounter_)) {
    Close();
    delete refCounter_;
  }

  --nofSockets_;
  if (!nofSockets_) End();
}

void Socket::Close() {
  closesocket(s_);
}

std::string Socket::receiveAll() {
  std::string ret;
  char r[1000];
  while (1) {
    switch(recv(s_, r, 1000, 0)) {
		case SOCKET_ERROR:
		case 0: 
			return ret;
	}
    ret += r;
  }
}

void Socket::SendBytes(const std::string& s) {
  send(s_,s.c_str(),s.length(),0);
}

SocketClient::SocketClient(const std::string& host, int port) : Socket() {
  std::string error;

  hostent *he;
  if ((he = gethostbyname(host.c_str())) == 0) {
    error = strerror(errno);
    throw error;
  }

  sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr = *((in_addr *)he->h_addr);
  memset(&(addr.sin_zero), 0, 8); 

  if (::connect(s_, (sockaddr *) &addr, sizeof(sockaddr))) {
    error = strerror(WSAGetLastError());
    throw error;
  }
}

Advertisement
Maybe somebody will know what may cause this behaviour, but in the meanwhile I recommend you profile your program and see where the time is being spent.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

This topic is closed to new replies.

Advertisement