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