]>
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 // --------------------------------------------------------------------------
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
37 #include "wx/socket.h"
39 // --------------------------------------------------------------------------
41 // --------------------------------------------------------------------------
43 // the application icon
44 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
45 # include "mondrian.xpm"
48 // --------------------------------------------------------------------------
50 // --------------------------------------------------------------------------
52 // Define a new application type
53 class MyApp
: public wxApp
56 virtual bool OnInit();
59 // Define a new frame type: this is going to be our main frame
60 class MyFrame
: public wxFrame
66 // event handlers (these functions should _not_ be virtual)
67 void OnQuit(wxCommandEvent
& event
);
68 void OnAbout(wxCommandEvent
& event
);
69 void OnServerEvent(wxSocketEvent
& event
);
70 void OnSocketEvent(wxSocketEvent
& event
);
72 void Test1(wxSocketBase
*sock
);
73 void Test2(wxSocketBase
*sock
);
74 void Test3(wxSocketBase
*sock
);
76 // convenience functions
77 void UpdateStatusBar();
80 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 // ==========================================================================
123 // ==========================================================================
125 // --------------------------------------------------------------------------
126 // the application class
127 // --------------------------------------------------------------------------
131 // Create the main application window
132 MyFrame
*frame
= new MyFrame();
134 // Show it and tell the application that it's our main window
142 // --------------------------------------------------------------------------
144 // --------------------------------------------------------------------------
148 MyFrame::MyFrame() : wxFrame((wxFrame
*)NULL
, -1,
149 _("wxSocket demo: Server"),
150 wxDefaultPosition
, wxSize(300, 200))
152 // Give the frame an icon
153 SetIcon(wxICON(mondrian
));
156 m_menuFile
= new wxMenu();
157 m_menuFile
->Append(SERVER_ABOUT
, _("&About...\tCtrl-A"), _("Show about dialog"));
158 m_menuFile
->AppendSeparator();
159 m_menuFile
->Append(SERVER_QUIT
, _("E&xit\tAlt-X"), _("Quit server"));
161 // Append menus to the menubar
162 m_menuBar
= new wxMenuBar();
163 m_menuBar
->Append(m_menuFile
, _("&File"));
164 SetMenuBar(m_menuBar
);
169 // Make a textctrl for logging
170 m_text
= new wxTextCtrl(this, -1,
171 _("Welcome to wxSocket demo: Server\n"),
172 wxDefaultPosition
, wxDefaultSize
,
173 wxTE_MULTILINE
| wxTE_READONLY
);
175 // Create the address - defaults to localhost:0 initially
180 m_server
= new wxSocketServer(addr
);
182 // We use Ok() here to see if the server is really listening
183 if (! m_server
->Ok())
185 m_text
->AppendText(_("Could not listen at the specified port !\n\n"));
190 m_text
->AppendText(_("Server listening.\n\n"));
193 // Setup the event handler and subscribe to connection events
194 m_server
->SetEventHandler(*this, SERVER_ID
);
195 m_server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
196 m_server
->Notify(TRUE
);
205 // No delayed deletion here, as the frame is dying anyway
211 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
213 // TRUE is to force the frame to close
217 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
219 wxMessageBox(_("wxSocket demo: Server\n"
220 "(c) 1999 Guillermo Rodriguez Garcia\n"),
222 wxOK
| wxICON_INFORMATION
, this);
225 void MyFrame::Test1(wxSocketBase
*sock
)
230 m_text
->AppendText(_("Test 1 begins\n"));
232 // Receive data from socket and send it back. We will first
233 // get a byte with the buffer size, so we can specify the
234 // exact size and use the wxSOCKET_WAITALL flag. Also, we
235 // disabled input events so we won't have unwanted reentrance.
236 // This way we can avoid the infamous wxSOCKET_BLOCK flag.
238 sock
->SetFlags(wxSOCKET_WAITALL
);
245 sock
->Read(buf
, len
);
246 m_text
->AppendText(_("Got the data, sending it back\n"));
249 sock
->Write(buf
, len
);
252 m_text
->AppendText(_("Test 1 ends\n\n"));
255 void MyFrame::Test2(wxSocketBase
*sock
)
257 #define MAX_MSG_SIZE 10000
260 char *buf
= new char[MAX_MSG_SIZE
];
263 m_text
->AppendText(_("Test 2 begins\n"));
265 // We don't need to set flags because ReadMsg and WriteMsg
266 // are not affected by them anyway.
269 len
= sock
->ReadMsg(buf
, MAX_MSG_SIZE
).LastCount();
270 s
.Printf(_("Client says: %s\n"), buf
);
271 m_text
->AppendText(s
);
272 m_text
->AppendText(_("Sending the data back\n"));
275 sock
->WriteMsg(buf
, len
);
278 m_text
->AppendText(_("Test 2 ends\n\n"));
283 void MyFrame::Test3(wxSocketBase
*sock
)
288 m_text
->AppendText(_("Test 3 begins\n"));
290 // This test is similar to the first one, but the len is
291 // expressed in kbytes - this tests large data transfers.
293 sock
->SetFlags(wxSOCKET_WAITALL
);
297 buf
= new char[len
* 1024];
300 sock
->Read(buf
, len
* 1024);
301 m_text
->AppendText(_("Got the data, sending it back\n"));
304 sock
->Write(buf
, len
* 1024);
307 m_text
->AppendText(_("Test 3 ends\n\n"));
310 void MyFrame::OnServerEvent(wxSocketEvent
& event
)
312 wxString s
= _("OnServerEvent: ");
315 switch(event
.GetSocketEvent())
317 case wxSOCKET_CONNECTION
: s
.Append(_("wxSOCKET_CONNECTION\n")); break;
318 default : s
.Append(_("Unexpected event !\n")); break;
321 m_text
->AppendText(s
);
323 // Accept new connection if there is one in the pending
324 // connections queue, else exit. We use Accept(FALSE) for
325 // non-blocking accept (although if we got here, there
326 // should ALWAYS be a pending connection).
328 sock
= m_server
->Accept(FALSE
);
332 m_text
->AppendText(_("New client connection accepted\n\n"));
336 m_text
->AppendText(_("Error: couldn't accept a new connection\n\n"));
340 sock
->SetEventHandler(*this, SOCKET_ID
);
341 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
348 void MyFrame::OnSocketEvent(wxSocketEvent
& event
)
350 wxString s
= _("OnSocketEvent: ");
351 wxSocketBase
*sock
= event
.GetSocket();
353 // First, print a message
354 switch(event
.GetSocketEvent())
356 case wxSOCKET_INPUT
: s
.Append(_("wxSOCKET_INPUT\n")); break;
357 case wxSOCKET_LOST
: s
.Append(_("wxSOCKET_LOST\n")); break;
358 default : s
.Append(_("Unexpected event !\n")); break;
361 m_text
->AppendText(s
);
363 // Now we process the event
364 switch(event
.GetSocketEvent())
368 // We disable input events, so that the test doesn't trigger
369 // wxSocketEvent again.
370 sock
->SetNotify(wxSOCKET_LOST_FLAG
);
372 // Which test are we going to run?
378 case 0xBE: Test1(sock
); break;
379 case 0xCE: Test2(sock
); break;
380 case 0xDE: Test3(sock
); break;
382 m_text
->AppendText(_("Unknown test id received from client\n\n"));
385 // Enable input events again.
386 sock
->SetNotify(wxSOCKET_LOST_FLAG
| wxSOCKET_INPUT_FLAG
);
393 // Destroy() should be used instead of delete wherever possible,
394 // due to the fact that wxSocket uses 'delayed events' (see the
395 // documentation for wxPostEvent) and we don't want an event to
396 // arrive to the event handler (the frame, here) after the socket
397 // has been deleted. Also, we might be doing some other thing with
398 // the socket at the same time; for example, we might be in the
399 // middle of a test or something. Destroy() takes care of all
402 m_text
->AppendText(_("Deleting socket.\n\n"));
412 // convenience functions
414 void MyFrame::UpdateStatusBar()
417 s
.Printf(_("%d clients connected"), m_numClients
);