]> git.mxchange.org Git - simgear.git/blob - simgear/io/tcp_client.cxx
Fixes to allow MingW compilation.
[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 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
15 using std::cout;
16 #endif
17
18 class TcpClient
19 {
20 public:
21     TcpClient( const char* host, const char* port );
22     ~TcpClient();
23
24     bool open();
25     bool process();
26     bool close();
27
28 private:
29     SGIOChannel* channel;
30 };
31
32 TcpClient::TcpClient( const char* host, const char* port )
33 {
34     channel = new SGSocket( host, port, "tcp" );
35 }
36
37 TcpClient::~TcpClient()
38 {
39     delete channel;
40 }
41
42 bool
43 TcpClient::open()
44 {
45     return channel->open( SG_IO_OUT );
46 }
47
48 bool
49 TcpClient::process()
50 {
51     char wbuf[1024];
52
53     sprintf( wbuf, "hello world\n" );
54     int length = channel->writestring( wbuf );
55     cout << "writestring returned " << length << "\n";
56
57     return true;
58 }
59
60 bool
61 TcpClient::close()
62 {
63     return channel->close();
64 }
65
66 int
67 main()
68 {
69     sglog().setLogLevels( SG_ALL, SG_INFO );
70     TcpClient client( "localhost", "5500" );
71     if (!client.open())
72     {
73         cout << "client open failed\n";
74         return 0;
75     }
76
77     for (int i = 0; i < 3; ++i)
78     {
79         client.process();
80 #ifdef _WIN32
81         Sleep(1000);
82 #else
83         sleep(1);
84 #endif
85     }
86
87     //client.close();
88     return 0;
89 }