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