]>
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 #if defined(__GNUG__) && !defined(__APPLE__)
21 #pragma implementation
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers
37 #include "wx/socket.h"
39 #include "wx/wfstream.h"
41 // --------------------------------------------------------------------------
43 // --------------------------------------------------------------------------
45 // the application icon
46 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
47 # include "mondrian.xpm"
50 // --------------------------------------------------------------------------
52 // --------------------------------------------------------------------------
54 // Define a new application type
55 class MyApp
: public wxApp
58 virtual bool OnInit();
61 // Define a new frame type: this is going to be our main frame
62 class MyFrame
: public wxFrame
68 // event handlers for File menu
69 void OnQuit(wxCommandEvent
& event
);
70 void OnAbout(wxCommandEvent
& event
);
72 // event handlers for Socket menu
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
);
80 // event handlers for Protocols menu
81 void OnTestURL(wxCommandEvent
& event
);
84 // event handlers for DatagramSocket menu (stub)
85 void OnDatagram(wxCommandEvent
& event
);
87 // socket event handler
88 void OnSocketEvent(wxSocketEvent
& event
);
90 // convenience functions
91 void UpdateStatusBar();
94 wxSocketClient
*m_sock
;
98 wxMenu
*m_menuDatagramSocket
;
99 wxMenu
*m_menuProtocols
;
100 wxMenuBar
*m_menuBar
;
103 // any class wishing to process wxWidgets events must use this macro
104 DECLARE_EVENT_TABLE()
107 // --------------------------------------------------------------------------
109 // --------------------------------------------------------------------------
111 // IDs for the controls and the menu commands
131 // --------------------------------------------------------------------------
132 // event tables and other macros for wxWidgets
133 // --------------------------------------------------------------------------
135 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
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
)
145 EVT_MENU(CLIENT_TESTURL
, MyFrame::OnTestURL
)
147 EVT_SOCKET(SOCKET_ID
, MyFrame::OnSocketEvent
)
152 // ==========================================================================
154 // ==========================================================================
156 // --------------------------------------------------------------------------
157 // the application class
158 // --------------------------------------------------------------------------
162 // Create the main application window
163 MyFrame
*frame
= new MyFrame();
165 // Show it and tell the application that it's our main window
173 // --------------------------------------------------------------------------
175 // --------------------------------------------------------------------------
178 MyFrame::MyFrame() : wxFrame((wxFrame
*)NULL
, wxID_ANY
,
179 _("wxSocket demo: Client"),
180 wxDefaultPosition
, wxSize(300, 200))
182 // Give the frame an icon
183 SetIcon(wxICON(mondrian
));
186 m_menuFile
= new wxMenu();
187 m_menuFile
->Append(CLIENT_ABOUT
, _("&About...\tCtrl-A"), _("Show about dialog"));
188 m_menuFile
->AppendSeparator();
189 m_menuFile
->Append(CLIENT_QUIT
, _("E&xit\tAlt-X"), _("Quit client"));
191 m_menuSocket
= new wxMenu();
192 m_menuSocket
->Append(CLIENT_OPEN
, _("&Open session"), _("Connect to server"));
193 m_menuSocket
->AppendSeparator();
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"));
197 m_menuSocket
->AppendSeparator();
198 m_menuSocket
->Append(CLIENT_CLOSE
, _("&Close session"), _("Close connection"));
200 m_menuDatagramSocket
= new wxMenu();
201 m_menuDatagramSocket
->Append(CLIENT_DGRAM
, _("Send Datagram"), _("Test UDP sockets"));
204 m_menuProtocols
= new wxMenu();
205 m_menuProtocols
->Append(CLIENT_TESTURL
, _("Test URL"), _("Get data from the specified URL"));
208 // Append menus to the menubar
209 m_menuBar
= new wxMenuBar();
210 m_menuBar
->Append(m_menuFile
, _("&File"));
211 m_menuBar
->Append(m_menuSocket
, _("&SocketClient"));
212 m_menuBar
->Append(m_menuDatagramSocket
, _("&DatagramSocket"));
214 m_menuBar
->Append(m_menuProtocols
, _("&Protocols"));
216 SetMenuBar(m_menuBar
);
221 #endif // wxUSE_STATUSBAR
223 // Make a textctrl for logging
224 m_text
= new wxTextCtrl(this, wxID_ANY
,
225 _("Welcome to wxSocket demo: Client\nClient ready\n"),
226 wxDefaultPosition
, wxDefaultSize
,
227 wxTE_MULTILINE
| wxTE_READONLY
);
230 m_sock
= new wxSocketClient();
232 // Setup the event handler and subscribe to most events
233 m_sock
->SetEventHandler(*this, SOCKET_ID
);
234 m_sock
->SetNotify(wxSOCKET_CONNECTION_FLAG
|
235 wxSOCKET_INPUT_FLAG
|
237 m_sock
->Notify(true);
245 // No delayed deletion here, as the frame is dying anyway
251 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
253 // true is to force the frame to close
257 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
259 wxMessageBox(_("wxSocket demo: Client\n(c) 1999 Guillermo Rodriguez Garcia\n"),
261 wxOK
| wxICON_INFORMATION
, this);
264 void MyFrame::OnOpenConnection(wxCommandEvent
& WXUNUSED(event
))
268 m_menuSocket
->Enable(CLIENT_OPEN
, false);
269 m_menuSocket
->Enable(CLIENT_CLOSE
, false);
271 // Ask user for server address
272 wxString hostname
= wxGetTextFromUser(
273 _("Enter the address of the wxSocket demo server:"),
277 addr
.Hostname(hostname
);
280 // Mini-tutorial for Connect() :-)
281 // ---------------------------
283 // There are two ways to use Connect(): blocking and non-blocking,
284 // depending on the value passed as the 'wait' (2nd) parameter.
286 // Connect(addr, true) will wait until the connection completes,
287 // returning true on success and false on failure. This call blocks
288 // the GUI (this might be changed in future releases to honour the
289 // wxSOCKET_BLOCK flag).
291 // Connect(addr, false) will issue a nonblocking connection request
292 // and return immediately. If the return value is true, then the
293 // connection has been already succesfully established. If it is
294 // false, you must wait for the request to complete, either with
295 // WaitOnConnect() or by watching wxSOCKET_CONNECTION / LOST
296 // events (please read the documentation).
298 // WaitOnConnect() itself never blocks the GUI (this might change
299 // in the future to honour the wxSOCKET_BLOCK flag). This call will
300 // return false on timeout, or true if the connection request
301 // completes, which in turn might mean:
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.
307 // Use IsConnected() to distinguish between these two.
309 // So, in a brief, you should do one of the following things:
311 // For blocking Connect:
313 // bool success = client->Connect(addr, true);
315 // For nonblocking Connect:
317 // client->Connect(addr, false);
319 // bool waitmore = true;
320 // while (! client->WaitOnConnect(seconds, millis) && waitmore )
322 // // possibly give some feedback to the user,
323 // // update waitmore if needed.
325 // bool success = client->IsConnected();
327 // And that's all :-)
329 m_text
->AppendText(_("\nTrying to connect (timeout = 10 sec) ...\n"));
330 m_sock
->Connect(addr
, false);
331 m_sock
->WaitOnConnect(10);
333 if (m_sock
->IsConnected())
334 m_text
->AppendText(_("Succeeded ! Connection established\n"));
338 m_text
->AppendText(_("Failed ! Unable to connect\n"));
339 wxMessageBox(_("Can't connect to the specified host"), _("Alert !"));
345 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
351 // Disable socket menu entries (exception: Close Session)
355 m_text
->AppendText(_("\n=== Test 1 begins ===\n"));
357 // Tell the server which test we are running
358 unsigned char c
= 0xBE;
359 m_sock
->Write(&c
, 1);
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
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.
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
369 // value larger than a byte "as is" across the network, or
370 // you might be in trouble! Ever heard about big and little
371 // endian computers?)
373 m_sock
->SetFlags(wxSOCKET_WAITALL
);
375 buf1
= _("Test string (less than 256 chars!)");
376 len
= (unsigned char)((wxStrlen(buf1
) + 1) * sizeof(wxChar
));
377 buf2
= new wxChar
[wxStrlen(buf1
) + 1];
379 m_text
->AppendText(_("Sending a test buffer to the server ..."));
380 m_sock
->Write(&len
, 1);
381 m_sock
->Write(buf1
, len
);
382 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
384 m_text
->AppendText(_("Receiving the buffer back from server ..."));
385 m_sock
->Read(buf2
, len
);
386 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
388 m_text
->AppendText(_("Comparing the two buffers ..."));
389 if (memcmp(buf1
, buf2
, len
) != 0)
391 m_text
->AppendText(_("failed!\n"));
392 m_text
->AppendText(_("Test 1 failed !\n"));
396 m_text
->AppendText(_("done\n"));
397 m_text
->AppendText(_("Test 1 passed !\n"));
399 m_text
->AppendText(_("=== Test 1 ends ===\n"));
406 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
412 // Disable socket menu entries (exception: Close Session)
416 m_text
->AppendText(_("\n=== Test 2 begins ===\n"));
418 // Tell the server which test we are running
419 unsigned char c
= 0xCE;
420 m_sock
->Write(&c
, 1);
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.
426 // We need to set no flags here (ReadMsg and WriteMsg are
427 // not affected by flags)
429 m_sock
->SetFlags(wxSOCKET_WAITALL
);
431 wxString s
= wxGetTextFromUser(
432 _("Enter an arbitrary string to send to the server:"),
434 _("Yes I like wxWidgets!"));
437 len
= (wxStrlen(msg1
) + 1) * sizeof(wxChar
);
438 msg2
= new wxChar
[wxStrlen(msg1
) + 1];
440 m_text
->AppendText(_("Sending the string with WriteMsg ..."));
441 m_sock
->WriteMsg(msg1
, len
);
442 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
443 m_text
->AppendText(_("Waiting for an event (timeout = 2 sec)\n"));
445 // Wait until data available (will also return if the connection is lost)
446 m_sock
->WaitForRead(2);
448 if (m_sock
->IsData())
450 m_text
->AppendText(_("Reading the string back with ReadMsg ..."));
451 m_sock
->ReadMsg(msg2
, len
);
452 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
453 m_text
->AppendText(_("Comparing the two buffers ..."));
454 if (memcmp(msg1
, msg2
, len
) != 0)
456 m_text
->AppendText(_("failed!\n"));
457 m_text
->AppendText(_("Test 2 failed !\n"));
461 m_text
->AppendText(_("done\n"));
462 m_text
->AppendText(_("Test 2 passed !\n"));
466 m_text
->AppendText(_("Timeout ! Test 2 failed.\n"));
468 m_text
->AppendText(_("=== Test 2 ends ===\n"));
475 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
481 // Disable socket menu entries (exception: Close Session)
485 m_text
->AppendText(_("\n=== Test 3 begins ===\n"));
487 // Tell the server which test we are running
488 unsigned char c
= 0xDE;
489 m_sock
->Write(&c
, 1);
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
496 m_sock
->SetFlags(wxSOCKET_WAITALL
);
498 // Note that len is in kbytes here!
500 buf1
= new char[len
* 1024];
501 buf2
= new char[len
* 1024];
503 for (int i
= 0; i
< len
* 1024; i
++)
504 buf1
[i
] = (char)(i
% 256);
506 m_text
->AppendText(_("Sending a large buffer (32K) to the server ..."));
507 m_sock
->Write(&len
, 1);
508 m_sock
->Write(buf1
, len
* 1024);
509 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
511 m_text
->AppendText(_("Receiving the buffer back from server ..."));
512 m_sock
->Read(buf2
, len
* 1024);
513 m_text
->AppendText(m_sock
->Error() ? _("failed !\n") : _("done\n"));
515 m_text
->AppendText(_("Comparing the two buffers ..."));
516 if (memcmp(buf1
, buf2
, len
) != 0)
518 m_text
->AppendText(_("failed!\n"));
519 m_text
->AppendText(_("Test 3 failed !\n"));
523 m_text
->AppendText(_("done\n"));
524 m_text
->AppendText(_("Test 3 passed !\n"));
526 m_text
->AppendText(_("=== Test 3 ends ===\n"));
533 void MyFrame::OnCloseConnection(wxCommandEvent
& WXUNUSED(event
))
539 void MyFrame::OnDatagram(wxCommandEvent
& WXUNUSED(event
))
541 m_text
->AppendText(_("\n=== Datagram test begins ===\n"));
542 m_text
->AppendText(_("Sorry, not implemented\n"));
543 m_text
->AppendText(_("=== Datagram test ends ===\n"));
548 void MyFrame::OnTestURL(wxCommandEvent
& WXUNUSED(event
))
550 // Note that we are creating a new socket here, so this
551 // won't mess with the client/server demo.
554 m_text
->AppendText(_("\n=== URL test begins ===\n"));
555 wxString urlname
= wxGetTextFromUser(_("Enter an URL to get"),
557 _T("http://localhost"));
561 if (url
.GetError() != wxURL_NOERR
)
563 m_text
->AppendText(_("Error: couldn't parse URL\n"));
564 m_text
->AppendText(_("=== URL test ends ===\n"));
568 // Try to get the input stream (connects to the given URL)
569 m_text
->AppendText(_("Trying to establish connection...\n"));
571 wxInputStream
*data
= url
.GetInputStream();
574 m_text
->AppendText(_("Error: couldn't read from URL\n"));
575 m_text
->AppendText(_("=== URL test ends ===\n"));
579 // Print the contents type and file size
581 s
.Printf(_("Contents type: %s\nFile size: %i\nStarting to download...\n"),
582 url
.GetProtocol().GetContentType().c_str(),
584 m_text
->AppendText(s
);
588 wxFile
fileTest(wxT("test.url"));
589 wxFileOutputStream
sout(fileTest
);
592 m_text
->AppendText(_("Error: couldn't open file for output\n"));
593 m_text
->AppendText(_("=== URL test ends ===\n"));
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"));
607 void MyFrame::OnSocketEvent(wxSocketEvent
& event
)
609 wxString s
= _("OnSocketEvent: ");
611 switch(event
.GetSocketEvent())
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;
619 m_text
->AppendText(s
);
623 // convenience functions
625 void MyFrame::UpdateStatusBar()
629 if (!m_sock
->IsConnected())
631 s
.Printf(_("Not connected"));
637 m_sock
->GetPeer(addr
);
638 s
.Printf(_("%s : %d"), (addr
.Hostname()).c_str(), addr
.Service());
643 #endif // wxUSE_STATUSBAR
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());