]>
Commit | Line | Data |
---|---|---|
f85d901f GRG |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: client.cpp | |
3 | // Purpose: Client for wxSocket demo | |
4 | // Author: Guillermo Rodriguez Garcia <guille@iies.es> | |
5 | // Modified by: | |
6 | // Created: 1999/09/19 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Guillermo Rodriguez Garcia | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ========================================================================== | |
13 | // declarations | |
14 | // ========================================================================== | |
15 | ||
16 | // -------------------------------------------------------------------------- | |
17 | // headers | |
18 | // -------------------------------------------------------------------------- | |
e2a6f233 | 19 | |
f4ada568 | 20 | #ifdef __GNUG__ |
f85d901f GRG |
21 | # pragma implementation "client.cpp" |
22 | # pragma interface "client.cpp" | |
f4ada568 GL |
23 | #endif |
24 | ||
f85d901f | 25 | // For compilers that support precompilation, includes "wx/wx.h". |
f4ada568 GL |
26 | #include "wx/wxprec.h" |
27 | ||
28 | #ifdef __BORLANDC__ | |
f85d901f | 29 | # pragma hdrstop |
f4ada568 GL |
30 | #endif |
31 | ||
f85d901f | 32 | // for all others, include the necessary headers |
f4ada568 | 33 | #ifndef WX_PRECOMP |
f85d901f | 34 | # include "wx/wx.h" |
09fb22cf JS |
35 | #endif |
36 | ||
6097c3a2 GRG |
37 | #include "wx/socket.h" |
38 | #include "wx/url.h" | |
25ba2b89 | 39 | #include "wx/wfstream.h" |
e2a6f233 | 40 | |
f85d901f GRG |
41 | // -------------------------------------------------------------------------- |
42 | // resources | |
43 | // -------------------------------------------------------------------------- | |
f4ada568 | 44 | |
f85d901f GRG |
45 | // the application icon |
46 | #if defined(__WXGTK__) || defined(__WXMOTIF__) | |
47 | # include "mondrian.xpm" | |
e2a6f233 JS |
48 | #endif |
49 | ||
f85d901f GRG |
50 | // -------------------------------------------------------------------------- |
51 | // classes | |
52 | // -------------------------------------------------------------------------- | |
f4ada568 | 53 | |
f85d901f GRG |
54 | // Define a new application type |
55 | class MyApp : public wxApp | |
56 | { | |
f4ada568 | 57 | public: |
f85d901f | 58 | virtual bool OnInit(); |
f4ada568 GL |
59 | }; |
60 | ||
f85d901f GRG |
61 | // Define a new frame type: this is going to be our main frame |
62 | class MyFrame : public wxFrame | |
f4ada568 GL |
63 | { |
64 | public: | |
f85d901f GRG |
65 | MyFrame(); |
66 | ~MyFrame(); | |
67 | ||
25ba2b89 | 68 | // event handlers for File menu |
f85d901f GRG |
69 | void OnQuit(wxCommandEvent& event); |
70 | void OnAbout(wxCommandEvent& event); | |
25ba2b89 GRG |
71 | |
72 | // event handlers for Socket menu | |
f85d901f GRG |
73 | void OnOpenConnection(wxCommandEvent& event); |
74 | void OnTest1(wxCommandEvent& event); | |
75 | void OnTest2(wxCommandEvent& event); | |
76 | void OnTest3(wxCommandEvent& event); | |
77 | void OnCloseConnection(wxCommandEvent& event); | |
25ba2b89 GRG |
78 | |
79 | // event handlers for Protocols menu | |
80 | void OnTestURL(wxCommandEvent& event); | |
81 | ||
82 | // event handlers for DatagramSocket menu | |
6097c3a2 | 83 | void OnDatagram(wxCommandEvent& event); |
f85d901f | 84 | |
25ba2b89 GRG |
85 | // socket event handler |
86 | void OnSocketEvent(wxSocketEvent& event); | |
87 | ||
f85d901f GRG |
88 | // convenience functions |
89 | void UpdateStatusBar(); | |
90 | ||
91 | private: | |
92 | wxSocketClient *m_sock; | |
93 | wxPanel *m_panel; | |
94 | wxTextCtrl *m_text; | |
95 | wxMenu *m_menuFile; | |
96 | wxMenu *m_menuSocket; | |
6097c3a2 | 97 | wxMenu *m_menuDatagramSocket; |
25ba2b89 | 98 | wxMenu *m_menuProtocols; |
f85d901f GRG |
99 | wxMenuBar *m_menuBar; |
100 | bool m_busy; | |
101 | ||
102 | // any class wishing to process wxWindows events must use this macro | |
103 | DECLARE_EVENT_TABLE() | |
f4ada568 GL |
104 | }; |
105 | ||
f85d901f GRG |
106 | // -------------------------------------------------------------------------- |
107 | // constants | |
108 | // -------------------------------------------------------------------------- | |
f4ada568 | 109 | |
f85d901f GRG |
110 | // IDs for the controls and the menu commands |
111 | enum | |
f4ada568 | 112 | { |
f85d901f GRG |
113 | // menu items |
114 | CLIENT_QUIT = 1000, | |
115 | CLIENT_ABOUT, | |
116 | CLIENT_OPEN, | |
117 | CLIENT_TEST1, | |
118 | CLIENT_TEST2, | |
119 | CLIENT_TEST3, | |
120 | CLIENT_CLOSE, | |
25ba2b89 | 121 | CLIENT_TESTURL, |
6097c3a2 | 122 | CLIENT_DGRAM, |
f85d901f GRG |
123 | |
124 | // id for socket | |
125 | SOCKET_ID | |
126 | }; | |
f4ada568 | 127 | |
f85d901f GRG |
128 | // -------------------------------------------------------------------------- |
129 | // event tables and other macros for wxWindows | |
130 | // -------------------------------------------------------------------------- | |
f4ada568 | 131 | |
f85d901f | 132 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
25ba2b89 GRG |
133 | EVT_MENU(CLIENT_QUIT, MyFrame::OnQuit) |
134 | EVT_MENU(CLIENT_ABOUT, MyFrame::OnAbout) | |
135 | EVT_MENU(CLIENT_OPEN, MyFrame::OnOpenConnection) | |
136 | EVT_MENU(CLIENT_TEST1, MyFrame::OnTest1) | |
137 | EVT_MENU(CLIENT_TEST2, MyFrame::OnTest2) | |
138 | EVT_MENU(CLIENT_TEST3, MyFrame::OnTest3) | |
139 | EVT_MENU(CLIENT_CLOSE, MyFrame::OnCloseConnection) | |
140 | EVT_MENU(CLIENT_DGRAM, MyFrame::OnDatagram) | |
141 | EVT_MENU(CLIENT_TESTURL, MyFrame::OnTestURL) | |
142 | EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent) | |
f85d901f | 143 | END_EVENT_TABLE() |
f4ada568 | 144 | |
f85d901f | 145 | IMPLEMENT_APP(MyApp) |
f4ada568 | 146 | |
f85d901f GRG |
147 | // ========================================================================== |
148 | // implementation | |
149 | // ========================================================================== | |
f4ada568 | 150 | |
f85d901f GRG |
151 | // -------------------------------------------------------------------------- |
152 | // the application class | |
153 | // -------------------------------------------------------------------------- | |
f4ada568 | 154 | |
f85d901f GRG |
155 | bool MyApp::OnInit() |
156 | { | |
157 | // Create the main application window | |
158 | MyFrame *frame = new MyFrame(); | |
f4ada568 | 159 | |
f85d901f | 160 | // Show it and tell the application that it's our main window |
f4ada568 | 161 | frame->Show(TRUE); |
f85d901f GRG |
162 | SetTopWindow(frame); |
163 | ||
164 | // success | |
27529614 | 165 | return TRUE; |
f4ada568 GL |
166 | } |
167 | ||
f85d901f GRG |
168 | // -------------------------------------------------------------------------- |
169 | // main frame | |
170 | // -------------------------------------------------------------------------- | |
171 | ||
172 | // frame constructor | |
173 | MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, -1, | |
25ba2b89 | 174 | _("wxSocket demo: Client"), |
f85d901f | 175 | wxDefaultPosition, wxSize(300, 200)) |
f4ada568 | 176 | { |
f85d901f GRG |
177 | // Give the frame an icon |
178 | SetIcon(wxICON(mondrian)); | |
179 | ||
180 | // Make menus | |
181 | m_menuFile = new wxMenu(); | |
25ba2b89 | 182 | m_menuFile->Append(CLIENT_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog")); |
f85d901f | 183 | m_menuFile->AppendSeparator(); |
25ba2b89 | 184 | m_menuFile->Append(CLIENT_QUIT, _("E&xit\tAlt-X"), _("Quit client")); |
f85d901f GRG |
185 | |
186 | m_menuSocket = new wxMenu(); | |
25ba2b89 | 187 | m_menuSocket->Append(CLIENT_OPEN, _("&Open session"), _("Connect to server")); |
f85d901f | 188 | m_menuSocket->AppendSeparator(); |
25ba2b89 GRG |
189 | m_menuSocket->Append(CLIENT_TEST1, _("Test &1"), _("Test basic functionality")); |
190 | m_menuSocket->Append(CLIENT_TEST2, _("Test &2"), _("Test ReadMsg and WriteMsg")); | |
191 | m_menuSocket->Append(CLIENT_TEST3, _("Test &3"), _("Test large data transfer")); | |
f85d901f | 192 | m_menuSocket->AppendSeparator(); |
25ba2b89 | 193 | m_menuSocket->Append(CLIENT_CLOSE, _("&Close session"), _("Close connection")); |
f85d901f | 194 | |
6097c3a2 | 195 | m_menuDatagramSocket = new wxMenu(); |
25ba2b89 GRG |
196 | m_menuDatagramSocket->Append(CLIENT_DGRAM, _("Send Datagram"), _("Test UDP sockets")); |
197 | ||
198 | m_menuProtocols = new wxMenu(); | |
199 | m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL"), _("Get data from the specified URL")); | |
6097c3a2 | 200 | |
f85d901f GRG |
201 | // Append menus to the menubar |
202 | m_menuBar = new wxMenuBar(); | |
25ba2b89 GRG |
203 | m_menuBar->Append(m_menuFile, _("&File")); |
204 | m_menuBar->Append(m_menuSocket, _("&SocketClient")); | |
205 | m_menuBar->Append(m_menuDatagramSocket, _("&DatagramSocket")); | |
206 | m_menuBar->Append(m_menuProtocols, _("&Protocols")); | |
f85d901f GRG |
207 | SetMenuBar(m_menuBar); |
208 | ||
209 | // Status bar | |
f4ada568 | 210 | CreateStatusBar(2); |
f85d901f GRG |
211 | |
212 | // Make a panel with a textctrl in it | |
213 | m_panel = new wxPanel(this, -1, wxPoint(0, 0), GetClientSize()); | |
214 | m_text = new wxTextCtrl(m_panel, -1, | |
25ba2b89 GRG |
215 | _("Welcome to wxSocket demo: Client\n" |
216 | "Client ready\n"), | |
f85d901f GRG |
217 | wxPoint(0, 0), m_panel->GetClientSize(), |
218 | wxTE_MULTILINE | wxTE_READONLY); | |
219 | ||
220 | // Create the socket | |
221 | m_sock = new wxSocketClient(); | |
222 | m_sock->SetEventHandler(*this, SOCKET_ID); | |
223 | m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG | | |
224 | wxSOCKET_INPUT_FLAG | | |
225 | wxSOCKET_LOST_FLAG); | |
226 | m_sock->Notify(TRUE); | |
227 | ||
228 | m_busy = FALSE; | |
229 | UpdateStatusBar(); | |
f4ada568 GL |
230 | } |
231 | ||
232 | MyFrame::~MyFrame() | |
233 | { | |
f85d901f | 234 | delete m_sock; |
f4ada568 GL |
235 | } |
236 | ||
f85d901f GRG |
237 | // event handlers |
238 | ||
239 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
f4ada568 | 240 | { |
f85d901f | 241 | // TRUE is to force the frame to close |
f4ada568 GL |
242 | Close(TRUE); |
243 | } | |
244 | ||
f85d901f GRG |
245 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
246 | { | |
25ba2b89 GRG |
247 | wxMessageBox(_("wxSocket demo: Client\n" |
248 | "(c) 1999 Guillermo Rodriguez Garcia\n"), | |
249 | _("About Client"), | |
f85d901f GRG |
250 | wxOK | wxICON_INFORMATION, this); |
251 | } | |
252 | ||
253 | void MyFrame::OnOpenConnection(wxCommandEvent& WXUNUSED(event)) | |
f4ada568 GL |
254 | { |
255 | wxIPV4address addr; | |
f4ada568 | 256 | |
f85d901f GRG |
257 | m_menuSocket->Enable(CLIENT_OPEN, FALSE); |
258 | m_menuSocket->Enable(CLIENT_CLOSE, FALSE); | |
259 | ||
25ba2b89 | 260 | // Ask user for server address |
f85d901f | 261 | wxString hostname = wxGetTextFromUser( |
25ba2b89 GRG |
262 | _("Enter the address of the wxSocket demo server:"), |
263 | _("Connect ..."), | |
264 | _("localhost")); | |
f85d901f GRG |
265 | |
266 | addr.Hostname(hostname); | |
f4ada568 | 267 | addr.Service(3000); |
f4ada568 | 268 | |
25ba2b89 GRG |
269 | // Mini-tutorial for Connect() :-) |
270 | // --------------------------- | |
271 | // | |
272 | // There are two ways to use Connect(): blocking and non-blocking, | |
273 | // depending on the value passed as the 'wait' (2nd) parameter. | |
274 | // | |
275 | // Connect(addr, TRUE) will wait until the connection completes, | |
276 | // returning TRUE on success and FALSE on failure. This call blocks | |
277 | // the GUI (this might be changed in future releases to honour the | |
278 | // wxSOCKET_BLOCK flag). | |
279 | // | |
280 | // Connect(addr, FALSE) will issue a nonblocking connection request | |
281 | // and return immediately. If the return value is TRUE, then the | |
282 | // connection has been already succesfully established. If it is | |
283 | // FALSE, you must wait for the request to complete, either with | |
284 | // WaitOnConnect() or by watching wxSOCKET_CONNECTION / LOST | |
285 | // events (please read the documentation). | |
286 | // | |
287 | // WaitOnConnect() itself never blocks the GUI (this might change | |
288 | // in the future to honour the wxSOCKET_BLOCK flag). This call will | |
289 | // return TRUE if the connection request completed succesfully, or | |
290 | // FALSE otherwise, which in turn might mean: | |
291 | // a) that the specified timeout ellapsed, or | |
292 | // b) that the connection request failed. | |
293 | // Use IsConnected() to distinguish between these two. | |
294 | // | |
295 | // So, in a brief, you should do one of the following things: | |
296 | // | |
297 | // For blocking Connect: | |
298 | // | |
299 | // bool success = Connect(addr, TRUE); | |
300 | // | |
301 | // For nonblocking Connect: | |
302 | // | |
303 | // Connect(addr, FALSE); | |
304 | // WaitOnConnect(seconds, millis); | |
305 | // success = IsConnected(); | |
306 | // | |
307 | // And that's all :-) | |
308 | // | |
309 | m_text->AppendText(_("\nTrying to connect (timeout = 10 sec) ...\n")); | |
310 | m_sock->Connect(addr, FALSE); | |
311 | m_sock->WaitOnConnect(10); | |
f85d901f GRG |
312 | |
313 | if (m_sock->IsConnected()) | |
25ba2b89 | 314 | m_text->AppendText(_("Succeeded ! Connection established\n")); |
f85d901f GRG |
315 | else |
316 | { | |
317 | m_sock->Close(); | |
25ba2b89 GRG |
318 | m_text->AppendText(_("Failed ! Unable to connect\n")); |
319 | wxMessageBox(_("Can't connect to the specified host"), _("Alert !")); | |
f85d901f GRG |
320 | } |
321 | ||
322 | UpdateStatusBar(); | |
f4ada568 GL |
323 | } |
324 | ||
f85d901f | 325 | void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 326 | { |
25ba2b89 GRG |
327 | const char *buf1; |
328 | char *buf2; | |
6097c3a2 | 329 | unsigned char len; |
f85d901f GRG |
330 | |
331 | // Disable socket menu entries (exception: Close Session) | |
332 | m_busy = TRUE; | |
333 | UpdateStatusBar(); | |
334 | ||
25ba2b89 | 335 | m_text->AppendText(_("\n=== Test 1 begins ===\n")); |
f85d901f GRG |
336 | |
337 | // Tell the server which test we are running | |
338 | char c = 0xBE; | |
339 | m_sock->Write(&c, 1); | |
340 | ||
341 | // Send some data and read it back. We know the size of the | |
342 | // buffer, so we can specify the exact number of bytes to be | |
6097c3a2 GRG |
343 | // sent or received and use the wxSOCKET_WAITALL flag. Also, |
344 | // we have disabled menu entries which could interfere with | |
345 | // the test, so we can safely avoid the wxSOCKET_BLOCK flag. | |
f85d901f GRG |
346 | // |
347 | // First we send a byte with the length of the string, then | |
348 | // we send the string itself (do NOT try to send any integral | |
6097c3a2 | 349 | // value larger than a byte "as is" across the network, or |
f85d901f GRG |
350 | // you might be in trouble! Ever heard about big and little |
351 | // endian computers?) | |
352 | // | |
353 | m_sock->SetFlags(wxSOCKET_WAITALL); | |
354 | ||
25ba2b89 | 355 | buf1 = _("Test string (less than 256 chars!)"); |
f85d901f GRG |
356 | len = wxStrlen(buf1) + 1; |
357 | buf2 = new char[len]; | |
358 | ||
25ba2b89 | 359 | m_text->AppendText(_("Sending a test buffer to the server ...")); |
6097c3a2 | 360 | m_sock->Write((char *)&len, 1); |
f85d901f | 361 | m_sock->Write(buf1, len); |
25ba2b89 | 362 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
f85d901f | 363 | |
25ba2b89 | 364 | m_text->AppendText(_("Receiving the buffer back from server ...")); |
f85d901f | 365 | m_sock->Read(buf2, len); |
25ba2b89 | 366 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
f85d901f | 367 | |
25ba2b89 | 368 | m_text->AppendText(_("Comparing the two buffers ...")); |
f85d901f GRG |
369 | if (memcmp(buf1, buf2, len) != 0) |
370 | { | |
25ba2b89 GRG |
371 | m_text->AppendText(_("failed!\n")); |
372 | m_text->AppendText(_("Test 1 failed !\n")); | |
f85d901f GRG |
373 | } |
374 | else | |
375 | { | |
25ba2b89 GRG |
376 | m_text->AppendText(_("done\n")); |
377 | m_text->AppendText(_("Test 1 passed !\n")); | |
f85d901f | 378 | } |
25ba2b89 | 379 | m_text->AppendText(_("=== Test 1 ends ===\n")); |
f4ada568 | 380 | |
f85d901f GRG |
381 | delete[] buf2; |
382 | m_busy = FALSE; | |
383 | UpdateStatusBar(); | |
384 | } | |
f4ada568 | 385 | |
f85d901f GRG |
386 | void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event)) |
387 | { | |
388 | char *msg1; | |
389 | char *msg2; | |
390 | size_t len; | |
391 | ||
392 | // Disable socket menu entries (exception: Close Session) | |
393 | m_busy = TRUE; | |
394 | UpdateStatusBar(); | |
395 | ||
25ba2b89 | 396 | m_text->AppendText(_("\n=== Test 2 begins ===\n")); |
f85d901f GRG |
397 | |
398 | // Tell the server which test we are running | |
399 | char c = 0xCE; | |
400 | m_sock->Write(&c, 1); | |
401 | ||
402 | // Here we use ReadMsg and WriteMsg to send messages with | |
403 | // a header with size information. Also, the reception is | |
404 | // event triggered, so we test input events as well. | |
405 | // | |
406 | // We need to set no flags here (ReadMsg and WriteMsg are | |
407 | // not affected by flags) | |
408 | // | |
409 | m_sock->SetFlags(wxSOCKET_WAITALL); | |
410 | ||
411 | wxString s = wxGetTextFromUser( | |
25ba2b89 GRG |
412 | _("Enter an arbitrary string to send to the server:"), |
413 | _("Test 2 ..."), | |
414 | _("Yes I like wxWindows!")); | |
f85d901f GRG |
415 | |
416 | msg1 = (char *)s.c_str(); | |
417 | len = wxStrlen(msg1) + 1; | |
6097c3a2 | 418 | msg2 = new char[len]; |
f85d901f | 419 | |
25ba2b89 | 420 | m_text->AppendText(_("Sending the string with WriteMsg ...")); |
f85d901f | 421 | m_sock->WriteMsg(msg1, len); |
25ba2b89 GRG |
422 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
423 | m_text->AppendText(_("Waiting for an event (timeout = 2 sec)\n")); | |
f85d901f GRG |
424 | |
425 | // Wait until data available (will also return if the connection is lost) | |
426 | m_sock->WaitForRead(2); | |
427 | ||
428 | if (m_sock->IsData()) | |
429 | { | |
25ba2b89 | 430 | m_text->AppendText(_("Reading the string back with ReadMsg ...")); |
f85d901f | 431 | m_sock->ReadMsg(msg2, len); |
25ba2b89 GRG |
432 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
433 | m_text->AppendText(_("Comparing the two buffers ...")); | |
f85d901f GRG |
434 | if (memcmp(msg1, msg2, len) != 0) |
435 | { | |
25ba2b89 GRG |
436 | m_text->AppendText(_("failed!\n")); |
437 | m_text->AppendText(_("Test 2 failed !\n")); | |
f85d901f GRG |
438 | } |
439 | else | |
440 | { | |
25ba2b89 GRG |
441 | m_text->AppendText(_("done\n")); |
442 | m_text->AppendText(_("Test 2 passed !\n")); | |
f85d901f | 443 | } |
765e386b | 444 | } |
f85d901f | 445 | else |
25ba2b89 | 446 | m_text->AppendText(_("Timeout ! Test 2 failed.\n")); |
765e386b | 447 | |
25ba2b89 | 448 | m_text->AppendText(_("=== Test 2 ends ===\n")); |
765e386b | 449 | |
6097c3a2 | 450 | delete[] msg2; |
f85d901f GRG |
451 | m_busy = FALSE; |
452 | UpdateStatusBar(); | |
765e386b GL |
453 | } |
454 | ||
f85d901f | 455 | void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 456 | { |
6097c3a2 GRG |
457 | char *buf1; |
458 | char *buf2; | |
459 | unsigned char len; | |
460 | ||
461 | // Disable socket menu entries (exception: Close Session) | |
462 | m_busy = TRUE; | |
463 | UpdateStatusBar(); | |
464 | ||
25ba2b89 | 465 | m_text->AppendText(_("\n=== Test 3 begins ===\n")); |
6097c3a2 GRG |
466 | |
467 | // Tell the server which test we are running | |
468 | char c = 0xDE; | |
469 | m_sock->Write(&c, 1); | |
470 | ||
471 | // This test also is similar to the first one but it sends a | |
472 | // large buffer so that wxSocket is actually forced to split | |
473 | // it into pieces and take care of sending everything before | |
474 | // returning. | |
475 | // | |
476 | m_sock->SetFlags(wxSOCKET_WAITALL); | |
477 | ||
478 | // Note that len is in kbytes here! | |
479 | len = 32; | |
480 | buf1 = new char[len * 1024]; | |
481 | buf2 = new char[len * 1024]; | |
482 | ||
483 | for (int i = 0; i < len * 1024; i ++) | |
484 | buf1[i] = (char)(i % 256); | |
485 | ||
25ba2b89 | 486 | m_text->AppendText(_("Sending a large buffer (32K) to the server ...")); |
6097c3a2 GRG |
487 | m_sock->Write((char *)&len, 1); |
488 | m_sock->Write(buf1, len * 1024); | |
25ba2b89 | 489 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
6097c3a2 | 490 | |
25ba2b89 | 491 | m_text->AppendText(_("Receiving the buffer back from server ...")); |
6097c3a2 | 492 | m_sock->Read(buf2, len * 1024); |
25ba2b89 | 493 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
6097c3a2 | 494 | |
25ba2b89 | 495 | m_text->AppendText(_("Comparing the two buffers ...")); |
6097c3a2 GRG |
496 | if (memcmp(buf1, buf2, len) != 0) |
497 | { | |
25ba2b89 GRG |
498 | m_text->AppendText(_("failed!\n")); |
499 | m_text->AppendText(_("Test 3 failed !\n")); | |
6097c3a2 GRG |
500 | } |
501 | else | |
502 | { | |
25ba2b89 GRG |
503 | m_text->AppendText(_("done\n")); |
504 | m_text->AppendText(_("Test 3 passed !\n")); | |
6097c3a2 | 505 | } |
25ba2b89 | 506 | m_text->AppendText(_("=== Test 3 ends ===\n")); |
6097c3a2 GRG |
507 | |
508 | delete[] buf2; | |
509 | m_busy = FALSE; | |
510 | UpdateStatusBar(); | |
f4ada568 GL |
511 | } |
512 | ||
f85d901f | 513 | void MyFrame::OnCloseConnection(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 514 | { |
f85d901f GRG |
515 | m_sock->Close(); |
516 | UpdateStatusBar(); | |
f4ada568 GL |
517 | } |
518 | ||
6097c3a2 GRG |
519 | void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event)) |
520 | { | |
521 | } | |
522 | ||
25ba2b89 GRG |
523 | void MyFrame::OnTestURL(wxCommandEvent& WXUNUSED(event)) |
524 | { | |
525 | // Note that we are creating a new socket here, so this | |
526 | // won't mess with the client/server demo. | |
527 | ||
528 | // Ask for the URL | |
529 | m_text->AppendText(_("\n=== URL test begins ===\n")); | |
530 | wxString urlname = wxGetTextFromUser(_("Enter an URL to get"), | |
531 | _("URL:"), | |
532 | _("http://localhost")); | |
533 | ||
534 | // Parse the URL | |
535 | wxURL url(urlname); | |
536 | ||
537 | // Try to get the input stream (connects to the given URL) | |
538 | m_text->AppendText(_("Trying to establish connection...\n")); | |
539 | wxYield(); | |
540 | wxInputStream *data = url.GetInputStream(); | |
541 | if (!data) | |
542 | { | |
543 | m_text->AppendText(_("Error: couldn't read from URL\n")); | |
544 | m_text->AppendText(_("=== URL test ends ===\n")); | |
545 | return; | |
546 | } | |
547 | ||
548 | // Print the contents type and file size | |
549 | wxString s; | |
550 | s.Printf(_("Contents type: %s\n" | |
551 | "File size: %i\n" | |
552 | "Starting to download...\n"), | |
553 | url.GetProtocol().GetContentType().c_str(), | |
554 | data->GetSize()); | |
555 | m_text->AppendText(s); | |
556 | wxYield(); | |
557 | ||
558 | // Get the data | |
559 | wxFileOutputStream sout("test.url"); | |
560 | data->Read(sout); | |
561 | m_text->AppendText(_("Results written to file: test.url\n")); | |
562 | m_text->AppendText(_("Done.\n")); | |
563 | m_text->AppendText(_("=== URL test ends ===\n")); | |
564 | ||
565 | delete data; | |
566 | } | |
567 | ||
f85d901f | 568 | void MyFrame::OnSocketEvent(wxSocketEvent& event) |
f4ada568 | 569 | { |
25ba2b89 | 570 | wxString s = _("OnSocketEvent: "); |
f85d901f GRG |
571 | |
572 | switch(event.SocketEvent()) | |
573 | { | |
25ba2b89 GRG |
574 | case wxSOCKET_INPUT : s.Append(_("wxSOCKET_INPUT\n")); break; |
575 | case wxSOCKET_LOST : s.Append(_("wxSOCKET_LOST\n")); break; | |
576 | case wxSOCKET_CONNECTION : s.Append(_("wxSOCKET_CONNECTION\n")); break; | |
577 | default : s.Append(_("Unexpected event !\n")); break; | |
f85d901f | 578 | } |
f4ada568 | 579 | |
f85d901f GRG |
580 | m_text->AppendText(s); |
581 | UpdateStatusBar(); | |
f4ada568 GL |
582 | } |
583 | ||
f85d901f | 584 | // convenience functions |
a324a7bc | 585 | |
f85d901f | 586 | void MyFrame::UpdateStatusBar() |
a324a7bc | 587 | { |
f85d901f | 588 | wxString s; |
a324a7bc | 589 | |
f85d901f GRG |
590 | if (!m_sock->IsConnected()) |
591 | { | |
25ba2b89 | 592 | s.Printf(_("Not connected")); |
a324a7bc | 593 | } |
f85d901f GRG |
594 | else |
595 | { | |
596 | wxIPV4address addr; | |
a324a7bc | 597 | |
f85d901f | 598 | m_sock->GetPeer(addr); |
25ba2b89 | 599 | s.Printf(_("%s : %d"), (addr.Hostname()).c_str(), addr.Service()); |
f85d901f | 600 | } |
f4ada568 | 601 | |
f85d901f | 602 | SetStatusText(s, 1); |
a737331d | 603 | |
f85d901f GRG |
604 | m_menuSocket->Enable(CLIENT_OPEN, !m_sock->IsConnected() && !m_busy); |
605 | m_menuSocket->Enable(CLIENT_TEST1, m_sock->IsConnected() && !m_busy); | |
606 | m_menuSocket->Enable(CLIENT_TEST2, m_sock->IsConnected() && !m_busy); | |
607 | m_menuSocket->Enable(CLIENT_TEST3, m_sock->IsConnected() && !m_busy); | |
608 | m_menuSocket->Enable(CLIENT_CLOSE, m_sock->IsConnected()); | |
f4ada568 | 609 | } |