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