Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

guiclient/echo.cpp

Go to the documentation of this file.
00001 //this is a wxWidgets based server that just echo's any infomation recieved
00002 
00003 #include <wx/wx.h>
00004 #include <wx/socket.h>
00005 #include "../trucknet/trucknet.h"
00006 
00018 class echoApp : public wxApp
00019 {
00020         public:
00021                 virtual bool OnInit();
00022 };
00023 
00035 class echoFrame : public wxFrame
00036 {
00037         public:
00038                 echoFrame();
00039                 ~echoFrame();
00040 
00041                 void OnServer(wxSocketEvent& event);
00042                 void OnClient(wxSocketEvent& event);
00043 
00044         private:
00045                 wxSocketServer  *server;
00046                 wxTextCtrl              *text;
00047 
00048                 DECLARE_EVENT_TABLE()
00049 };
00050 
00051  
00052 enum
00053 {       SERVER_SOCKET,
00054         CLIENT_SOCKET,
00055 };
00056 
00057 BEGIN_EVENT_TABLE(echoFrame, wxFrame)
00058         EVT_SOCKET(SERVER_SOCKET,       echoFrame::OnServer)
00059         EVT_SOCKET(CLIENT_SOCKET,       echoFrame::OnClient)
00060 END_EVENT_TABLE()
00061 
00062         
00063 
00064 IMPLEMENT_APP(echoApp)
00065 
00077 bool echoApp::OnInit()
00078 {
00079         echoFrame *frame = new echoFrame();
00080         frame->Show();
00081         SetTopWindow(frame);
00082         return true;
00083 }
00084 
00096 echoFrame::echoFrame() :
00097         wxFrame((wxFrame *)NULL, wxID_ANY, wxT("Echo Server"), wxDefaultPosition,
00098                                 wxSize(300, 200))
00099 {       CreateStatusBar();
00100         text = new wxTextCtrl(this, wxID_ANY, wxT(""), \
00101                         wxDefaultPosition, wxDefaultSize, \
00102                         wxTE_MULTILINE | wxTE_READONLY);
00103         
00104         wxIPV4address addr; //defalts to localhost
00105         addr.Service(GUI_PORT.PORT);
00106         
00107         server = new wxSocketServer(addr,wxSOCKET_REUSEADDR);
00108 
00109         if (! server->Ok())
00110         {       text->AppendText(wxT("Problem creating socket...\n"));
00111                 return;
00112         }
00113         else
00114                 SetStatusText(wxT("Server listening..."));
00115 
00116         server->SetEventHandler(*this, SERVER_SOCKET);
00117         server->SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_LOST_FLAG);
00118         server->Notify(true);
00119 }
00120 
00132 echoFrame::~echoFrame()
00133 {       delete server;
00134 }
00135 
00147 void echoFrame::OnServer(wxSocketEvent& event)
00148 {       switch(event.GetSocketEvent())
00149         {       case wxSOCKET_CONNECTION:
00150                         wxSocketBase *client;
00151                         client = server->Accept(false); //false = nonblocking
00152                         if (client)
00153                                 SetStatusText(wxT("Connected."));
00154                         else
00155                                 SetStatusText(wxT("Attempted connection failed..."));
00156                         client->SetEventHandler(*this, CLIENT_SOCKET); 
00157                         client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
00158                         client->Notify(true);
00159                         client->SetFlags(wxSOCKET_NOWAIT);
00160                         break;
00161                 case wxSOCKET_LOST:
00162                         SetStatusText(wxT("Server socket lost."));
00163                         break;
00164                 default:
00165                         SetStatusText(wxT("Unknown event on server socket!"));
00166         }
00167 }
00168 
00169 
00170 
00181 void echoFrame::OnClient(wxSocketEvent& event)
00182 {       wxSocketBase *sock = event.GetSocket();
00183         int count;
00184         const int buflen = 32;//make multiple of 4 for unicode
00185         char buf[buflen + 1];
00186 
00187         switch(event.GetSocketEvent())
00188         {       case wxSOCKET_INPUT:
00189                         *text << wxT("Data from client:\n");
00190                         do
00191                         {       count = sock->Read(buf, buflen).LastCount();
00192                                 if(count > 0)
00193                                 {       *text << wxString(buf, *wxConvCurrent, count);
00194                                         sock->Write(buf, count);
00195                                 }
00196                         } while(count == buflen);
00197                         *text << wxT("\n\n");
00198                         break;
00199                 case wxSOCKET_LOST:
00200                         SetStatusText(wxT("Client connection lost."));
00201                         break;
00202                 default:
00203                         SetStatusText(wxT("Unknown event on client socket!"));
00204         }
00205 }
00206 
00207 

Generated on Fri Sep 1 14:25:44 2006 for Raptor by  doxygen 1.4.4