]>
git.saurik.com Git - wxWidgets.git/blob - samples/sockets/client.cpp
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(int family
= AF_INET
);
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(AF_INET
);
279 void MyFrame::OnOpenConnectionIPv6(wxCommandEvent
& WXUNUSED(event
))
281 OpenConnection(AF_INET6
);
285 void MyFrame::OpenConnection(int family
)
291 if ( family
==AF_INET6
)
300 m_menuSocket
->Enable(CLIENT_OPEN
, false);
302 m_menuSocket
->Enable(CLIENT_OPENIPV6
, false);
304 m_menuSocket
->Enable(CLIENT_CLOSE
, false);
306 // Ask user for server address
307 wxString hostname
= wxGetTextFromUser(
308 _("Enter the address of the wxSocket demo server:"),
312 addr
->Hostname(hostname
);
315 // Mini-tutorial for Connect() :-)
316 // ---------------------------
318 // There are two ways to use Connect(): blocking and non-blocking,
319 // depending on the value passed as the 'wait' (2nd) parameter.
321 // Connect(addr, true) will wait until the connection completes,
322 // returning true on success and false on failure. This call blocks
323 // the GUI (this might be changed in future releases to honour the
324 // wxSOCKET_BLOCK flag).
326 // Connect(addr, false) will issue a nonblocking connection request
327 // and return immediately. If the return value is true, then the
328 // connection has been already successfully established. If it is
329 // false, you must wait for the request to complete, either with
330 // WaitOnConnect() or by watching wxSOCKET_CONNECTION / LOST
331 // events (please read the documentation).
333 // WaitOnConnect() itself never blocks the GUI (this might change
334 // in the future to honour the wxSOCKET_BLOCK flag). This call will
335 // return false on timeout, or true if the connection request
336 // completes, which in turn might mean:
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.
342 // Use IsConnected() to distinguish between these two.
344 // So, in a brief, you should do one of the following things:
346 // For blocking Connect:
348 // bool success = client->Connect(addr, true);
350 // For nonblocking Connect:
352 // client->Connect(addr, false);
354 // bool waitmore = true;
355 // while (! client->WaitOnConnect(seconds, millis) && waitmore )
357 // // possibly give some feedback to the user,
358 // // update waitmore if needed.
360 // bool success = client->IsConnected();
362 // And that's all :-)
364 m_text
->AppendText(_("\nTrying to connect (timeout = 10 sec) ...\n"));
365 m_sock
->Connect(*addr
, false);
366 m_sock
->WaitOnConnect(10);
368 if (m_sock
->IsConnected())
369 m_text
->AppendText(_("Succeeded ! Connection established\n"));
373 m_text
->AppendText(_("Failed ! Unable to connect\n"));
374 wxMessageBox(_("Can't connect to the specified host"), _("Alert !"));
380 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
382 // Disable socket menu entries (exception: Close Session)
386 m_text
->AppendText(_("\n=== Test 1 begins ===\n"));
388 // Tell the server which test we are running
389 unsigned char c
= 0xBE;
390 m_sock
->Write(&c
, 1);
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
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.
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
400 // value larger than a byte "as is" across the network, or
401 // you might be in trouble! Ever heard about big and little
402 // endian computers?)
404 m_sock
->SetFlags(wxSOCKET_WAITALL
);
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];
410 m_text
->AppendText(_("Sending a test buffer to the server ..."));
411 m_sock
->Write(&len
, 1);
412 m_sock
->Write(buf1
, len
);
413 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
415 m_text
->AppendText(_("Receiving the buffer back from server ..."));
416 m_sock
->Read(buf2
, len
);
417 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
419 m_text
->AppendText(_("Comparing the two buffers ..."));
420 if (memcmp(buf1
, buf2
, len
) != 0)
422 m_text
->AppendText(_("failed!\n"));
423 m_text
->AppendText(_("Test 1 failed !\n"));
427 m_text
->AppendText(_("done\n"));
428 m_text
->AppendText(_("Test 1 passed !\n"));
430 m_text
->AppendText(_("=== Test 1 ends ===\n"));
437 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
443 // Disable socket menu entries (exception: Close Session)
447 m_text
->AppendText(_("\n=== Test 2 begins ===\n"));
449 // Tell the server which test we are running
450 unsigned char c
= 0xCE;
451 m_sock
->Write(&c
, 1);
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.
457 // We need to set no flags here (ReadMsg and WriteMsg are
458 // not affected by flags)
460 m_sock
->SetFlags(wxSOCKET_WAITALL
);
462 wxString s
= wxGetTextFromUser(
463 _("Enter an arbitrary string to send to the server:"),
465 _("Yes I like wxWidgets!"));
468 len
= (wxStrlen(msg1
) + 1) * sizeof(wxChar
);
469 msg2
= new wxChar
[wxStrlen(msg1
) + 1];
471 m_text
->AppendText(_("Sending the string with WriteMsg ..."));
472 m_sock
->WriteMsg(msg1
, len
);
473 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
474 m_text
->AppendText(_("Waiting for an event (timeout = 2 sec)\n"));
476 // Wait until data available (will also return if the connection is lost)
477 m_sock
->WaitForRead(2);
479 if (m_sock
->IsData())
481 m_text
->AppendText(_("Reading the string back with ReadMsg ..."));
482 m_sock
->ReadMsg(msg2
, len
);
483 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
484 m_text
->AppendText(_("Comparing the two buffers ..."));
485 if (memcmp(msg1
, msg2
, len
) != 0)
487 m_text
->AppendText(_("failed!\n"));
488 m_text
->AppendText(_("Test 2 failed !\n"));
492 m_text
->AppendText(_("done\n"));
493 m_text
->AppendText(_("Test 2 passed !\n"));
497 m_text
->AppendText(_("Timeout ! Test 2 failed.\n"));
499 m_text
->AppendText(_("=== Test 2 ends ===\n"));
506 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
512 // Disable socket menu entries (exception: Close Session)
516 m_text
->AppendText(_("\n=== Test 3 begins ===\n"));
518 // Tell the server which test we are running
519 unsigned char c
= 0xDE;
520 m_sock
->Write(&c
, 1);
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
527 m_sock
->SetFlags(wxSOCKET_WAITALL
);
529 // Note that len is in kbytes here!
531 buf1
= new char[len
* 1024];
532 buf2
= new char[len
* 1024];
534 for (int i
= 0; i
< len
* 1024; i
++)
535 buf1
[i
] = (char)(i
% 256);
537 m_text
->AppendText(_("Sending a large buffer (32K) to the server ..."));
538 m_sock
->Write(&len
, 1);
539 m_sock
->Write(buf1
, len
* 1024);
540 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
542 m_text
->AppendText(_("Receiving the buffer back from server ..."));
543 m_sock
->Read(buf2
, len
* 1024);
544 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
546 m_text
->AppendText(_("Comparing the two buffers ..."));
547 if (memcmp(buf1
, buf2
, len
) != 0)
549 m_text
->AppendText(_("failed!\n"));
550 m_text
->AppendText(_("Test 3 failed !\n"));
554 m_text
->AppendText(_("done\n"));
555 m_text
->AppendText(_("Test 3 passed !\n"));
557 m_text
->AppendText(_("=== Test 3 ends ===\n"));
564 void MyFrame::OnCloseConnection(wxCommandEvent
& WXUNUSED(event
))
570 void MyFrame::OnDatagram(wxCommandEvent
& WXUNUSED(event
))
572 m_text
->AppendText(_("\n=== Datagram test begins ===\n"));
573 m_text
->AppendText(_("Sorry, not implemented\n"));
574 m_text
->AppendText(_("=== Datagram test ends ===\n"));
579 void MyFrame::OnTestURL(wxCommandEvent
& WXUNUSED(event
))
581 // Note that we are creating a new socket here, so this
582 // won't mess with the client/server demo.
585 m_text
->AppendText(_("\n=== URL test begins ===\n"));
586 wxString urlname
= wxGetTextFromUser(_("Enter an URL to get"),
588 _T("http://localhost"));
592 if (url
.GetError() != wxURL_NOERR
)
594 m_text
->AppendText(_("Error: couldn't parse URL\n"));
595 m_text
->AppendText(_("=== URL test ends ===\n"));
599 // Try to get the input stream (connects to the given URL)
600 m_text
->AppendText(_("Trying to establish connection...\n"));
602 wxInputStream
*data
= url
.GetInputStream();
605 m_text
->AppendText(_("Error: couldn't read from URL\n"));
606 m_text
->AppendText(_("=== URL test ends ===\n"));
610 // Print the contents type and file size
612 s
.Printf(_("Contents type: %s\nFile size: %i\nStarting to download...\n"),
613 url
.GetProtocol().GetContentType().c_str(),
615 m_text
->AppendText(s
);
619 wxFile
fileTest(wxT("test.url"), wxFile::write
);
620 wxFileOutputStream
sout(fileTest
);
623 m_text
->AppendText(_("Error: couldn't open file for output\n"));
624 m_text
->AppendText(_("=== URL test ends ===\n"));
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"));
638 void MyFrame::OnSocketEvent(wxSocketEvent
& event
)
640 wxString s
= _("OnSocketEvent: ");
642 switch(event
.GetSocketEvent())
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;
650 m_text
->AppendText(s
);
654 // convenience functions
656 void MyFrame::UpdateStatusBar()
660 if (!m_sock
->IsConnected())
662 s
.Printf(_("Not connected"));
672 m_sock
->GetPeer(addr
);
673 s
.Printf(_("%s : %d"), (addr
.Hostname()).c_str(), addr
.Service());
678 #endif // wxUSE_STATUSBAR
680 m_menuSocket
->Enable(CLIENT_OPEN
, !m_sock
->IsConnected() && !m_busy
);
682 m_menuSocket
->Enable(CLIENT_OPENIPV6
, !m_sock
->IsConnected() && !m_busy
);
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());