1#include "InetAddress.h"
2#include "TcpStream.h"
3#include <unistd.h>
4
5int main(int argc, const char* argv[])
6{
7  if (argc < 3)
8  {
9    printf("Usage: %s hostname message_length [scp]\n", argv[0]);
10    return 0;
11  }
12
13  const int len = atoi(argv[2]);
14
15  uint16_t port = 3007;
16  InetAddress addr;
17  if (!InetAddress::resolve(argv[1], port, &addr))
18  {
19    printf("Unable to resolve %s\n", argv[1]);
20    return 0;
21  }
22
23  printf("connecting to %s\n", addr.toIpPort().c_str());
24  TcpStreamPtr stream(TcpStream::connect(addr));
25  if (!stream)
26  {
27    perror("");
28    printf("Unable to connect %s\n", addr.toIpPort().c_str());
29    return 0;
30  }
31
32  printf("connected, sending %d bytes\n", len);
33
34  std::string message(len, 'S');
35  int nw = stream->sendAll(message.c_str(), message.size());
36  printf("sent %d bytes\n", nw);
37
38  if (argc > 3)
39  {
40    for (char cmd : std::string(argv[3]))
41    {
42      if (cmd == 's')  // shutdown
43      {
44        printf("shutdown write\n");
45        stream->shutdownWrite();
46      }
47      else if (cmd == 'p') // pause
48      {
49        printf("sleeping for 10 seconds\n");
50        ::sleep(10);
51        printf("done\n");
52      }
53      else if (cmd == 'c') // close
54      {
55        printf("close without reading response\n");
56        return 0;
57      }
58      else
59      {
60        printf("unknown command '%c'\n", cmd);
61      }
62    }
63  }
64
65  std::vector<char> receive(len);
66  int nr = stream->receiveAll(receive.data(), receive.size());
67  printf("received %d bytes\n", nr);
68  if (nr != nw)
69  {
70    printf("!!! Incomplete response !!!\n");
71  }
72}
73