Hi.
I wrote a network program that sends a HTTP GET command and receives the page source code but I have a problem. When I use gethostbyname function, a runtime error occurs. What's the problem?
Here's a piece of code:
1 2 3 4 5 6 7 8 9 10
char hostname[100];
HOSTENT *pHostEnt;
sockaddr_in svraddr;
int **ppaddr;
cin >> hostname ;
pHostEnt = gethostbyname(hostname);
ppaddr = (int**)pHostEnt->h_addr_list;
svraddr.sin_addr.s_addr = **ppaddr; // <- This line makes a runtime error
If Windows - then you have to initialise the WINSOCK system.
Something like this before you start using the networking functions.
(Error checking not shown)
1 2 3 4 5 6 7 8 9 10 11 12
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
// Now use the networking functions
//Once done with networking stuff - end the use of the winsock dll
WSACleanup();