Original Post
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: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.)
Address for www.google.com is 72.14.203.99. AOXHTTP Error: connect() failed with error code 10060