]> git.mxchange.org Git - simgear.git/blob - simgear/io/tcp_client.cxx
Bernie Bright:
[simgear.git] / simgear / io / tcp_client.cxx
1 #include <iostream>
2 #include <unistd.h>
3
4 #include <simgear/debug/logstream.hxx>
5
6 #include "sg_socket.hxx"
7
8 using std::cout;
9
10 class TcpClient
11 {
12 public:
13     TcpClient( const char* host, const char* port );
14     ~TcpClient();
15
16     bool open();
17     bool process();
18     bool close();
19
20 private:
21     SGIOChannel* channel;
22 };
23
24 TcpClient::TcpClient( const char* host, const char* port )
25 {
26     channel = new SGSocket( host, port, "tcp" );
27 }
28
29 TcpClient::~TcpClient()
30 {
31     delete channel;
32 }
33
34 bool
35 TcpClient::open()
36 {
37     return channel->open( SG_IO_OUT );
38 }
39
40 bool
41 TcpClient::process()
42 {
43     char wbuf[1024];
44
45     sprintf( wbuf, "hello world\n" );
46     int length = channel->writestring( wbuf );
47     cout << "writestring returned " << length << "\n";
48
49     return true;
50 }
51
52 bool
53 TcpClient::close()
54 {
55     return channel->close();
56 }
57
58 int
59 main()
60 {
61     sglog().setLogLevels( SG_ALL, SG_INFO );
62     TcpClient client( "localhost", "5500" );
63     if (!client.open())
64     {
65         cout << "client open failed\n";
66         return 0;
67     }
68
69     for (int i = 0; i < 3; ++i)
70     {
71         client.process();
72         sleep(1);
73     }
74
75     //client.close();
76     return 0;
77 }