Sign in to follow this  

HTTP connect() timeout

This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.)

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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[i];
}
webpage = ss.str();


Share this post


Link to post
Share on other sites

This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this