3  * Purpose:     wxSocket: server demo 
   4  * Author:      LAVAUX Guilhem 
   7  * Copyright: (C) 1997, LAVAUX Guilhem 
  11 #pragma implementation 
  15 // For compilers that support precompilation, includes "wx.h". 
  16 #include "wx/wxprec.h" 
  26 #include "wx/socket.h" 
  28 #if defined(__WXMOTIF__) || defined(__WXGTK__) 
  29 #include "mondrian.xpm" 
  32 // Define a new application type 
  33 class MyApp
: public wxApp
 
  40 // Define a new frame type 
  41 class MyFrame
: public wxFrame
 
  48   MyFrame(wxFrame 
*frame
); 
  50   void Menu_Exit(wxCommandEvent
& evt
); 
  51   void ExecTest1(wxSocketBase 
*sock_o
); 
  52   void UpdateStatus(int incr
); 
  55 #define SKDEMO_QUIT 101 
  57 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
) 
  58   EVT_MENU(SKDEMO_QUIT
, MyFrame::Menu_Exit
) 
  61 class MySock
: public wxSocketBase 
{ 
  65   void OldOnNotify(wxRequestEvent flags
); 
  68 class MyServer
: public wxSocketServer 
{ 
  72   MyServer(wxSockAddress
& addr
) : wxSocketServer(addr
) { } 
  73   void OldOnNotify(wxRequestEvent flags
); 
  78 // `Main program' equivalent, creating windows and returning main app frame 
  79 bool MyApp::OnInit(void) 
  81   // Create the main frame window 
  82   MyFrame 
*frame 
= new MyFrame(NULL
); 
  85   frame
->SetIcon(wxICON(mondrian
)); 
  88   wxMenu 
*file_menu 
= new wxMenu
; 
  90   file_menu
->Append(SKDEMO_QUIT
, "E&xit"); 
  91   wxMenuBar 
*menu_bar 
= new wxMenuBar
; 
  92   menu_bar
->Append(file_menu
, "File"); 
  93   frame
->SetMenuBar(menu_bar
); 
  95   // Make a panel with a message 
  96   (void)new wxPanel(frame
, 0, 0, 300, 100); 
 101   // Return the main frame window 
 105 void MySock::OldOnNotify(wxRequestEvent flags
) 
 107   extern wxList WXDLLEXPORT wxPendingDelete
; 
 113     ReadMsg((char *)&c
, 1); 
 115       frame
->ExecTest1(this); 
 119     frame
->UpdateStatus(-1); 
 120     wxPendingDelete
.Append(this); 
 125 void MyServer::OldOnNotify(wxRequestEvent 
WXUNUSED(flags
)) 
 127   MySock 
*sock2 
= new MySock(); 
 129   if (!AcceptWith(*sock2
)) 
 132   m_handler
->Register(sock2
); 
 134   sock2
->SetFlags(NONE
); 
 135   sock2
->frame 
= frame
; 
 136   sock2
->SetNotify(REQ_READ 
| REQ_LOST
); 
 138   frame
->UpdateStatus(1); 
 141 // My frame Constructor 
 142 MyFrame::MyFrame(wxFrame 
*frame
): 
 143   wxFrame(frame
, -1, "wxSocket sample (server)", wxDefaultPosition
, 
 150   wxSocketHandler::Master(); 
 152   sock 
= new MyServer(addr
); 
 153   wxSocketHandler::Master().Register(sock
); 
 155   sock
->SetNotify(wxSocketBase::REQ_ACCEPT
); 
 168 // Intercept menu commands 
 169 void MyFrame::Menu_Exit(wxCommandEvent
& WXUNUSED(event
)) 
 174 void MyFrame::ExecTest1(wxSocketBase 
*sock_o
) 
 176   char *buf 
= new char[50]; 
 179   l 
= sock_o
->Read(buf
, 50).LastCount(); 
 180   sock_o
->Write(buf
, l
); 
 183 void MyFrame::UpdateStatus(int incr
) 
 187   sprintf(s
, "%d clients connected", nb_clients
);