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__)
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
;
88 // any class wishing to process wxWindows events must use this macro
92 // --------------------------------------------------------------------------
94 // --------------------------------------------------------------------------
96 // IDs for the controls and the menu commands
108 // --------------------------------------------------------------------------
109 // event tables and other macros for wxWindows
110 // --------------------------------------------------------------------------
112 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
113 EVT_MENU(SERVER_QUIT
, MyFrame::OnQuit
)
114 EVT_MENU(SERVER_ABOUT
, MyFrame::OnAbout
)
115 EVT_SOCKET(SERVER_ID
, MyFrame::OnServerEvent
)
116 EVT_SOCKET(SOCKET_ID
, MyFrame::OnSocketEvent
)
122 // To append sockets for delayed deletion [XXX: this should be removed]
123 extern WXDLLEXPORT wxList wxPendingDelete
;
126 // ==========================================================================
128 // ==========================================================================
130 // --------------------------------------------------------------------------
131 // the application class
132 // --------------------------------------------------------------------------
136 // Create the main application window
137 MyFrame
*frame
= new MyFrame();
139 // Show it and tell the application that it's our main window
147 // --------------------------------------------------------------------------
149 // --------------------------------------------------------------------------
152 MyFrame::MyFrame() : wxFrame((wxFrame
*)NULL
, -1,
153 _T("wxSocket demo: Server"),
154 wxDefaultPosition
, wxSize(300, 200))
156 // Give the frame an icon
157 SetIcon(wxICON(mondrian
));
160 m_menuFile
= new wxMenu();
161 m_menuFile
->Append(SERVER_ABOUT
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
162 m_menuFile
->AppendSeparator();
163 m_menuFile
->Append(SERVER_QUIT
, _T("E&xit\tAlt-X"), _T("Quit server"));
165 // Append menus to the menubar
166 m_menuBar
= new wxMenuBar();
167 m_menuBar
->Append(m_menuFile
, _T("&File"));
168 SetMenuBar(m_menuBar
);
173 // Make a panel with a textctrl in it
174 m_panel
= new wxPanel(this, -1, wxPoint(0, 0), GetClientSize());
175 m_text
= new wxTextCtrl(m_panel
, -1,
176 _T("Welcome to wxSocket demo: Server\n"),
177 wxPoint(0, 0), m_panel
->GetClientSize(),
178 wxTE_MULTILINE
| wxTE_READONLY
);
180 // Create the socket - defaults to localhost:0
184 m_server
= new wxSocketServer(addr
);
185 m_server
->SetEventHandler(*this, SERVER_ID
);
186 m_server
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
187 m_server
->Notify(TRUE
);
189 // We use Ok() here to see if the server is really listening
191 m_text
->AppendText(_T("Server listening.\n\n"));
193 m_text
->AppendText(_T("Could not listen at the specified port !\n\n"));
207 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
209 // TRUE is to force the frame to close
213 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
215 wxMessageBox(_T("wxSocket demo: Server\n")
216 _T("(c) 1999 Guillermo Rodriguez Garcia\n"),
218 wxOK
| wxICON_INFORMATION
, this);
221 void MyFrame::Test1(wxSocketBase
*sock
)
226 m_text
->AppendText(_T("Test 1 begins\n"));
228 // Receive data from socket and send it back. We will first
229 // get a byte with the buffer size, so we can specify the
230 // exact size and use the wxSOCKET_WAITALL flag. Also, we
231 // disabled input events so we won't have unwanted reentrance.
232 // This way we can avoid the infamous wxSOCKET_BLOCK flag.
234 sock
->SetFlags(wxSOCKET_WAITALL
);
236 sock
->Read((char *)&len
, 1);
239 sock
->Read(buf
, len
);
240 sock
->Write(buf
, len
);
243 m_text
->AppendText(_T("Test 1 ends\n"));
246 void MyFrame::Test2(wxSocketBase
*sock
)
248 #define MAX_MSG_SIZE 10000
251 char *buf
= new char[MAX_MSG_SIZE
];
254 m_text
->AppendText(_T("Test 2 begins\n"));
256 // We don't need to set flags because ReadMsg and WriteMsg
257 // are not affected by them anyway.
259 len
= sock
->ReadMsg(buf
, MAX_MSG_SIZE
).LastCount();
261 s
.Printf(_T("Client says: %s\n"), buf
);
262 m_text
->AppendText(s
);
264 sock
->WriteMsg(buf
, len
);
267 m_text
->AppendText(_T("Test 2 ends\n"));
272 void MyFrame::Test3(wxSocketBase
*sock
)
277 m_text
->AppendText(_T("Test 3 begins\n"));
279 // This test is similar to the first one, but the len is
280 // expressed in kbytes - this tests large data transfers.
282 sock
->SetFlags(wxSOCKET_WAITALL
);
284 sock
->Read((char *)&len
, 1);
285 buf
= new char[len
* 1024];
286 sock
->Read(buf
, len
* 1024);
287 sock
->Write(buf
, len
* 1024);
290 m_text
->AppendText(_T("Test 3 ends\n"));
293 void MyFrame::OnServerEvent(wxSocketEvent
& event
)
295 wxString s
= _T("OnServerEvent: ");
298 switch(event
.SocketEvent())
300 case wxSOCKET_CONNECTION
: s
.Append(_T("wxSOCKET_CONNECTION\n")); break;
301 default : s
.Append(_T("Unexpected event !\n")); break;
304 m_text
->AppendText(s
);
306 // Accept new connection if there is one in the pending
307 // connections queue, else exit. We use Accept(FALSE) for
308 // non-blocking accept (although if we got here, there
309 // should ALWAYS be a pending connection).
311 sock
= m_server
->Accept(FALSE
);
315 m_text
->AppendText(_T("New client connection accepted\n"));
319 m_text
->AppendText(_T("Error: couldn't accept a new connection"));
323 sock
->SetEventHandler(*this, SOCKET_ID
);
324 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
331 void MyFrame::OnSocketEvent(wxSocketEvent
& event
)
333 wxSocketBase
*sock
= event
.Socket();
334 wxString s
= _T("OnSocketEvent: ");
336 // We first print a msg
337 switch(event
.SocketEvent())
339 case wxSOCKET_INPUT
: s
.Append(_T("wxSOCKET_INPUT\n")); break;
340 case wxSOCKET_LOST
: s
.Append(_T("wxSOCKET_LOST\n")); break;
341 default: s
.Append(_T("unexpected event !\n"));
344 m_text
->AppendText(s
);
346 // Now we process the event
347 switch(event
.SocketEvent())
351 // We disable input events, so that the test doesn't trigger
352 // wxSocketEvent again.
353 sock
->SetNotify(wxSOCKET_LOST_FLAG
);
355 // Which test are we going to run?
357 sock
->Read((char *)&c
,1);
361 case 0xBE: Test1(sock
); break;
362 case 0xCE: Test2(sock
); break;
363 case 0xDE: Test3(sock
); break;
364 default: s
.Append(_T("Unknown test id received from client\n"));
367 // Enable input events again.
368 sock
->SetNotify(wxSOCKET_LOST_FLAG
| wxSOCKET_INPUT_FLAG
);
375 // We cannot delete the socket right now because we can
376 // be in the middle of a test or something. So we append
377 // it to the list of objects to be deleted.
378 m_text
->AppendText(_T("Deleting socket.\n"));
379 wxPendingDelete
.Append(sock
);
388 // convenience functions
390 void MyFrame::UpdateStatusBar()
393 s
.Printf(_T("%d clients connected"), m_numClients
);