00001 #include <simgear/compiler.h>
00002 #include <iostream>
00003 
00004 #ifdef _WIN32
00005 #include <windows.h>
00006 #else
00007 #include <unistd.h>
00008 #endif
00009 
00010 #include <iostream>
00011 
00012 #include <simgear/debug/logstream.hxx>
00013 
00014 #include "sg_socket.hxx"
00015 
00016 
00017 class TcpClient
00018 {
00019 public:
00020     TcpClient( const char* host, const char* port );
00021     ~TcpClient();
00022 
00023     bool open();
00024     bool process();
00025     bool close();
00026 
00027 private:
00028     SGIOChannel* channel;
00029 };
00030 
00031 TcpClient::TcpClient( const char* host, const char* port )
00032 {
00033     channel = new SGSocket( host, port, "tcp" );
00034 }
00035 
00036 TcpClient::~TcpClient()
00037 {
00038     delete channel;
00039 }
00040 
00041 bool
00042 TcpClient::open()
00043 {
00044     return channel->open( SG_IO_OUT );
00045 }
00046 
00047 bool
00048 TcpClient::process()
00049 {
00050     char wbuf[1024];
00051 
00052     sprintf( wbuf, "hello world\n" );
00053     int length = channel->writestring( wbuf );
00054     std::cout << "writestring returned " << length << "\n";
00055 
00056     return true;
00057 }
00058 
00059 bool
00060 TcpClient::close()
00061 {
00062     return channel->close();
00063 }
00064 
00065 int
00066 main()
00067 {
00068     sglog().setLogLevels( SG_ALL, SG_INFO );
00069     TcpClient client( "localhost", "5500" );
00070     if (!client.open())
00071     {
00072         std::cout << "client open failed\n";
00073         return 0;
00074     }
00075 
00076     for (int i = 0; i < 3; ++i)
00077     {
00078         client.process();
00079 #ifdef _WIN32
00080         Sleep(1000);
00081 #else
00082         sleep(1);
00083 #endif
00084     }
00085 
00086     
00087     return 0;
00088 }