]> git.saurik.com Git - wxWidgets.git/blame - samples/sockets/client.cpp
removed not doing anything and not compiling code from DoAssociation()
[wxWidgets.git] / samples / sockets / client.cpp
CommitLineData
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
f85d901f 20// For compilers that support precompilation, includes "wx/wx.h".
f4ada568
GL
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
f85d901f 24# pragma hdrstop
f4ada568
GL
25#endif
26
925e9792 27// for all others, include the necessary headers
f4ada568 28#ifndef WX_PRECOMP
f85d901f 29# include "wx/wx.h"
09fb22cf
JS
30#endif
31
6097c3a2
GRG
32#include "wx/socket.h"
33#include "wx/url.h"
25ba2b89 34#include "wx/wfstream.h"
e2a6f233 35
f85d901f
GRG
36// --------------------------------------------------------------------------
37// resources
38// --------------------------------------------------------------------------
f4ada568 39
f85d901f 40// the application icon
2804f77d 41#include "mondrian.xpm"
e2a6f233 42
f85d901f
GRG
43// --------------------------------------------------------------------------
44// classes
45// --------------------------------------------------------------------------
f4ada568 46
f85d901f
GRG
47// Define a new application type
48class MyApp : public wxApp
49{
f4ada568 50public:
f85d901f 51 virtual bool OnInit();
f4ada568
GL
52};
53
f85d901f
GRG
54// Define a new frame type: this is going to be our main frame
55class MyFrame : public wxFrame
f4ada568
GL
56{
57public:
f85d901f
GRG
58 MyFrame();
59 ~MyFrame();
60
25ba2b89 61 // event handlers for File menu
f85d901f
GRG
62 void OnQuit(wxCommandEvent& event);
63 void OnAbout(wxCommandEvent& event);
25ba2b89
GRG
64
65 // event handlers for Socket menu
f85d901f
GRG
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);
25ba2b89 71
16cafacf 72#if wxUSE_URL
25ba2b89
GRG
73 // event handlers for Protocols menu
74 void OnTestURL(wxCommandEvent& event);
16cafacf 75#endif
8575ff50
VZ
76#if wxUSE_IPV6
77 void OnOpenConnectionIPv6(wxCommandEvent& event);
78#endif
79
80 void OpenConnection(int family = AF_INET);
25ba2b89 81
e51b0130 82 // event handlers for DatagramSocket menu (stub)
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
91private:
92 wxSocketClient *m_sock;
f85d901f
GRG
93 wxTextCtrl *m_text;
94 wxMenu *m_menuFile;
95 wxMenu *m_menuSocket;
6097c3a2 96 wxMenu *m_menuDatagramSocket;
25ba2b89 97 wxMenu *m_menuProtocols;
f85d901f
GRG
98 wxMenuBar *m_menuBar;
99 bool m_busy;
100
be5a51fb 101 // any class wishing to process wxWidgets events must use this macro
f85d901f 102 DECLARE_EVENT_TABLE()
f4ada568
GL
103};
104
f85d901f
GRG
105// --------------------------------------------------------------------------
106// constants
107// --------------------------------------------------------------------------
f4ada568 108
f85d901f
GRG
109// IDs for the controls and the menu commands
110enum
f4ada568 111{
f85d901f 112 // menu items
91b07357
JS
113 CLIENT_QUIT = wxID_EXIT,
114 CLIENT_ABOUT = wxID_ABOUT,
115 CLIENT_OPEN = 100,
8575ff50
VZ
116#if wxUSE_IPV6
117 CLIENT_OPENIPV6,
118#endif
f85d901f
GRG
119 CLIENT_TEST1,
120 CLIENT_TEST2,
121 CLIENT_TEST3,
122 CLIENT_CLOSE,
16cafacf 123#if wxUSE_URL
25ba2b89 124 CLIENT_TESTURL,
16cafacf 125#endif
6097c3a2 126 CLIENT_DGRAM,
f85d901f
GRG
127
128 // id for socket
129 SOCKET_ID
130};
f4ada568 131
f85d901f 132// --------------------------------------------------------------------------
be5a51fb 133// event tables and other macros for wxWidgets
f85d901f 134// --------------------------------------------------------------------------
f4ada568 135
f85d901f 136BEGIN_EVENT_TABLE(MyFrame, wxFrame)
25ba2b89
GRG
137 EVT_MENU(CLIENT_QUIT, MyFrame::OnQuit)
138 EVT_MENU(CLIENT_ABOUT, MyFrame::OnAbout)
139 EVT_MENU(CLIENT_OPEN, MyFrame::OnOpenConnection)
8575ff50
VZ
140#if wxUSE_IPV6
141 EVT_MENU(CLIENT_OPENIPV6, MyFrame::OnOpenConnectionIPv6)
142#endif
25ba2b89
GRG
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)
16cafacf 148#if wxUSE_URL
25ba2b89 149 EVT_MENU(CLIENT_TESTURL, MyFrame::OnTestURL)
16cafacf 150#endif
25ba2b89 151 EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent)
f85d901f 152END_EVENT_TABLE()
f4ada568 153
f85d901f 154IMPLEMENT_APP(MyApp)
f4ada568 155
f85d901f
GRG
156// ==========================================================================
157// implementation
158// ==========================================================================
f4ada568 159
f85d901f
GRG
160// --------------------------------------------------------------------------
161// the application class
162// --------------------------------------------------------------------------
f4ada568 163
f85d901f
GRG
164bool MyApp::OnInit()
165{
45e6e6f8
VZ
166 if ( !wxApp::OnInit() )
167 return false;
168
f85d901f
GRG
169 // Create the main application window
170 MyFrame *frame = new MyFrame();
f4ada568 171
f85d901f 172 // Show it and tell the application that it's our main window
b62ca03d 173 frame->Show(true);
f85d901f
GRG
174 SetTopWindow(frame);
175
176 // success
b62ca03d 177 return true;
f4ada568
GL
178}
179
f85d901f
GRG
180// --------------------------------------------------------------------------
181// main frame
182// --------------------------------------------------------------------------
183
184// frame constructor
b62ca03d 185MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY,
25ba2b89 186 _("wxSocket demo: Client"),
f85d901f 187 wxDefaultPosition, wxSize(300, 200))
f4ada568 188{
f85d901f
GRG
189 // Give the frame an icon
190 SetIcon(wxICON(mondrian));
191
192 // Make menus
193 m_menuFile = new wxMenu();
25ba2b89 194 m_menuFile->Append(CLIENT_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog"));
f85d901f 195 m_menuFile->AppendSeparator();
25ba2b89 196 m_menuFile->Append(CLIENT_QUIT, _("E&xit\tAlt-X"), _("Quit client"));
f85d901f
GRG
197
198 m_menuSocket = new wxMenu();
25ba2b89 199 m_menuSocket->Append(CLIENT_OPEN, _("&Open session"), _("Connect to server"));
8575ff50
VZ
200#if wxUSE_IPV6
201 m_menuSocket->Append(CLIENT_OPENIPV6, _("&Open session(IPv6)"), _("Connect to server(IPv6)"));
202#endif
f85d901f 203 m_menuSocket->AppendSeparator();
25ba2b89
GRG
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"));
f85d901f 207 m_menuSocket->AppendSeparator();
25ba2b89 208 m_menuSocket->Append(CLIENT_CLOSE, _("&Close session"), _("Close connection"));
f85d901f 209
6097c3a2 210 m_menuDatagramSocket = new wxMenu();
25ba2b89
GRG
211 m_menuDatagramSocket->Append(CLIENT_DGRAM, _("Send Datagram"), _("Test UDP sockets"));
212
16cafacf 213#if wxUSE_URL
25ba2b89
GRG
214 m_menuProtocols = new wxMenu();
215 m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL"), _("Get data from the specified URL"));
16cafacf 216#endif
6097c3a2 217
f85d901f
GRG
218 // Append menus to the menubar
219 m_menuBar = new wxMenuBar();
25ba2b89
GRG
220 m_menuBar->Append(m_menuFile, _("&File"));
221 m_menuBar->Append(m_menuSocket, _("&SocketClient"));
222 m_menuBar->Append(m_menuDatagramSocket, _("&DatagramSocket"));
16cafacf 223#if wxUSE_URL
25ba2b89 224 m_menuBar->Append(m_menuProtocols, _("&Protocols"));
16cafacf 225#endif
f85d901f
GRG
226 SetMenuBar(m_menuBar);
227
8520f137 228#if wxUSE_STATUSBAR
f85d901f 229 // Status bar
f4ada568 230 CreateStatusBar(2);
8520f137 231#endif // wxUSE_STATUSBAR
f85d901f 232
bd4d918f 233 // Make a textctrl for logging
b62ca03d 234 m_text = new wxTextCtrl(this, wxID_ANY,
4693b20c 235 _("Welcome to wxSocket demo: Client\nClient ready\n"),
bd4d918f 236 wxDefaultPosition, wxDefaultSize,
f85d901f
GRG
237 wxTE_MULTILINE | wxTE_READONLY);
238
239 // Create the socket
240 m_sock = new wxSocketClient();
bd4d918f
GRG
241
242 // Setup the event handler and subscribe to most events
f85d901f
GRG
243 m_sock->SetEventHandler(*this, SOCKET_ID);
244 m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG |
245 wxSOCKET_INPUT_FLAG |
246 wxSOCKET_LOST_FLAG);
b62ca03d 247 m_sock->Notify(true);
f85d901f 248
b62ca03d 249 m_busy = false;
f85d901f 250 UpdateStatusBar();
f4ada568
GL
251}
252
253MyFrame::~MyFrame()
254{
bd4d918f 255 // No delayed deletion here, as the frame is dying anyway
f85d901f 256 delete m_sock;
f4ada568
GL
257}
258
f85d901f
GRG
259// event handlers
260
261void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
f4ada568 262{
b62ca03d
WS
263 // true is to force the frame to close
264 Close(true);
f4ada568
GL
265}
266
f85d901f
GRG
267void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
268{
4693b20c 269 wxMessageBox(_("wxSocket demo: Client\n(c) 1999 Guillermo Rodriguez Garcia\n"),
25ba2b89 270 _("About Client"),
f85d901f
GRG
271 wxOK | wxICON_INFORMATION, this);
272}
273
274void MyFrame::OnOpenConnection(wxCommandEvent& WXUNUSED(event))
f4ada568 275{
8575ff50
VZ
276 OpenConnection(AF_INET);
277}
278#if wxUSE_IPV6
279void MyFrame::OnOpenConnectionIPv6(wxCommandEvent& WXUNUSED(event))
280{
281 OpenConnection(AF_INET6);
282}
283#endif // wxUSE_IPV6
284
285void MyFrame::OpenConnection(int family)
286{
287 wxIPaddress * addr;
25248cb6 288 wxIPV4address addr4;
8575ff50
VZ
289#if wxUSE_IPV6
290 wxIPV6address addr6;
25248cb6
VZ
291 if ( family==AF_INET6 )
292 addr = &addr6;
293 else
294 addr = &addr4;
8575ff50 295#else
25248cb6
VZ
296 wxUnusedVar(family);
297 addr = &addr4;
8575ff50 298#endif
f4ada568 299
b62ca03d 300 m_menuSocket->Enable(CLIENT_OPEN, false);
8575ff50
VZ
301#if wxUSE_IPV6
302 m_menuSocket->Enable(CLIENT_OPENIPV6, false);
303#endif
b62ca03d 304 m_menuSocket->Enable(CLIENT_CLOSE, false);
f85d901f 305
25ba2b89 306 // Ask user for server address
f85d901f 307 wxString hostname = wxGetTextFromUser(
25ba2b89
GRG
308 _("Enter the address of the wxSocket demo server:"),
309 _("Connect ..."),
310 _("localhost"));
f85d901f 311
8575ff50
VZ
312 addr->Hostname(hostname);
313 addr->Service(3000);
f4ada568 314
25ba2b89
GRG
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 //
b62ca03d
WS
321 // Connect(addr, true) will wait until the connection completes,
322 // returning true on success and false on failure. This call blocks
25ba2b89
GRG
323 // the GUI (this might be changed in future releases to honour the
324 // wxSOCKET_BLOCK flag).
325 //
b62ca03d
WS
326 // Connect(addr, false) will issue a nonblocking connection request
327 // and return immediately. If the return value is true, then the
3103e8a9 328 // connection has been already successfully established. If it is
b62ca03d 329 // false, you must wait for the request to complete, either with
25ba2b89
GRG
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
b62ca03d 335 // return false on timeout, or true if the connection request
bd4d918f
GRG
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 //
25ba2b89
GRG
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 //
b62ca03d 348 // bool success = client->Connect(addr, true);
25ba2b89
GRG
349 //
350 // For nonblocking Connect:
351 //
b62ca03d 352 // client->Connect(addr, false);
bd4d918f 353 //
b62ca03d 354 // bool waitmore = true;
a85139a1 355 // while (! client->WaitOnConnect(seconds, millis) && waitmore )
bd4d918f
GRG
356 // {
357 // // possibly give some feedback to the user,
358 // // update waitmore if needed.
359 // }
a85139a1 360 // bool success = client->IsConnected();
925e9792 361 //
25ba2b89 362 // And that's all :-)
e51b0130 363
25ba2b89 364 m_text->AppendText(_("\nTrying to connect (timeout = 10 sec) ...\n"));
8575ff50 365 m_sock->Connect(*addr, false);
25ba2b89 366 m_sock->WaitOnConnect(10);
f85d901f
GRG
367
368 if (m_sock->IsConnected())
25ba2b89 369 m_text->AppendText(_("Succeeded ! Connection established\n"));
f85d901f
GRG
370 else
371 {
372 m_sock->Close();
25ba2b89
GRG
373 m_text->AppendText(_("Failed ! Unable to connect\n"));
374 wxMessageBox(_("Can't connect to the specified host"), _("Alert !"));
f85d901f 375 }
925e9792 376
f85d901f 377 UpdateStatusBar();
f4ada568
GL
378}
379
f85d901f 380void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
f4ada568 381{
f85d901f 382 // Disable socket menu entries (exception: Close Session)
b62ca03d 383 m_busy = true;
f85d901f
GRG
384 UpdateStatusBar();
385
25ba2b89 386 m_text->AppendText(_("\n=== Test 1 begins ===\n"));
f85d901f
GRG
387
388 // Tell the server which test we are running
f4d5e009 389 unsigned char c = 0xBE;
f85d901f
GRG
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
6097c3a2
GRG
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.
f85d901f
GRG
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
6097c3a2 400 // value larger than a byte "as is" across the network, or
f85d901f
GRG
401 // you might be in trouble! Ever heard about big and little
402 // endian computers?)
e51b0130 403
f85d901f
GRG
404 m_sock->SetFlags(wxSOCKET_WAITALL);
405
b6ea3123
VZ
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];
f85d901f 409
25ba2b89 410 m_text->AppendText(_("Sending a test buffer to the server ..."));
f187448d 411 m_sock->Write(&len, 1);
f85d901f 412 m_sock->Write(buf1, len);
25ba2b89 413 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
f85d901f 414
25ba2b89 415 m_text->AppendText(_("Receiving the buffer back from server ..."));
f85d901f 416 m_sock->Read(buf2, len);
25ba2b89 417 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
f85d901f 418
25ba2b89 419 m_text->AppendText(_("Comparing the two buffers ..."));
f85d901f
GRG
420 if (memcmp(buf1, buf2, len) != 0)
421 {
25ba2b89
GRG
422 m_text->AppendText(_("failed!\n"));
423 m_text->AppendText(_("Test 1 failed !\n"));
f85d901f
GRG
424 }
425 else
426 {
25ba2b89
GRG
427 m_text->AppendText(_("done\n"));
428 m_text->AppendText(_("Test 1 passed !\n"));
f85d901f 429 }
25ba2b89 430 m_text->AppendText(_("=== Test 1 ends ===\n"));
f4ada568 431
f85d901f 432 delete[] buf2;
b62ca03d 433 m_busy = false;
f85d901f
GRG
434 UpdateStatusBar();
435}
f4ada568 436
f85d901f
GRG
437void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
438{
4693b20c
MB
439 const wxChar *msg1;
440 wxChar *msg2;
f85d901f
GRG
441 size_t len;
442
443 // Disable socket menu entries (exception: Close Session)
b62ca03d 444 m_busy = true;
f85d901f
GRG
445 UpdateStatusBar();
446
25ba2b89 447 m_text->AppendText(_("\n=== Test 2 begins ===\n"));
f85d901f
GRG
448
449 // Tell the server which test we are running
f4d5e009 450 unsigned char c = 0xCE;
f85d901f
GRG
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)
e51b0130 459
f85d901f
GRG
460 m_sock->SetFlags(wxSOCKET_WAITALL);
461
462 wxString s = wxGetTextFromUser(
25ba2b89
GRG
463 _("Enter an arbitrary string to send to the server:"),
464 _("Test 2 ..."),
be5a51fb 465 _("Yes I like wxWidgets!"));
f85d901f 466
4693b20c
MB
467 msg1 = s.c_str();
468 len = (wxStrlen(msg1) + 1) * sizeof(wxChar);
469 msg2 = new wxChar[wxStrlen(msg1) + 1];
f85d901f 470
25ba2b89 471 m_text->AppendText(_("Sending the string with WriteMsg ..."));
f85d901f 472 m_sock->WriteMsg(msg1, len);
25ba2b89
GRG
473 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
474 m_text->AppendText(_("Waiting for an event (timeout = 2 sec)\n"));
f85d901f
GRG
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 {
25ba2b89 481 m_text->AppendText(_("Reading the string back with ReadMsg ..."));
f85d901f 482 m_sock->ReadMsg(msg2, len);
25ba2b89
GRG
483 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
484 m_text->AppendText(_("Comparing the two buffers ..."));
f85d901f
GRG
485 if (memcmp(msg1, msg2, len) != 0)
486 {
925e9792 487 m_text->AppendText(_("failed!\n"));
25ba2b89 488 m_text->AppendText(_("Test 2 failed !\n"));
f85d901f
GRG
489 }
490 else
491 {
25ba2b89
GRG
492 m_text->AppendText(_("done\n"));
493 m_text->AppendText(_("Test 2 passed !\n"));
f85d901f 494 }
765e386b 495 }
f85d901f 496 else
25ba2b89 497 m_text->AppendText(_("Timeout ! Test 2 failed.\n"));
765e386b 498
25ba2b89 499 m_text->AppendText(_("=== Test 2 ends ===\n"));
765e386b 500
6097c3a2 501 delete[] msg2;
b62ca03d 502 m_busy = false;
f85d901f 503 UpdateStatusBar();
765e386b
GL
504}
505
f85d901f 506void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
f4ada568 507{
6097c3a2
GRG
508 char *buf1;
509 char *buf2;
510 unsigned char len;
511
512 // Disable socket menu entries (exception: Close Session)
b62ca03d 513 m_busy = true;
6097c3a2
GRG
514 UpdateStatusBar();
515
25ba2b89 516 m_text->AppendText(_("\n=== Test 3 begins ===\n"));
6097c3a2
GRG
517
518 // Tell the server which test we are running
f4d5e009 519 unsigned char c = 0xDE;
6097c3a2
GRG
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.
e51b0130 526
6097c3a2
GRG
527 m_sock->SetFlags(wxSOCKET_WAITALL);
528
529 // Note that len is in kbytes here!
f4d5e009 530 len = 32;
6097c3a2
GRG
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
25ba2b89 537 m_text->AppendText(_("Sending a large buffer (32K) to the server ..."));
f187448d 538 m_sock->Write(&len, 1);
6097c3a2 539 m_sock->Write(buf1, len * 1024);
25ba2b89 540 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
6097c3a2 541
25ba2b89 542 m_text->AppendText(_("Receiving the buffer back from server ..."));
6097c3a2 543 m_sock->Read(buf2, len * 1024);
25ba2b89 544 m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
6097c3a2 545
25ba2b89 546 m_text->AppendText(_("Comparing the two buffers ..."));
6097c3a2
GRG
547 if (memcmp(buf1, buf2, len) != 0)
548 {
25ba2b89
GRG
549 m_text->AppendText(_("failed!\n"));
550 m_text->AppendText(_("Test 3 failed !\n"));
6097c3a2
GRG
551 }
552 else
553 {
25ba2b89
GRG
554 m_text->AppendText(_("done\n"));
555 m_text->AppendText(_("Test 3 passed !\n"));
6097c3a2 556 }
25ba2b89 557 m_text->AppendText(_("=== Test 3 ends ===\n"));
6097c3a2
GRG
558
559 delete[] buf2;
b62ca03d 560 m_busy = false;
6097c3a2 561 UpdateStatusBar();
f4ada568
GL
562}
563
f85d901f 564void MyFrame::OnCloseConnection(wxCommandEvent& WXUNUSED(event))
f4ada568 565{
f85d901f
GRG
566 m_sock->Close();
567 UpdateStatusBar();
f4ada568
GL
568}
569
6097c3a2
GRG
570void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event))
571{
f187448d 572 m_text->AppendText(_("\n=== Datagram test begins ===\n"));
36bec0ac
GRG
573 m_text->AppendText(_("Sorry, not implemented\n"));
574 m_text->AppendText(_("=== Datagram test ends ===\n"));
6097c3a2
GRG
575}
576
16cafacf
RN
577#if wxUSE_URL
578
25ba2b89
GRG
579void 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:"),
8dfea369 588 _T("http://localhost"));
25ba2b89
GRG
589
590 // Parse the URL
591 wxURL url(urlname);
6f505cae
GRG
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 }
25ba2b89
GRG
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;
4693b20c 612 s.Printf(_("Contents type: %s\nFile size: %i\nStarting to download...\n"),
25ba2b89
GRG
613 url.GetProtocol().GetContentType().c_str(),
614 data->GetSize());
615 m_text->AppendText(s);
616 wxYield();
617
618 // Get the data
061dccd2 619 wxFile fileTest(wxT("test.url"), wxFile::write);
0a3d26b8 620 wxFileOutputStream sout(fileTest);
6f505cae
GRG
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
25ba2b89
GRG
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
16cafacf
RN
636#endif
637
f85d901f 638void MyFrame::OnSocketEvent(wxSocketEvent& event)
f4ada568 639{
25ba2b89 640 wxString s = _("OnSocketEvent: ");
f85d901f 641
bc7e88ae 642 switch(event.GetSocketEvent())
f85d901f 643 {
25ba2b89
GRG
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;
f85d901f 648 }
f4ada568 649
f85d901f
GRG
650 m_text->AppendText(s);
651 UpdateStatusBar();
f4ada568
GL
652}
653
f85d901f 654// convenience functions
a324a7bc 655
f85d901f 656void MyFrame::UpdateStatusBar()
a324a7bc 657{
f85d901f 658 wxString s;
a324a7bc 659
f85d901f
GRG
660 if (!m_sock->IsConnected())
661 {
25ba2b89 662 s.Printf(_("Not connected"));
a324a7bc 663 }
f85d901f
GRG
664 else
665 {
8575ff50
VZ
666#if wxUSE_IPV6
667 wxIPV6address addr;
668#else
f85d901f 669 wxIPV4address addr;
8575ff50 670#endif
a324a7bc 671
f85d901f 672 m_sock->GetPeer(addr);
25ba2b89 673 s.Printf(_("%s : %d"), (addr.Hostname()).c_str(), addr.Service());
f85d901f 674 }
f4ada568 675
8520f137 676#if wxUSE_STATUSBAR
f85d901f 677 SetStatusText(s, 1);
8520f137 678#endif // wxUSE_STATUSBAR
a737331d 679
f85d901f 680 m_menuSocket->Enable(CLIENT_OPEN, !m_sock->IsConnected() && !m_busy);
8575ff50
VZ
681#if wxUSE_IPV6
682 m_menuSocket->Enable(CLIENT_OPENIPV6, !m_sock->IsConnected() && !m_busy);
683#endif
f85d901f
GRG
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());
f4ada568 688}