1 |
#include <sockinet.h> |
2 |
#include <ctype.h> |
3 |
#include <string.h> |
4 |
|
5 |
static void get_text (iosockstream& s); |
6 |
|
7 |
int main(int ac, char** av) |
8 |
{ |
9 |
if (ac != 2) { |
10 |
cerr << "USAGE: " << av[0] << " hostname\n"; |
11 |
return 1; |
12 |
} |
13 |
|
14 |
iosockinet sio(sockbuf::sock_stream); |
15 |
sio->connect ("nic.ddn.mil", "hostnames", "tcp"); |
16 |
|
17 |
sio << "HNAME " << av[1] << "\r\n" << flush; |
18 |
get_text (sio); |
19 |
|
20 |
return 0; |
21 |
} |
22 |
|
23 |
void get_text(iosockstream& s) |
24 |
{ |
25 |
char buf[1024]; |
26 |
int tmo = s->recvtimeout(30); // wait for 30 sec before timing out |
27 |
|
28 |
if (s.getline (buf, 1023)) |
29 |
cout << buf << endl; |
30 |
|
31 |
if (strncmp (buf, "BEGIN", 5) == 0) |
32 |
while (s.getline (buf, 1023)) { |
33 |
cout << buf << endl; |
34 |
if (strncmp (buf, "END", 3) == 0) break; |
35 |
} |
36 |
else |
37 |
while (s.getline (buf, 1023)) { |
38 |
cout << buf << endl; |
39 |
if (!isspace (buf [0])) break; |
40 |
} |
41 |
if ( !s->is_eof() ) s.clear(); |
42 |
s->recvtimeout(tmo); //reset timeout to the previous value |
43 |
} |