1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Client for wxSocket demo
4 // Author: Guillermo Rodriguez Garcia <guille@iies.es>
8 // Copyright: (c) 1999 Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ==========================================================================
14 // ==========================================================================
16 // --------------------------------------------------------------------------
18 // --------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
32 #include "wx/socket.h"
34 #include "wx/wfstream.h"
36 // --------------------------------------------------------------------------
38 // --------------------------------------------------------------------------
40 // the application icon
41 #include "mondrian.xpm"
43 // --------------------------------------------------------------------------
45 // --------------------------------------------------------------------------
47 // Define a new application type
48 class MyApp
: public wxApp
51 virtual bool OnInit();
54 // Define a new frame type: this is going to be our main frame
55 class MyFrame
: public wxFrame
61 // event handlers for File menu
62 void OnQuit(wxCommandEvent
& event
);
63 void OnAbout(wxCommandEvent
& event
);
65 // event handlers for Socket menu
66 void OnOpenConnection(wxCommandEvent
& event
);
67 void OnTest1(wxCommandEvent
& event
);
68 void OnTest2(wxCommandEvent
& event
);
69 void OnTest3(wxCommandEvent
& event
);
70 void OnCloseConnection(wxCommandEvent
& event
);
73 // event handlers for Protocols menu
74 void OnTestURL(wxCommandEvent
& event
);
77 void OnOpenConnectionIPv6(wxCommandEvent
& event
);
80 void OpenConnection(wxSockAddress
::Family family
);
82 // event handlers for DatagramSocket menu (stub)
83 void OnDatagram(wxCommandEvent
& event
);
85 // socket event handler
86 void OnSocketEvent(wxSocketEvent
& event
);
88 // convenience functions
89 void UpdateStatusBar();
92 wxSocketClient
*m_sock
;
96 wxMenu
*m_menuDatagramSocket
;
97 wxMenu
*m_menuProtocols
;
101 // any class wishing to process wxWidgets events must use this macro
102 DECLARE_EVENT_TABLE()
105 // --------------------------------------------------------------------------
107 // --------------------------------------------------------------------------
109 // IDs for the controls and the menu commands
113 CLIENT_QUIT
= wxID_EXIT
,
114 CLIENT_ABOUT
= wxID_ABOUT
,
132 // --------------------------------------------------------------------------
133 // event tables and other macros for wxWidgets
134 // --------------------------------------------------------------------------
136 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
137 EVT_MENU(CLIENT_QUIT
, MyFrame
::OnQuit
)
138 EVT_MENU(CLIENT_ABOUT
, MyFrame
::OnAbout
)
139 EVT_MENU(CLIENT_OPEN
, MyFrame
::OnOpenConnection
)
141 EVT_MENU(CLIENT_OPENIPV6
, MyFrame
::OnOpenConnectionIPv6
)
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
)
149 EVT_MENU(CLIENT_TESTURL
, MyFrame
::OnTestURL
)
151 EVT_SOCKET(SOCKET_ID
, MyFrame
::OnSocketEvent
)
156 // ==========================================================================
158 // ==========================================================================
160 // --------------------------------------------------------------------------
161 // the application class
162 // --------------------------------------------------------------------------
166 if ( !wxApp
::OnInit() )
169 // Create the main application window
170 MyFrame
*frame
= new MyFrame();
172 // Show it and tell the application that it's our main window
180 // --------------------------------------------------------------------------
182 // --------------------------------------------------------------------------
185 MyFrame
::MyFrame() : wxFrame((wxFrame
*)NULL
, wxID_ANY
,
186 _("wxSocket demo: Client"),
187 wxDefaultPosition
, wxSize(300, 200))
189 // Give the frame an icon
190 SetIcon(wxICON(mondrian
));
193 m_menuFile
= new wxMenu();
194 m_menuFile
->Append(CLIENT_ABOUT
, _("&About...\tCtrl-A"), _("Show about dialog"));
195 m_menuFile
->AppendSeparator();
196 m_menuFile
->Append(CLIENT_QUIT
, _("E&xit\tAlt-X"), _("Quit client"));
198 m_menuSocket
= new wxMenu();
199 m_menuSocket
->Append(CLIENT_OPEN
, _("&Open session"), _("Connect to server"));
201 m_menuSocket
->Append(CLIENT_OPENIPV6
, _("&Open session(IPv6)"), _("Connect to server(IPv6)"));
203 m_menuSocket
->AppendSeparator();
204 m_menuSocket
->Append(CLIENT_TEST1
, _("Test &1"), _("Test basic functionality"));
205 m_menuSocket
->Append(CLIENT_TEST2
, _("Test &2"), _("Test ReadMsg and WriteMsg"));
206 m_menuSocket
->Append(CLIENT_TEST3
, _("Test &3"), _("Test large data transfer"));
207 m_menuSocket
->AppendSeparator();
208 m_menuSocket
->Append(CLIENT_CLOSE
, _("&Close session"), _("Close connection"));
210 m_menuDatagramSocket
= new wxMenu();
211 m_menuDatagramSocket
->Append(CLIENT_DGRAM
, _("Send Datagram"), _("Test UDP sockets"));
214 m_menuProtocols
= new wxMenu();
215 m_menuProtocols
->Append(CLIENT_TESTURL
, _("Test URL"), _("Get data from the specified URL"));
218 // Append menus to the menubar
219 m_menuBar
= new wxMenuBar();
220 m_menuBar
->Append(m_menuFile
, _("&File"));
221 m_menuBar
->Append(m_menuSocket
, _("&SocketClient"));
222 m_menuBar
->Append(m_menuDatagramSocket
, _("&DatagramSocket"));
224 m_menuBar
->Append(m_menuProtocols
, _("&Protocols"));
226 SetMenuBar(m_menuBar
);
231 #endif // wxUSE_STATUSBAR
233 // Make a textctrl for logging
234 m_text
= new wxTextCtrl(this, wxID_ANY
,
235 _("Welcome to wxSocket demo: Client\nClient ready\n"),
236 wxDefaultPosition
, wxDefaultSize
,
237 wxTE_MULTILINE
| wxTE_READONLY
);
240 m_sock
= new wxSocketClient();
242 // Setup the event handler and subscribe to most events
243 m_sock
->SetEventHandler(*this, SOCKET_ID
);
244 m_sock
->SetNotify(wxSOCKET_CONNECTION_FLAG
|
245 wxSOCKET_INPUT_FLAG
|
247 m_sock
->Notify(true);
255 // No delayed deletion here, as the frame is dying anyway
261 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
263 // true is to force the frame to close
267 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
269 wxMessageBox(_("wxSocket demo: Client\n(c) 1999 Guillermo Rodriguez Garcia\n"),
271 wxOK
| wxICON_INFORMATION
, this);
274 void MyFrame
::OnOpenConnection(wxCommandEvent
& WXUNUSED(event
))
276 OpenConnection(wxSockAddress
::IPV4
);
279 void MyFrame
::OnOpenConnectionIPv6(wxCommandEvent
& WXUNUSED(event
))
281 OpenConnection(wxSockAddress
::IPV6
);
285 void MyFrame
::OpenConnection(wxSockAddress
::Family family
)
287 wxUnusedVar(family
); // unused in !wxUSE_IPV6 case
293 if ( family
== wxSockAddress
::IPV6
)
299 m_menuSocket
->Enable(CLIENT_OPEN
, false);
301 m_menuSocket
->Enable(CLIENT_OPENIPV6
, false);
303 m_menuSocket
->Enable(CLIENT_CLOSE
, false);
305 // Ask user for server address
306 wxString hostname
= wxGetTextFromUser(
307 _("Enter the address of the wxSocket demo server:"),
311 addr
->Hostname(hostname
);
314 // Mini-tutorial for Connect() :-)
315 // ---------------------------
317 // There are two ways to use Connect(): blocking and non-blocking,
318 // depending on the value passed as the 'wait' (2nd) parameter.
320 // Connect(addr, true) will wait until the connection completes,
321 // returning true on success and false on failure. This call blocks
322 // the GUI (this might be changed in future releases to honour the
323 // wxSOCKET_BLOCK flag).
325 // Connect(addr, false) will issue a nonblocking connection request
326 // and return immediately. If the return value is true, then the
327 // connection has been already successfully established. If it is
328 // false, you must wait for the request to complete, either with
329 // WaitOnConnect() or by watching wxSOCKET_CONNECTION / LOST
330 // events (please read the documentation).
332 // WaitOnConnect() itself never blocks the GUI (this might change
333 // in the future to honour the wxSOCKET_BLOCK flag). This call will
334 // return false on timeout, or true if the connection request
335 // completes, which in turn might mean:
337 // a) That the connection was successfully established
338 // b) That the connection request failed (for example, because
339 // it was refused by the peer.
341 // Use IsConnected() to distinguish between these two.
343 // So, in a brief, you should do one of the following things:
345 // For blocking Connect:
347 // bool success = client->Connect(addr, true);
349 // For nonblocking Connect:
351 // client->Connect(addr, false);
353 // bool waitmore = true;
354 // while (! client->WaitOnConnect(seconds, millis) && waitmore )
356 // // possibly give some feedback to the user,
357 // // update waitmore if needed.
359 // bool success = client->IsConnected();
361 // And that's all :-)
363 m_text
->AppendText(_("\nTrying to connect (timeout = 10 sec) ...\n"));
364 m_sock
->Connect(*addr
, false);
365 m_sock
->WaitOnConnect(10);
367 if (m_sock
->IsConnected())
368 m_text
->AppendText(_("Succeeded ! Connection established\n"));
372 m_text
->AppendText(_("Failed ! Unable to connect\n"));
373 wxMessageBox(_("Can't connect to the specified host"), _("Alert !"));
379 void MyFrame
::OnTest1(wxCommandEvent
& WXUNUSED(event
))
381 // Disable socket menu entries (exception: Close Session)
385 m_text
->AppendText(_("\n=== Test 1 begins ===\n"));
387 // Tell the server which test we are running
388 unsigned char c
= 0xBE;
389 m_sock
->Write(&c
, 1);
391 // Send some data and read it back. We know the size of the
392 // buffer, so we can specify the exact number of bytes to be
393 // sent or received and use the wxSOCKET_WAITALL flag. Also,
394 // we have disabled menu entries which could interfere with
395 // the test, so we can safely avoid the wxSOCKET_BLOCK flag.
397 // First we send a byte with the length of the string, then
398 // we send the string itself (do NOT try to send any integral
399 // value larger than a byte "as is" across the network, or
400 // you might be in trouble! Ever heard about big and little
401 // endian computers?)
403 m_sock
->SetFlags(wxSOCKET_WAITALL
);
405 const wxChar
*buf1
= _T("Test string (less than 256 chars!)");
406 unsigned char len
= (unsigned char)((wxStrlen(buf1
) + 1)*sizeof(wxChar
));
407 wxChar
*buf2
= new wxChar
[wxStrlen(buf1
) + 1];
409 m_text
->AppendText(_("Sending a test buffer to the server ..."));
410 m_sock
->Write(&len
, 1);
411 m_sock
->Write(buf1
, len
);
412 m_text
->AppendText(m_sock
->Error() ?
_("failed !\n") : _("done\n"));
414 m_text
->AppendText(_("Receiving the buffer back from server ..."));
415 m_sock
->Read(buf2
, len
);
416 m_text
->AppendText(m_sock
->Error() ?
_("failed !\n") : _("done\n"));
418 m_text
->AppendText(_("Comparing the two buffers ..."));
419 if (memcmp(buf1
, buf2
, len
) != 0)
421 m_text
->AppendText(_("failed!\n"));
422 m_text
->AppendText(_("Test 1 failed !\n"));
426 m_text
->AppendText(_("done\n"));
427 m_text
->AppendText(_("Test 1 passed !\n"));
429 m_text
->AppendText(_("=== Test 1 ends ===\n"));
436 void MyFrame
::OnTest2(wxCommandEvent
& WXUNUSED(event
))
442 // Disable socket menu entries (exception: Close Session)
446 m_text
->AppendText(_("\n=== Test 2 begins ===\n"));
448 // Tell the server which test we are running
449 unsigned char c
= 0xCE;
450 m_sock
->Write(&c
, 1);
452 // Here we use ReadMsg and WriteMsg to send messages with
453 // a header with size information. Also, the reception is
454 // event triggered, so we test input events as well.
456 // We need to set no flags here (ReadMsg and WriteMsg are
457 // not affected by flags)
459 m_sock
->SetFlags(wxSOCKET_WAITALL
);
461 wxString s
= wxGetTextFromUser(
462 _("Enter an arbitrary string to send to the server:"),
464 _("Yes I like wxWidgets!"));
467 len
= (wxStrlen(msg1
) + 1) * sizeof(wxChar
);
468 msg2
= new wxChar
[wxStrlen(msg1
) + 1];
470 m_text
->AppendText(_("Sending the string with WriteMsg ..."));
471 m_sock
->WriteMsg(msg1
, len
);
472 m_text
->AppendText(m_sock
->Error() ?
_("failed !\n") : _("done\n"));
473 m_text
->AppendText(_("Waiting for an event (timeout = 2 sec)\n"));
475 // Wait until data available (will also return if the connection is lost)
476 m_sock
->WaitForRead(2);
478 if (m_sock
->IsData())
480 m_text
->AppendText(_("Reading the string back with ReadMsg ..."));
481 m_sock
->ReadMsg(msg2
, len
);
482 m_text
->AppendText(m_sock
->Error() ?
_("failed !\n") : _("done\n"));
483 m_text
->AppendText(_("Comparing the two buffers ..."));
484 if (memcmp(msg1
, msg2
, len
) != 0)
486 m_text
->AppendText(_("failed!\n"));
487 m_text
->AppendText(_("Test 2 failed !\n"));
491 m_text
->AppendText(_("done\n"));
492 m_text
->AppendText(_("Test 2 passed !\n"));
496 m_text
->AppendText(_("Timeout ! Test 2 failed.\n"));
498 m_text
->AppendText(_("=== Test 2 ends ===\n"));
505 void MyFrame
::OnTest3(wxCommandEvent
& WXUNUSED(event
))
511 // Disable socket menu entries (exception: Close Session)
515 m_text
->AppendText(_("\n=== Test 3 begins ===\n"));
517 // Tell the server which test we are running
518 unsigned char c
= 0xDE;
519 m_sock
->Write(&c
, 1);
521 // This test also is similar to the first one but it sends a
522 // large buffer so that wxSocket is actually forced to split
523 // it into pieces and take care of sending everything before
526 m_sock
->SetFlags(wxSOCKET_WAITALL
);
528 // Note that len is in kbytes here!
530 buf1
= new char[len
* 1024];
531 buf2
= new char[len
* 1024];
533 for (int i
= 0; i
< len
* 1024; i
++)
534 buf1
[i
] = (char)(i
% 256);
536 m_text
->AppendText(_("Sending a large buffer (32K) to the server ..."));
537 m_sock
->Write(&len
, 1);
538 m_sock
->Write(buf1
, len
* 1024);
539 m_text
->AppendText(m_sock
->Error() ?
_("failed !\n") : _("done\n"));
541 m_text
->AppendText(_("Receiving the buffer back from server ..."));
542 m_sock
->Read(buf2
, len
* 1024);
543 m_text
->AppendText(m_sock
->Error() ?
_("failed !\n") : _("done\n"));
545 m_text
->AppendText(_("Comparing the two buffers ..."));
546 if (memcmp(buf1
, buf2
, len
) != 0)
548 m_text
->AppendText(_("failed!\n"));
549 m_text
->AppendText(_("Test 3 failed !\n"));
553 m_text
->AppendText(_("done\n"));
554 m_text
->AppendText(_("Test 3 passed !\n"));
556 m_text
->AppendText(_("=== Test 3 ends ===\n"));
563 void MyFrame
::OnCloseConnection(wxCommandEvent
& WXUNUSED(event
))
569 void MyFrame
::OnDatagram(wxCommandEvent
& WXUNUSED(event
))
571 m_text
->AppendText(_("\n=== Datagram test begins ===\n"));
572 m_text
->AppendText(_("Sorry, not implemented\n"));
573 m_text
->AppendText(_("=== Datagram test ends ===\n"));
578 void MyFrame
::OnTestURL(wxCommandEvent
& WXUNUSED(event
))
580 // Note that we are creating a new socket here, so this
581 // won't mess with the client/server demo.
584 m_text
->AppendText(_("\n=== URL test begins ===\n"));
585 wxString urlname
= wxGetTextFromUser(_("Enter an URL to get"),
587 _T("http://localhost"));
591 if (url
.GetError() != wxURL_NOERR
)
593 m_text
->AppendText(_("Error: couldn't parse URL\n"));
594 m_text
->AppendText(_("=== URL test ends ===\n"));
598 // Try to get the input stream (connects to the given URL)
599 m_text
->AppendText(_("Trying to establish connection...\n"));
601 wxInputStream
*data
= url
.GetInputStream();
604 m_text
->AppendText(_("Error: couldn't read from URL\n"));
605 m_text
->AppendText(_("=== URL test ends ===\n"));
609 // Print the contents type and file size
611 s
.Printf(_("Contents type: %s\nFile size: %i\nStarting to download...\n"),
612 url
.GetProtocol().GetContentType().c_str(),
614 m_text
->AppendText(s
);
618 wxFile
fileTest(wxT("test.url"), wxFile
::write
);
619 wxFileOutputStream
sout(fileTest
);
622 m_text
->AppendText(_("Error: couldn't open file for output\n"));
623 m_text
->AppendText(_("=== URL test ends ===\n"));
628 m_text
->AppendText(_("Results written to file: test.url\n"));
629 m_text
->AppendText(_("Done.\n"));
630 m_text
->AppendText(_("=== URL test ends ===\n"));
637 void MyFrame
::OnSocketEvent(wxSocketEvent
& event
)
639 wxString s
= _("OnSocketEvent: ");
641 switch(event
.GetSocketEvent())
643 case wxSOCKET_INPUT
: s
.Append(_("wxSOCKET_INPUT\n")); break;
644 case wxSOCKET_LOST
: s
.Append(_("wxSOCKET_LOST\n")); break;
645 case wxSOCKET_CONNECTION
: s
.Append(_("wxSOCKET_CONNECTION\n")); break;
646 default : s
.Append(_("Unexpected event !\n")); break;
649 m_text
->AppendText(s
);
653 // convenience functions
655 void MyFrame
::UpdateStatusBar()
659 if (!m_sock
->IsConnected())
661 s
.Printf(_("Not connected"));
671 m_sock
->GetPeer(addr
);
672 s
.Printf(_("%s : %d"), (addr
.Hostname()).c_str(), addr
.Service());
677 #endif // wxUSE_STATUSBAR
679 m_menuSocket
->Enable(CLIENT_OPEN
, !m_sock
->IsConnected() && !m_busy
);
681 m_menuSocket
->Enable(CLIENT_OPENIPV6
, !m_sock
->IsConnected() && !m_busy
);
683 m_menuSocket
->Enable(CLIENT_TEST1
, m_sock
->IsConnected() && !m_busy
);
684 m_menuSocket
->Enable(CLIENT_TEST2
, m_sock
->IsConnected() && !m_busy
);
685 m_menuSocket
->Enable(CLIENT_TEST3
, m_sock
->IsConnected() && !m_busy
);
686 m_menuSocket
->Enable(CLIENT_CLOSE
, m_sock
->IsConnected());