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