GetHostByName() Runtime Error

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 
What operating system??

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();

Thanks.
But it does not get anything :-(
What's the problem?
Here's another piece of code:
1
2
3
4
   SOCKET skt;
   if(connect(skt, (struct sockaddr*)&svraddr, sizeof(svraddr)) == 0)
       cout << "Connected to the server successfully\n" ;
   send(skt, "GET HTTP/1.0\r\n\r\n", 16, 0); // <- Is this true? 
That's because you are doing it all wrong.
I'll give you this - but I suggest you go and learn about network programming.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
 
    wVersionRequested = MAKEWORD( 2, 2 );
 
    err = WSAStartup( wVersionRequested, &wsaData );

   
    
  char hostname[100];
   HOSTENT *pHostEnt;
   sockaddr_in svraddr ={0};
   int **ppaddr;
   
   cin >> hostname ;
   
   pHostEnt = gethostbyname(hostname);
   ppaddr = (int**)pHostEnt->h_addr_list;
   svraddr.sin_addr.s_addr  = **ppaddr;
    
    
    SOCKET skt;
    skt = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    
    svraddr.sin_port = htons(80); //Remember must be in Network order (big Endian?)
    svraddr.sin_family = AF_INET;
 
   if(connect(skt, (struct sockaddr*)&svraddr, sizeof(svraddr)) == 0)
       cout << "Connected to the server successfully\n" ;
   
   int sent=0, recvd=0;
   sent = send(skt, "GET HTTP/1.0\r\n\r\n", 16, 0); // Probably not correct request - but you should get some data back 
   
   char buff[2048] = {0};
   recvd = recv(skt,buff,2048,0);

   cout << "Bytes Sent: " << sent << endl;
   cout << "Bytes Received: " << recvd << endl;
   cout << "Data Received: " << buff << endl;
Last edited on
Thanks again.
I Copied your code and tested it with borland C++ but It returns "HTTP/1.0 400 Bad Request etc".
What's the problem?
Thanks.
As you can see in the comment in the code I posted - Your HTTP request isn't right? - you'll have to go and check/read up on the HTTP protocol.
Topic archived. No new replies allowed.