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