ace-http-client.cpp

(plain text)
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
41
42
43
#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <ace/INET_Addr.h>
#include <ace/SOCK_Connector.h>
#include <ace/SOCK_Stream.h>
using namespace std;

int main () {
  ACE_SOCK_Stream yahoo;

  if (ACE_SOCK_Connector().connect(yahoo, ACE_INET_Addr(80, "www.yahoo.com"))
      != -1) {
    static const char request[] = "GET / HTTP/1.0\r\n\r\n";
    yahoo.send(request, sizeof(request) - 1); // "- 1": don't send '\0'
    yahoo.close_writer();

    char buf[65535]; // maximum size of an IP packet

    while (ssize_t len = yahoo.recv(buf, sizeof(buf))) { // (len == 0) is EOF
      if (len > 0) {
	cout.write(buf, len);
      }

      else if (errno == EINTR) {
	continue;
      }

      else {
	std::cerr << "fatal error: " << std::strerror(errno) << '\n';
	break;
      }
    }
  }

  else {
    cerr << "could not connect: " << strerror(errno) << endl;
    return EXIT_FAILURE;
  }

  return 0;
}