]>
git.saurik.com Git - wxWidgets.git/blob - samples/sockets/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 // --------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
32 #include "wx/socket.h"
34 // --------------------------------------------------------------------------
36 // --------------------------------------------------------------------------
38 // the application icon
39 #include "mondrian.xpm"
41 // --------------------------------------------------------------------------
43 // --------------------------------------------------------------------------
45 // Define a new application type
46 class MyApp
: public wxApp
49 virtual bool OnInit();
52 // Define a new frame type: this is going to be our main frame
53 class MyFrame
: public wxFrame
59 // event handlers (these functions should _not_ be virtual)
60 void OnQuit(wxCommandEvent
& event
);
61 void OnAbout(wxCommandEvent
& event
);
62 void OnServerEvent(wxSocketEvent
& event
);
63 void OnSocketEvent(wxSocketEvent
& event
);
65 void Test1(wxSocketBase
*sock
);
66 void Test2(wxSocketBase
*sock
);
67 void Test3(wxSocketBase
*sock
);
69 // convenience functions
70 void UpdateStatusBar();
73 wxSocketServer
*m_server
;
80 // any class wishing to process wxWidgets events must use this macro
84 // --------------------------------------------------------------------------
86 // --------------------------------------------------------------------------
88 // IDs for the controls and the menu commands
92 SERVER_QUIT
= wxID_EXIT
,
93 SERVER_ABOUT
= wxID_ABOUT
,
100 // --------------------------------------------------------------------------
101 // event tables and other macros for wxWidgets
102 // --------------------------------------------------------------------------
104 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
105 EVT_MENU(SERVER_QUIT
, MyFrame::OnQuit
)
106 EVT_MENU(SERVER_ABOUT
, MyFrame::OnAbout
)
107 EVT_SOCKET(SERVER_ID
, MyFrame::OnServerEvent
)
108 EVT_SOCKET(SOCKET_ID
, MyFrame::OnSocketEvent
)
114 // ==========================================================================
116 // ==========================================================================
118 // --------------------------------------------------------------------------
119 // the application class
120 // --------------------------------------------------------------------------
124 if ( !wxApp::OnInit() )
127 // Create the main application window
128 MyFrame
*frame
= new MyFrame();
130 // Show it and tell the application that it's our main window
138 // --------------------------------------------------------------------------
140 // --------------------------------------------------------------------------
144 MyFrame::MyFrame() : wxFrame((wxFrame
*)NULL
, wxID_ANY
,
145 _("wxSocket demo: Server"),
146 wxDefaultPosition
, wxSize(300, 200))
148 // Give the frame an icon
149 SetIcon(wxICON(mondrian
));
152 m_menuFile
= new wxMenu();
153 m_menuFile
->Append(SERVER_ABOUT
, _("&About...\tCtrl-A"), _("Show about dialog"));
154 m_menuFile
->AppendSeparator();
155 m_menuFile
->Append(SERVER_QUIT
, _("E&xit\tAlt-X"), _("Quit server"));
157 // Append menus to the menubar
158 m_menuBar
= new wxMenuBar();
159 m_menuBar
->Append(m_menuFile
, _("&File"));
160 SetMenuBar(m_menuBar
);
165 #endif // wxUSE_STATUSBAR
167 // Make a textctrl for logging
168 m_text
= new wxTextCtrl(this, wxID_ANY
,
169 _("Welcome to wxSocket demo: Server\n"),
170 wxDefaultPosition
, wxDefaultSize
,
171 wxTE_MULTILINE
| wxTE_READONLY
);
173 // Create the address - defaults to localhost:0 initially
182 m_server
= new wxSocketServer(addr
);
184 // We use Ok() here to see if the server is really listening
185 if (! m_server
->Ok())
187 m_text
->AppendText(_("Could not listen at the specified port !\n\n"));
192 m_text
->AppendText(_("Server listening.\n\n"));
195 // Setup the event handler and subscribe to connection events
196 m_server
->SetEventHandler(*this, SERVER_ID
);
197 m_server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
198 m_server
->Notify(true);
207 // No delayed deletion here, as the frame is dying anyway
213 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
215 // true is to force the frame to close
219 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
221 wxMessageBox(_("wxSocket demo: Server\n(c) 1999 Guillermo Rodriguez Garcia\n"),
223 wxOK
| wxICON_INFORMATION
, this);
226 void MyFrame::Test1(wxSocketBase
*sock
)
231 m_text
->AppendText(_("Test 1 begins\n"));
233 // Receive data from socket and send it back. We will first
234 // get a byte with the buffer size, so we can specify the
235 // exact size and use the wxSOCKET_WAITALL flag. Also, we
236 // disabled input events so we won't have unwanted reentrance.
237 // This way we can avoid the infamous wxSOCKET_BLOCK flag.
239 sock
->SetFlags(wxSOCKET_WAITALL
);
246 sock
->Read(buf
, len
);
247 m_text
->AppendText(_("Got the data, sending it back\n"));
250 sock
->Write(buf
, len
);
253 m_text
->AppendText(_("Test 1 ends\n\n"));
256 void MyFrame::Test2(wxSocketBase
*sock
)
258 #define MAX_MSG_SIZE 10000
261 wxChar
*buf
= new wxChar
[MAX_MSG_SIZE
];
264 m_text
->AppendText(_("Test 2 begins\n"));
266 // We don't need to set flags because ReadMsg and WriteMsg
267 // are not affected by them anyway.
270 len
= sock
->ReadMsg(buf
, MAX_MSG_SIZE
* sizeof(wxChar
)).LastCount();
271 s
.Printf(_("Client says: %s\n"), buf
);
272 m_text
->AppendText(s
);
273 m_text
->AppendText(_("Sending the data back\n"));
276 sock
->WriteMsg(buf
, len
);
279 m_text
->AppendText(_("Test 2 ends\n\n"));
284 void MyFrame::Test3(wxSocketBase
*sock
)
289 m_text
->AppendText(_("Test 3 begins\n"));
291 // This test is similar to the first one, but the len is
292 // expressed in kbytes - this tests large data transfers.
294 sock
->SetFlags(wxSOCKET_WAITALL
);
298 buf
= new char[len
* 1024];
301 sock
->Read(buf
, len
* 1024);
302 m_text
->AppendText(_("Got the data, sending it back\n"));
305 sock
->Write(buf
, len
* 1024);
308 m_text
->AppendText(_("Test 3 ends\n\n"));
311 void MyFrame::OnServerEvent(wxSocketEvent
& event
)
313 wxString s
= _("OnServerEvent: ");
316 switch(event
.GetSocketEvent())
318 case wxSOCKET_CONNECTION
: s
.Append(_("wxSOCKET_CONNECTION\n")); break;
319 default : s
.Append(_("Unexpected event !\n")); break;
322 m_text
->AppendText(s
);
324 // Accept new connection if there is one in the pending
325 // connections queue, else exit. We use Accept(false) for
326 // non-blocking accept (although if we got here, there
327 // should ALWAYS be a pending connection).
329 sock
= m_server
->Accept(false);
333 m_text
->AppendText(_("New client connection accepted\n\n"));
337 m_text
->AppendText(_("Error: couldn't accept a new connection\n\n"));
341 sock
->SetEventHandler(*this, SOCKET_ID
);
342 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
349 void MyFrame::OnSocketEvent(wxSocketEvent
& event
)
351 wxString s
= _("OnSocketEvent: ");
352 wxSocketBase
*sock
= event
.GetSocket();
354 // First, print a message
355 switch(event
.GetSocketEvent())
357 case wxSOCKET_INPUT
: s
.Append(_("wxSOCKET_INPUT\n")); break;
358 case wxSOCKET_LOST
: s
.Append(_("wxSOCKET_LOST\n")); break;
359 default : s
.Append(_("Unexpected event !\n")); break;
362 m_text
->AppendText(s
);
364 // Now we process the event
365 switch(event
.GetSocketEvent())
369 // We disable input events, so that the test doesn't trigger
370 // wxSocketEvent again.
371 sock
->SetNotify(wxSOCKET_LOST_FLAG
);
373 // Which test are we going to run?
379 case 0xBE: Test1(sock
); break;
380 case 0xCE: Test2(sock
); break;
381 case 0xDE: Test3(sock
); break;
383 m_text
->AppendText(_("Unknown test id received from client\n\n"));
386 // Enable input events again.
387 sock
->SetNotify(wxSOCKET_LOST_FLAG
| wxSOCKET_INPUT_FLAG
);
394 // Destroy() should be used instead of delete wherever possible,
395 // due to the fact that wxSocket uses 'delayed events' (see the
396 // documentation for wxPostEvent) and we don't want an event to
397 // arrive to the event handler (the frame, here) after the socket
398 // has been deleted. Also, we might be doing some other thing with
399 // the socket at the same time; for example, we might be in the
400 // middle of a test or something. Destroy() takes care of all
403 m_text
->AppendText(_("Deleting socket.\n\n"));
413 // convenience functions
415 void MyFrame::UpdateStatusBar()
419 s
.Printf(_("%d clients connected"), m_numClients
);
421 #endif // wxUSE_STATUSBAR