]>
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 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
40 # include "mondrian.xpm"
43 // --------------------------------------------------------------------------
45 // --------------------------------------------------------------------------
47 // Define a new application type
48 class MyApp
: public wxApp
51 virtual bool OnInit();
54 // Define a new frame type: this is going to be our main frame
55 class MyFrame
: public wxFrame
61 // event handlers (these functions should _not_ be virtual)
62 void OnQuit(wxCommandEvent
& event
);
63 void OnAbout(wxCommandEvent
& event
);
64 void OnServerEvent(wxSocketEvent
& event
);
65 void OnSocketEvent(wxSocketEvent
& event
);
67 void Test1(wxSocketBase
*sock
);
68 void Test2(wxSocketBase
*sock
);
69 void Test3(wxSocketBase
*sock
);
71 // convenience functions
72 void UpdateStatusBar();
75 wxSocketServer
*m_server
;
82 // any class wishing to process wxWidgets events must use this macro
86 // --------------------------------------------------------------------------
88 // --------------------------------------------------------------------------
90 // IDs for the controls and the menu commands
94 SERVER_QUIT
= wxID_EXIT
,
95 SERVER_ABOUT
= wxID_ABOUT
,
102 // --------------------------------------------------------------------------
103 // event tables and other macros for wxWidgets
104 // --------------------------------------------------------------------------
106 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
107 EVT_MENU(SERVER_QUIT
, MyFrame::OnQuit
)
108 EVT_MENU(SERVER_ABOUT
, MyFrame::OnAbout
)
109 EVT_SOCKET(SERVER_ID
, MyFrame::OnServerEvent
)
110 EVT_SOCKET(SOCKET_ID
, MyFrame::OnSocketEvent
)
116 // ==========================================================================
118 // ==========================================================================
120 // --------------------------------------------------------------------------
121 // the application class
122 // --------------------------------------------------------------------------
126 if ( !wxApp::OnInit() )
129 // Create the main application window
130 MyFrame
*frame
= new MyFrame();
132 // Show it and tell the application that it's our main window
140 // --------------------------------------------------------------------------
142 // --------------------------------------------------------------------------
146 MyFrame::MyFrame() : wxFrame((wxFrame
*)NULL
, wxID_ANY
,
147 _("wxSocket demo: Server"),
148 wxDefaultPosition
, wxSize(300, 200))
150 // Give the frame an icon
151 SetIcon(wxICON(mondrian
));
154 m_menuFile
= new wxMenu();
155 m_menuFile
->Append(SERVER_ABOUT
, _("&About...\tCtrl-A"), _("Show about dialog"));
156 m_menuFile
->AppendSeparator();
157 m_menuFile
->Append(SERVER_QUIT
, _("E&xit\tAlt-X"), _("Quit server"));
159 // Append menus to the menubar
160 m_menuBar
= new wxMenuBar();
161 m_menuBar
->Append(m_menuFile
, _("&File"));
162 SetMenuBar(m_menuBar
);
167 #endif // wxUSE_STATUSBAR
169 // Make a textctrl for logging
170 m_text
= new wxTextCtrl(this, wxID_ANY
,
171 _("Welcome to wxSocket demo: Server\n"),
172 wxDefaultPosition
, wxDefaultSize
,
173 wxTE_MULTILINE
| wxTE_READONLY
);
175 // Create the address - defaults to localhost:0 initially
184 m_server
= new wxSocketServer(addr
);
186 // We use Ok() here to see if the server is really listening
187 if (! m_server
->Ok())
189 m_text
->AppendText(_("Could not listen at the specified port !\n\n"));
194 m_text
->AppendText(_("Server listening.\n\n"));
197 // Setup the event handler and subscribe to connection events
198 m_server
->SetEventHandler(*this, SERVER_ID
);
199 m_server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
200 m_server
->Notify(true);
209 // No delayed deletion here, as the frame is dying anyway
215 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
217 // true is to force the frame to close
221 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
223 wxMessageBox(_("wxSocket demo: Server\n(c) 1999 Guillermo Rodriguez Garcia\n"),
225 wxOK
| wxICON_INFORMATION
, this);
228 void MyFrame::Test1(wxSocketBase
*sock
)
233 m_text
->AppendText(_("Test 1 begins\n"));
235 // Receive data from socket and send it back. We will first
236 // get a byte with the buffer size, so we can specify the
237 // exact size and use the wxSOCKET_WAITALL flag. Also, we
238 // disabled input events so we won't have unwanted reentrance.
239 // This way we can avoid the infamous wxSOCKET_BLOCK flag.
241 sock
->SetFlags(wxSOCKET_WAITALL
);
248 sock
->Read(buf
, len
);
249 m_text
->AppendText(_("Got the data, sending it back\n"));
252 sock
->Write(buf
, len
);
255 m_text
->AppendText(_("Test 1 ends\n\n"));
258 void MyFrame::Test2(wxSocketBase
*sock
)
260 #define MAX_MSG_SIZE 10000
263 wxChar
*buf
= new wxChar
[MAX_MSG_SIZE
];
266 m_text
->AppendText(_("Test 2 begins\n"));
268 // We don't need to set flags because ReadMsg and WriteMsg
269 // are not affected by them anyway.
272 len
= sock
->ReadMsg(buf
, MAX_MSG_SIZE
* sizeof(wxChar
)).LastCount();
273 s
.Printf(_("Client says: %s\n"), buf
);
274 m_text
->AppendText(s
);
275 m_text
->AppendText(_("Sending the data back\n"));
278 sock
->WriteMsg(buf
, len
);
281 m_text
->AppendText(_("Test 2 ends\n\n"));
286 void MyFrame::Test3(wxSocketBase
*sock
)
291 m_text
->AppendText(_("Test 3 begins\n"));
293 // This test is similar to the first one, but the len is
294 // expressed in kbytes - this tests large data transfers.
296 sock
->SetFlags(wxSOCKET_WAITALL
);
300 buf
= new char[len
* 1024];
303 sock
->Read(buf
, len
* 1024);
304 m_text
->AppendText(_("Got the data, sending it back\n"));
307 sock
->Write(buf
, len
* 1024);
310 m_text
->AppendText(_("Test 3 ends\n\n"));
313 void MyFrame::OnServerEvent(wxSocketEvent
& event
)
315 wxString s
= _("OnServerEvent: ");
318 switch(event
.GetSocketEvent())
320 case wxSOCKET_CONNECTION
: s
.Append(_("wxSOCKET_CONNECTION\n")); break;
321 default : s
.Append(_("Unexpected event !\n")); break;
324 m_text
->AppendText(s
);
326 // Accept new connection if there is one in the pending
327 // connections queue, else exit. We use Accept(false) for
328 // non-blocking accept (although if we got here, there
329 // should ALWAYS be a pending connection).
331 sock
= m_server
->Accept(false);
335 m_text
->AppendText(_("New client connection accepted\n\n"));
339 m_text
->AppendText(_("Error: couldn't accept a new connection\n\n"));
343 sock
->SetEventHandler(*this, SOCKET_ID
);
344 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
351 void MyFrame::OnSocketEvent(wxSocketEvent
& event
)
353 wxString s
= _("OnSocketEvent: ");
354 wxSocketBase
*sock
= event
.GetSocket();
356 // First, print a message
357 switch(event
.GetSocketEvent())
359 case wxSOCKET_INPUT
: s
.Append(_("wxSOCKET_INPUT\n")); break;
360 case wxSOCKET_LOST
: s
.Append(_("wxSOCKET_LOST\n")); break;
361 default : s
.Append(_("Unexpected event !\n")); break;
364 m_text
->AppendText(s
);
366 // Now we process the event
367 switch(event
.GetSocketEvent())
371 // We disable input events, so that the test doesn't trigger
372 // wxSocketEvent again.
373 sock
->SetNotify(wxSOCKET_LOST_FLAG
);
375 // Which test are we going to run?
381 case 0xBE: Test1(sock
); break;
382 case 0xCE: Test2(sock
); break;
383 case 0xDE: Test3(sock
); break;
385 m_text
->AppendText(_("Unknown test id received from client\n\n"));
388 // Enable input events again.
389 sock
->SetNotify(wxSOCKET_LOST_FLAG
| wxSOCKET_INPUT_FLAG
);
396 // Destroy() should be used instead of delete wherever possible,
397 // due to the fact that wxSocket uses 'delayed events' (see the
398 // documentation for wxPostEvent) and we don't want an event to
399 // arrive to the event handler (the frame, here) after the socket
400 // has been deleted. Also, we might be doing some other thing with
401 // the socket at the same time; for example, we might be in the
402 // middle of a test or something. Destroy() takes care of all
405 m_text
->AppendText(_("Deleting socket.\n\n"));
415 // convenience functions
417 void MyFrame::UpdateStatusBar()
421 s
.Printf(_("%d clients connected"), m_numClients
);
423 #endif // wxUSE_STATUSBAR