]>
git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/server.cpp
3 * Purpose: wxSocket: server demo
4 * Author: LAVAUX Guilhem (from minimal.cc)
7 * Copyright: (c) 1993, AIAI, University of Edinburgh
8 * (C) 1997, LAVAUX Guilhem
11 #pragma implementation
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
25 #include "wx/socket.h"
27 // Define a new application type
28 class MyApp
: public wxApp
35 // Define a new frame type
36 class MyFrame
: public wxFrame
43 MyFrame(wxFrame
*frame
);
45 void Menu_Exit(wxCommandEvent
& evt
);
46 void ExecTest1(wxSocketBase
*sock_o
);
47 void UpdateStatus(int incr
);
50 #define SKDEMO_QUIT 101
52 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
53 EVT_MENU(SKDEMO_QUIT
, MyFrame::Menu_Exit
)
56 class MySock
: public wxSocketBase
{
60 void OldOnNotify(wxRequestEvent flags
);
63 class MyServer
: public wxSocketServer
{
67 MyServer(wxSockAddress
& addr
) : wxSocketServer(addr
) { }
68 void OldOnNotify(wxRequestEvent flags
);
73 // `Main program' equivalent, creating windows and returning main app frame
74 bool MyApp::OnInit(void)
76 // Create the main frame window
77 MyFrame
*frame
= new MyFrame(NULL
);
81 frame
->SetIcon(new wxIcon("mondrian"));
84 frame
->SetIcon(new wxIcon("aiai.xbm"));
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 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
& 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
);