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