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