Skip to main content
gamedev.net
🔒 Locked

HTTP connect() timeout

Started by AndOrXor Dec 20, 2005 at 1:23 AM 2 replies 750+ views
Original Post
AndOrXor
AndOrXor
I am writing a class to make it easier to read in a web page. I can currently resolve the host name, but the connection always times out. I really don't understand this because I had it working fine before I decided to make it into a class (it also used a hard ip instead of gethostbyname()). Here's a snippet:
string AOXHTTP::getPage(string host, string path) {
	string		request;
	string		ret;
	WSADATA		wsaData;
	SOCKET		m_socket;
	sockaddr_in	to;
	hostent*	hp;
	
	to.sin_family	= AF_INET;
	to.sin_port	= 80;

	if(WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR)
		return "";
	if((m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
		WSACleanup();
		return "";
	}
	if(!(hp = gethostbyname(host.c_str()))) {
#ifdef debug
		add_log("AOXHTTP Error: gethostbyname() failed with error code %i\n", host.c_str(), WSAGetLastError());
#endif
		return "";
	}
	to.sin_addr = *(in_addr*)hp->h_addr_list[0];
#ifdef debug
	add_log("Address for %s is %s.\n", host.c_str(), inet_ntoa(*(in_addr*)hp->h_addr_list[0]));
#endif
	if(connect(m_socket, (SOCKADDR*)&to, sizeof(to)) == SOCKET_ERROR) {
#ifdef debug
		add_log("AOXHTTP Error: connect() failed with error code %i\n", WSAGetLastError());
#endif
		WSACleanup();
		return "";
	}
And when I call getPage("www.google.com", "/") I always get the following output:
Quote:
Address for www.google.com is 72.14.203.99. AOXHTTP Error: connect() failed with error code 10060
Of course it's not just for google, but for any site that I try. I've also checked to make sure that gethostbyname() is returning the correct ip. (10060 is the timeout error.)
nprz
nprz
Did some testing since your code is almost identical to some that I used (Beej's).

Try changing

to.sin_port = 80;

to

to.sin_port = htons(80);
AndOrXor
AndOrXor
Wow I feel like an idiot for missing that.

Well I have it returning now.. but if you've done this before maybe you can help me with another problem.. storing the entire web page into one variable (or each line in a vector). Currently I am just using a huge buffer (recv(m_socket, recvBuf, 16384, 0)), but not only is that wildly ineffecient but could easily not be big enough. Any suggestions?
nprz
nprz
Don't worry because you can read from the stream multiple times.
So you can read 512 (or any amount) of bytes at a time, then put them into a stringstream or something. Read until you get 0 bytes in a read, which means that the stream is finished and then you can take everything from the stringstream and put it into a string.

My code for doing it was something like this.
	std::stringstream ss;	string webpage;	int leng;	char BUFFER[512];	while((leng = recv(sockfd, BUFFER, sizeof(BUFFER), 0)) > 0)	{		for (int i = 0; i < leng; ++i) // I put them in 1 byte at a time because of binary data, a 0 might be part of the data			ss << BUFFER;	}	webpage = ss.str();

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.