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