00001 #include <simgear/compiler.h>
00002 #include <simgear/debug/logstream.hxx>
00003 #include <string>
00004 #include <iostream>
00005
00006 #include "sg_socket.hxx"
00007
00008 using std::string;
00009 using std::cout;
00010
00011 class TcpServer
00012 {
00013 public:
00014 TcpServer();
00015 ~TcpServer();
00016 bool open();
00017 bool process();
00018 bool close();
00019
00020 private:
00021 SGIOChannel* channel;
00022 };
00023
00024 TcpServer::TcpServer()
00025 {
00026 channel = new SGSocket( "", "5500", "tcp" );
00027 }
00028
00029 TcpServer::~TcpServer()
00030 {
00031 delete channel;
00032 }
00033
00034 bool
00035 TcpServer::open()
00036 {
00037 channel->open( SG_IO_BI );
00038 return true;
00039 }
00040
00041 bool
00042 TcpServer::process()
00043 {
00044 char buf[1024];
00045
00046 int len;
00047 while ((len = channel->readline( buf, sizeof(buf) )) > 0)
00048 {
00049 cout << len << ": " << buf;
00050 }
00051 return true;
00052 }
00053
00054 bool
00055 TcpServer::close()
00056 {
00057 return channel->close();
00058 }
00059
00060
00061 int
00062 main()
00063 {
00064 sglog().setLogLevels( SG_ALL, SG_INFO );
00065 TcpServer server;
00066 server.open();
00067 SG_LOG( SG_IO, SG_INFO, "Created TCP server" );
00068
00069 while (1)
00070 {
00071 server.process();
00072 }
00073
00074 server.close();
00075 return 0;
00076 }