]> git.mxchange.org Git - simgear.git/blob - simgear/io/tcp_server.cxx
Mac OS X fixes from Markus Morawitz
[simgear.git] / simgear / io / tcp_server.cxx
1 #include <simgear/compiler.h>
2 #include <simgear/debug/logstream.hxx>
3 #include STL_STRING
4 #include STL_IOSTREAM
5
6 #include "sg_socket.hxx"
7
8 SG_USING_STD(string);
9 SG_USING_STD(cout);
10
11 class TcpServer
12 {
13 public:
14     TcpServer();
15     bool open();
16     bool process();
17     bool close();
18
19 private:
20     SGIOChannel* channel;
21 };
22
23 TcpServer::TcpServer()
24 {
25     channel = new SGSocket( "", "5500", "tcp" );
26 }
27
28 bool
29 TcpServer::open()
30 {
31     channel->open( SG_IO_BI );
32     return true;
33 }
34
35 bool
36 TcpServer::process()
37 {
38     char buf[1024];
39
40     int len;
41     while ((len = channel->readline( buf, sizeof(buf) )) > 0)
42     {
43         cout << len << ": " << buf;
44     }
45     return true;
46 }
47
48 bool
49 TcpServer::close()
50 {
51     return channel->close();
52 }
53
54
55 int
56 main()
57 {
58     sglog().setLogLevels( SG_ALL, SG_INFO );
59     TcpServer server;
60     server.open();
61     SG_LOG( SG_IO, SG_INFO, "Created TCP server" );
62
63     while (1)
64     {
65         server.process();
66     }
67
68     server.close();
69     return 0;
70 }