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