]>
git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/server.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Server for wxSocket demo
4 // Author: Guillermo Rodriguez Garcia <guille@iies.es>
8 // Copyright: (c) 1999 Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ==========================================================================
14 // ==========================================================================
16 // --------------------------------------------------------------------------
18 // --------------------------------------------------------------------------
21 # pragma implementation "server.cpp"
22 # pragma interface "server.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers
35 # include "wx/socket.h"
38 // --------------------------------------------------------------------------
40 // --------------------------------------------------------------------------
42 // the application icon
43 #if defined(__WXGTK__) || defined(__WXMOTIF__)
44 # include "mondrian.xpm"
47 // --------------------------------------------------------------------------
49 // --------------------------------------------------------------------------
51 // Define a new application type
52 class MyApp
: public wxApp
55 virtual bool OnInit();
58 // Define a new frame type: this is going to be our main frame
59 class MyFrame
: public wxFrame
65 // event handlers (these functions should _not_ be virtual)
66 void OnQuit(wxCommandEvent
& event
);
67 void OnAbout(wxCommandEvent
& event
);
68 void OnServerEvent(wxSocketEvent
& event
);
69 void OnSocketEvent(wxSocketEvent
& event
);
71 void Test1(wxSocketBase
*sock
);
72 void Test2(wxSocketBase
*sock
);
73 void Test3(wxSocketBase
*sock
);
75 // convenience functions
76 void UpdateStatusBar();
79 wxSocketServer
*m_server
;
87 // any class wishing to process wxWindows events must use this macro
91 // --------------------------------------------------------------------------
93 // --------------------------------------------------------------------------
95 // IDs for the controls and the menu commands
107 // --------------------------------------------------------------------------
108 // event tables and other macros for wxWindows
109 // --------------------------------------------------------------------------
111 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
112 EVT_MENU(SERVER_QUIT
, MyFrame::OnQuit
)
113 EVT_MENU(SERVER_ABOUT
, MyFrame::OnAbout
)
114 EVT_SOCKET(SERVER_ID
, MyFrame::OnServerEvent
)
115 EVT_SOCKET(SOCKET_ID
, MyFrame::OnSocketEvent
)
121 // To append sockets for delayed deletion
122 extern wxList wxPendingDelete
;
125 // ==========================================================================
127 // ==========================================================================
129 // --------------------------------------------------------------------------
130 // the application class
131 // --------------------------------------------------------------------------
135 // Create the main application window
136 MyFrame
*frame
= new MyFrame();
138 // Show it and tell the application that it's our main window
146 // --------------------------------------------------------------------------
148 // --------------------------------------------------------------------------
151 MyFrame::MyFrame() : wxFrame((wxFrame
*)NULL
, -1,
152 _T("wxSocket demo: Server"),
153 wxDefaultPosition
, wxSize(300, 200))
155 // Give the frame an icon
156 SetIcon(wxICON(mondrian
));
159 m_menuFile
= new wxMenu();
160 m_menuFile
->Append(SERVER_ABOUT
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
161 m_menuFile
->AppendSeparator();
162 m_menuFile
->Append(SERVER_QUIT
, _T("E&xit\tAlt-X"), _T("Quit server"));
164 // Append menus to the menubar
165 m_menuBar
= new wxMenuBar();
166 m_menuBar
->Append(m_menuFile
, _T("&File"));
167 SetMenuBar(m_menuBar
);
172 // Make a panel with a textctrl in it
173 m_panel
= new wxPanel(this, -1, wxPoint(0, 0), GetClientSize());
174 m_text
= new wxTextCtrl(m_panel
, -1,
175 _T("Welcome to wxSocket demo: Server\n"),
176 wxPoint(0, 0), m_panel
->GetClientSize(),
177 wxTE_MULTILINE
| wxTE_READONLY
);
183 m_server
= new wxSocketServer(addr
);
184 m_server
->SetEventHandler(*this, SERVER_ID
);
185 m_server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
186 m_server
->Notify(TRUE
);
188 // We use Ok() here to see if the server is really listening
190 m_text
->AppendText(_T("Server listening.\n\n"));
192 m_text
->AppendText(_T("Could not listen at the specified port !\n\n"));
206 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
208 // TRUE is to force the frame to close
212 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
214 wxMessageBox(_T("wxSocket demo: Server\n")
215 _T("(c) 1999 Guillermo Rodriguez Garcia\n"),
217 wxOK
| wxICON_INFORMATION
, this);
220 void MyFrame::Test1(wxSocketBase
*sock
)
225 m_text
->AppendText(_T("Test 1 begins\n"));
227 // Receive data from socket and send it back. We will first
228 // get a byte with the buffer size, so we can specify the
229 // exact size and use the WAITALL flag. Also, we disabled
230 // input events so we won't have unwanted reentrance. This
231 // way we can avoid the infamous BLOCK (formerly SPEED) flag.
233 sock
->SetFlags(wxSOCKET_WAITALL
);
235 sock
->Read((char *)&len
, 1);
237 buf
= (char *)malloc(len
);
238 sock
->Read(buf
, len
);
239 sock
->Write(buf
, len
);
242 m_text
->AppendText(_T("Test 1 ends\n"));
245 void MyFrame::Test2(wxSocketBase
*sock
)
247 #define MAX_MSG_SIZE 10000
250 char *buf
= (char *)malloc(MAX_MSG_SIZE
);
253 m_text
->AppendText(_T("Test 2 begins\n"));
255 // We don't need to set flags because ReadMsg and WriteMsg
256 // are not affected by them anyway.
258 len
= sock
->ReadMsg(buf
, MAX_MSG_SIZE
).LastCount();
260 s
.Printf(_T("Client says: %s\n"), buf
);
261 m_text
->AppendText(s
);
263 sock
->WriteMsg(buf
, len
);
266 m_text
->AppendText(_T("Test 2 ends\n"));
271 void MyFrame::Test3(wxSocketBase
*sock
)
273 m_text
->AppendText(_T("Test 3 begins\n"));
274 m_text
->AppendText(_T("(not implemented)\n"));
275 m_text
->AppendText(_T("Test 3 ends\n"));
278 void MyFrame::OnServerEvent(wxSocketEvent
& event
)
280 wxString s
= _T("OnServerEvent: ");
283 switch(event
.SocketEvent())
285 case wxSOCKET_CONNECTION
: s
.Append(_T("wxSOCKET_CONNECTION\n")); break;
286 default : s
.Append(_T("Unexpected event !\n")); break;
289 m_text
->AppendText(s
);
291 // Accept new connection if there is one in the pending
292 // connections queue, else exit. We use Accept(FALSE) for
293 // non-blocking accept (although if we got here, there
294 // should ALWAYS be a pending connection).
296 sock
= m_server
->Accept(FALSE
);
300 m_text
->AppendText(_T("New client connection accepted\n"));
304 m_text
->AppendText(_T("Error: couldn't accept a new connection"));
308 sock
->SetEventHandler(*this, SOCKET_ID
);
309 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
316 void MyFrame::OnSocketEvent(wxSocketEvent
& event
)
318 wxSocketBase
*sock
= event
.Socket();
319 wxString s
= _T("OnSocketEvent: ");
321 // We first print a msg
322 switch(event
.SocketEvent())
324 case wxSOCKET_INPUT
: s
.Append(_T("wxSOCKET_INPUT\n")); break;
325 case wxSOCKET_LOST
: s
.Append(_T("wxSOCKET_LOST\n")); break;
326 default: s
.Append(_T("unexpected event !\n"));
329 m_text
->AppendText(s
);
331 // Now we process the event
332 switch(event
.SocketEvent())
336 // We disable input events, so that the test doesn't trigger
337 // wxSocketEvent again.
338 sock
->SetNotify(wxSOCKET_LOST_FLAG
);
340 // Which test are we going to run?
342 sock
->Read((char *)&c
,1);
346 case 0xBE: Test1(sock
); break;
347 case 0xCE: Test2(sock
); break;
348 case 0xDE: Test3(sock
); break;
349 default: s
.Append(_T("Unknown test id received from client\n"));
352 // Enable input events again.
353 sock
->SetNotify(wxSOCKET_LOST_FLAG
| wxSOCKET_INPUT_FLAG
);
360 // We cannot delete the socket right now because we can
361 // be in the middle of a test or something. So we append
362 // it to the list of objects to be deleted.
363 m_text
->AppendText(_T("Deleting socket.\n"));
364 wxPendingDelete
.Append(sock
);
373 // convenience functions
375 void MyFrame::UpdateStatusBar()
378 s
.Printf(_T("%d clients connected"), m_numClients
);