]>
git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/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 // --------------------------------------------------------------------------
21 # pragma implementation "client.cpp"
22 # pragma interface "client.cpp"
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/protocol/http.h"
40 # include "wx/progdlg.h"
42 // --------------------------------------------------------------------------
44 // --------------------------------------------------------------------------
46 // the application icon
47 #if defined(__WXGTK__) || defined(__WXMOTIF__)
48 # include "mondrian.xpm"
51 // --------------------------------------------------------------------------
53 // --------------------------------------------------------------------------
55 // Define a new application type
56 class MyApp
: public wxApp
59 virtual bool OnInit();
62 // Define a new frame type: this is going to be our main frame
63 class MyFrame
: public wxFrame
69 // event handlers (these functions should _not_ be virtual)
70 void OnQuit(wxCommandEvent
& event
);
71 void OnAbout(wxCommandEvent
& event
);
72 void OnOpenConnection(wxCommandEvent
& event
);
73 void OnTest1(wxCommandEvent
& event
);
74 void OnTest2(wxCommandEvent
& event
);
75 void OnTest3(wxCommandEvent
& event
);
76 void OnCloseConnection(wxCommandEvent
& event
);
77 void OnSocketEvent(wxSocketEvent
& event
);
79 // convenience functions
80 void UpdateStatusBar();
83 wxSocketClient
*m_sock
;
91 // any class wishing to process wxWindows events must use this macro
95 // --------------------------------------------------------------------------
97 // --------------------------------------------------------------------------
99 // IDs for the controls and the menu commands
115 // --------------------------------------------------------------------------
116 // event tables and other macros for wxWindows
117 // --------------------------------------------------------------------------
119 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
120 EVT_MENU(CLIENT_QUIT
, MyFrame::OnQuit
)
121 EVT_MENU(CLIENT_ABOUT
, MyFrame::OnAbout
)
122 EVT_MENU(CLIENT_OPEN
, MyFrame::OnOpenConnection
)
123 EVT_MENU(CLIENT_TEST1
, MyFrame::OnTest1
)
124 EVT_MENU(CLIENT_TEST2
, MyFrame::OnTest2
)
125 EVT_MENU(CLIENT_TEST3
, MyFrame::OnTest3
)
126 EVT_MENU(CLIENT_CLOSE
, MyFrame::OnCloseConnection
)
127 EVT_SOCKET(SOCKET_ID
, MyFrame::OnSocketEvent
)
132 // ==========================================================================
134 // ==========================================================================
136 // --------------------------------------------------------------------------
137 // the application class
138 // --------------------------------------------------------------------------
142 // Create the main application window
143 MyFrame
*frame
= new MyFrame();
145 // Show it and tell the application that it's our main window
153 // --------------------------------------------------------------------------
155 // --------------------------------------------------------------------------
158 MyFrame::MyFrame() : wxFrame((wxFrame
*)NULL
, -1,
159 _T("wxSocket demo: Client"),
160 wxDefaultPosition
, wxSize(300, 200))
162 // Give the frame an icon
163 SetIcon(wxICON(mondrian
));
166 m_menuFile
= new wxMenu();
167 m_menuFile
->Append(CLIENT_ABOUT
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
168 m_menuFile
->AppendSeparator();
169 m_menuFile
->Append(CLIENT_QUIT
, _T("E&xit\tAlt-X"), _T("Quit client"));
171 m_menuSocket
= new wxMenu();
172 m_menuSocket
->Append(CLIENT_OPEN
, _T("&Open session"), _T("Connect to server"));
173 m_menuSocket
->AppendSeparator();
174 m_menuSocket
->Append(CLIENT_TEST1
, _T("Test &1"), _T("Test basic functionality"));
175 m_menuSocket
->Append(CLIENT_TEST2
, _T("Test &2"), _T("Test ReadMsg and WriteMsg"));
176 m_menuSocket
->Append(CLIENT_TEST3
, _T("Test &3"), _T("Test large data transfer"));
177 m_menuSocket
->AppendSeparator();
178 m_menuSocket
->Append(CLIENT_CLOSE
, _T("&Close session"), _T("Close connection"));
180 // Append menus to the menubar
181 m_menuBar
= new wxMenuBar();
182 m_menuBar
->Append(m_menuFile
, _T("&File"));
183 m_menuBar
->Append(m_menuSocket
, _T("&Socket"));
184 SetMenuBar(m_menuBar
);
189 // Make a panel with a textctrl in it
190 m_panel
= new wxPanel(this, -1, wxPoint(0, 0), GetClientSize());
191 m_text
= new wxTextCtrl(m_panel
, -1,
192 _T("Welcome to wxSocket demo: Client\n")
193 _T("Client ready\n\n"),
194 wxPoint(0, 0), m_panel
->GetClientSize(),
195 wxTE_MULTILINE
| wxTE_READONLY
);
198 m_sock
= new wxSocketClient();
199 m_sock
->SetEventHandler(*this, SOCKET_ID
);
200 m_sock
->SetNotify(wxSOCKET_CONNECTION_FLAG
|
201 wxSOCKET_INPUT_FLAG
|
203 m_sock
->Notify(TRUE
);
216 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
218 // TRUE is to force the frame to close
222 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
224 wxMessageBox(_T("wxSocket demo: Client\n")
225 _T("(c) 1999 Guillermo Rodriguez Garcia\n"),
227 wxOK
| wxICON_INFORMATION
, this);
230 void MyFrame::OnOpenConnection(wxCommandEvent
& WXUNUSED(event
))
234 m_menuSocket
->Enable(CLIENT_OPEN
, FALSE
);
235 m_menuSocket
->Enable(CLIENT_CLOSE
, FALSE
);
237 // Ask server address
238 wxString hostname
= wxGetTextFromUser(
239 _T("Enter the address of the wxSocket demo server:"),
243 addr
.Hostname(hostname
);
246 // Non-blocking connect
247 m_text
->AppendText(_T("Trying to connect (timeout = 10 sec) ...\n"));
248 m_sock
->Connect(addr
, FALSE
);
249 m_sock
->WaitOnConnect(10);
251 if (m_sock
->IsConnected())
252 m_text
->AppendText(_T("Succeeded ! Connection established\n"));
256 m_text
->AppendText(_T("Failed ! Unable to connect\n"));
257 wxMessageBox(_T("Can't connect to the specified host"), _T("Alert !"));
263 void MyFrame::OnTest1(wxCommandEvent
& WXUNUSED(event
))
268 // Disable socket menu entries (exception: Close Session)
272 m_text
->AppendText(_T("\n=== Test 1 begins ===\n"));
274 // Tell the server which test we are running
276 m_sock
->Write(&c
, 1);
278 // Send some data and read it back. We know the size of the
279 // buffer, so we can specify the exact number of bytes to be
280 // sent or received and use the WAITALL flag. Also, we have
281 // disabled menu entries which could interfere with the test,
282 // so we can safely avoid the BLOCK (formerly SPEED) flag.
284 // First we send a byte with the length of the string, then
285 // we send the string itself (do NOT try to send any integral
286 // value larger than a byte "as is" acrosss the network, or
287 // you might be in trouble! Ever heard about big and little
288 // endian computers?)
290 m_sock
->SetFlags(wxSOCKET_WAITALL
);
292 buf1
= _T("Test string (less than 127 chars!)");
293 len
= wxStrlen(buf1
) + 1;
294 buf2
= new char[len
];
296 m_text
->AppendText(_T("Sending a test buffer to the server ..."));
297 m_sock
->Write(&len
, 1);
298 m_sock
->Write(buf1
, len
);
299 m_text
->AppendText(m_sock
->Error() ? _T("failed !\n") : _T("done\n"));
301 m_text
->AppendText(_T("Receiving the buffer back from server ..."));
302 m_sock
->Read(buf2
, len
);
303 m_text
->AppendText(m_sock
->Error() ? _T("failed !\n") : _T("done\n"));
305 m_text
->AppendText(_T("Comparing the two buffers ..."));
306 if (memcmp(buf1
, buf2
, len
) != 0)
308 m_text
->AppendText(_T("failed!\n"));
309 m_text
->AppendText(_T("Test 1 failed !\n"));
313 m_text
->AppendText(_T("done\n"));
314 m_text
->AppendText(_T("Test 1 passed !\n"));
316 m_text
->AppendText(_T("=== Test 1 ends ===\n"));
323 void MyFrame::OnTest2(wxCommandEvent
& WXUNUSED(event
))
329 // Disable socket menu entries (exception: Close Session)
333 m_text
->AppendText(_T("\n=== Test 2 begins ===\n"));
335 // Tell the server which test we are running
337 m_sock
->Write(&c
, 1);
339 // Here we use ReadMsg and WriteMsg to send messages with
340 // a header with size information. Also, the reception is
341 // event triggered, so we test input events as well.
343 // We need to set no flags here (ReadMsg and WriteMsg are
344 // not affected by flags)
346 m_sock
->SetFlags(wxSOCKET_WAITALL
);
348 wxString s
= wxGetTextFromUser(
349 _T("Enter an arbitrary string to send to the server:"),
351 _T("Yes I like wxWindows!"));
353 msg1
= (char *)s
.c_str();
354 len
= wxStrlen(msg1
) + 1;
355 msg2
= (char *)malloc(len
);
357 m_text
->AppendText(_T("Sending the string with WriteMsg ..."));
358 m_sock
->WriteMsg(msg1
, len
);
359 m_text
->AppendText(m_sock
->Error() ? _T("failed !\n") : _T("done\n"));
360 m_text
->AppendText(_T("Waiting for an event (timeout = 2 sec)\n"));
362 // Wait until data available (will also return if the connection is lost)
363 m_sock
->WaitForRead(2);
365 if (m_sock
->IsData())
367 m_text
->AppendText(_T("Reading the string back with ReadMsg ..."));
368 m_sock
->ReadMsg(msg2
, len
);
369 m_text
->AppendText(m_sock
->Error() ? _T("failed !\n") : _T("done\n"));
370 m_text
->AppendText(_T("Comparing the two buffers ..."));
371 if (memcmp(msg1
, msg2
, len
) != 0)
373 m_text
->AppendText(_T("failed!\n"));
374 m_text
->AppendText(_T("Test 2 failed !\n"));
378 m_text
->AppendText(_T("done\n"));
379 m_text
->AppendText(_T("Test 2 passed !\n"));
383 m_text
->AppendText(_T("Timeout ! Test 2 failed.\n"));
385 m_text
->AppendText(_T("=== Test 2 ends ===\n"));
392 void MyFrame::OnTest3(wxCommandEvent
& WXUNUSED(event
))
394 m_text
->AppendText(_T("\n=== Test 3 begins ===\n"));
395 m_text
->AppendText(_T("Test 3 not implemented\n"));
396 m_text
->AppendText(_T("=== Test 3 ends ===\n"));
399 void MyFrame::OnCloseConnection(wxCommandEvent
& WXUNUSED(event
))
405 void MyFrame::OnSocketEvent(wxSocketEvent
& event
)
407 wxString s
= _T("OnSocketEvent: ");
409 switch(event
.SocketEvent())
411 case wxSOCKET_INPUT
: s
.Append(_T("wxSOCKET_INPUT\n")); break;
412 case wxSOCKET_LOST
: s
.Append(_T("wxSOCKET_LOST\n")); break;
413 case wxSOCKET_CONNECTION
: s
.Append(_T("wxSOCKET_CONNECTION\n")); break;
414 default : s
.Append(_T("Unexpected event !\n")); break;
417 m_text
->AppendText(s
);
421 // convenience functions
423 void MyFrame::UpdateStatusBar()
427 if (!m_sock
->IsConnected())
429 s
.Printf(_T("Not connected"));
435 m_sock
->GetPeer(addr
);
436 s
.Printf(_T("%s : %d"), (addr
.Hostname()).c_str(), addr
.Service());
441 m_menuSocket
->Enable(CLIENT_OPEN
, !m_sock
->IsConnected() && !m_busy
);
442 m_menuSocket
->Enable(CLIENT_TEST1
, m_sock
->IsConnected() && !m_busy
);
443 m_menuSocket
->Enable(CLIENT_TEST2
, m_sock
->IsConnected() && !m_busy
);
444 m_menuSocket
->Enable(CLIENT_TEST3
, m_sock
->IsConnected() && !m_busy
);
445 m_menuSocket
->Enable(CLIENT_CLOSE
, m_sock
->IsConnected());