]>
git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/client.cpp
3 * Purpose: wxSocket: client demo
4 * Author: LAVAUX Guilhem
7 * Copyright: (c) 1997, LAVAUX Guilhem
11 #pragma implementation
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
26 #include "wx/wfstream.h"
27 #include "wx/socket.h"
29 #include "wx/protocol/http.h"
30 #include "wx/thread.h"
31 #include "wx/progdlg.h"
33 #if defined(__WXMOTIF__) || defined(__WXGTK__)
34 #include "mondrian.xpm"
37 // Define a new application type
38 class MyApp
: public wxApp
40 virtual bool OnInit(void);
45 // Define a new frame type
46 class MyFrame
: public wxFrame
48 DECLARE_CLASS(MyFrame
)
55 void OnCloseTest(wxCommandEvent
& evt
);
56 void OnExecTest1(wxCommandEvent
& evt
);
57 void OnExecUrlTest(wxCommandEvent
& evt
);
58 void OnQuitApp(wxCommandEvent
& evt
);
59 void OnExecOpenConnection(wxCommandEvent
& evt
);
60 void OnExecCloseConnection(wxCommandEvent
& evt
);
61 void OnSocketEvent(wxSocketEvent
& evt
);
64 void Download(wxInputStream
*input
);
70 IMPLEMENT_CLASS(MyFrame
, wxFrame
)
73 * Define a new derived SocketClient
75 class MyClient
: public wxSocketClient
80 void OnNotify(GSocketEventFlags
WXUNUSED(flags
)) { frame
->UpdateStatus(); }
83 // ID for the menu quit command
84 const int SKDEMO_QUIT
= 101;
85 const int SKDEMO_CONNECT
= 102;
86 const int SKDEMO_TEST1
= 103;
87 const int SKDEMO_TEST2
= 104;
88 const int SKDEMO_CLOSE
= 105;
89 const int SKDEMO_TEST3
= 106;
90 const int ID_TEST_CLOSE
= 107;
91 const int SKDEMO_SCK
= 108;
96 * `Main program' equivalent, creating windows and returning main app frame
98 bool MyApp::OnInit(void)
100 // Create the main frame window
101 MyFrame
*frame
= new MyFrame();
104 frame
->SetIcon(wxICON(mondrian
));
107 wxMenu
*file_menu
= new wxMenu();
109 file_menu
->Append(SKDEMO_QUIT
, "Exit");
110 wxMenuBar
*menu_bar
= new wxMenuBar
;
111 menu_bar
->Append(file_menu
, "File");
113 wxMenu
*socket_menu
= new wxMenu();
114 socket_menu
->Append(SKDEMO_CONNECT
, "Open session");
115 socket_menu
->AppendSeparator();
116 socket_menu
->Append(SKDEMO_TEST1
, "Start test 1");
117 socket_menu
->AppendSeparator();
118 socket_menu
->Append(SKDEMO_CLOSE
, "Close session");
119 socket_menu
->AppendSeparator();
120 socket_menu
->Append(SKDEMO_TEST3
, "Start URL test");
122 menu_bar
->Append(socket_menu
, "Socket");
124 frame
->SetMenuBar(menu_bar
);
126 // Make a panel with a message
127 (void)new wxPanel(frame
, -1, wxPoint(0, 0), wxSize(300, 100));
132 // Return the main frame window
137 * MyFrame Constructor
140 wxFrame(NULL
, -1, "wxSocket client demo",
141 wxDefaultPosition
, wxSize(300, 200), wxDEFAULT_FRAME_STYLE
)
143 sock
= new MyClient();
144 sock
->SetFlags((wxSocketBase::wxSockFlags
) (wxSocketBase::WAITALL
| wxSocketBase::SPEED
));
146 sock
->SetNotify(wxSOCKET_LOST_FLAG
);
156 void MyFrame::OnQuitApp(wxCommandEvent
& WXUNUSED(evt
))
161 void MyFrame::OnExecOpenConnection(wxCommandEvent
& WXUNUSED(evt
))
165 if (sock
->IsConnected())
168 wxString hname
= wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
169 "Connect ...", "localhost");
170 addr
.Hostname(hname
);
173 sock
->Connect(addr
, TRUE
);
174 sock
->SetFlags(wxSocketBase::NONE
);
175 if (!sock
->IsConnected())
176 wxMessageBox("Can't connect to the specified host", "Alert !");
181 void MyFrame::OnExecCloseConnection(wxCommandEvent
& WXUNUSED(evt
))
187 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
188 EVT_BUTTON(ID_TEST_CLOSE
, MyFrame::OnCloseTest
)
189 EVT_MENU(SKDEMO_TEST1
, MyFrame::OnExecTest1
)
190 EVT_MENU(SKDEMO_TEST3
, MyFrame::OnExecUrlTest
)
191 EVT_MENU(SKDEMO_QUIT
, MyFrame::OnQuitApp
)
192 EVT_MENU(SKDEMO_CONNECT
, MyFrame::OnExecOpenConnection
)
193 EVT_MENU(SKDEMO_CLOSE
, MyFrame::OnExecCloseConnection
)
194 EVT_SOCKET(SKDEMO_SCK
, MyFrame::OnSocketEvent
)
197 class MyFrameSocketTimer
: public wxTimer
{
206 void MyFrame::OnSocketEvent(wxSocketEvent
& evt
)
211 void MyFrame::OnCloseTest(wxCommandEvent
& evt
)
213 wxButton
*button
= (wxButton
*)evt
.GetEventObject();
214 wxDialog
*dlg
= (wxDialog
*)button
->GetParent();
219 void MyFrame::UpdateStatus()
221 if (!sock
->IsConnected()) {
222 SetStatusText("Not connected", 0);
223 SetStatusText("", 1);
229 wxSprintf(s
, _T("Connected to %s"), WXSTRINGCAST addr
.Hostname());
231 wxSprintf(s
, _T("Service: %d"), addr
.Service());
236 void MyFrame::OnExecTest1(wxCommandEvent
& WXUNUSED(evt
))
238 if (!sock
->IsConnected())
241 wxDialog
*dlgbox
= new wxDialog(this, -1, "Test 1", wxDefaultPosition
, wxSize(414, 250));
242 wxTextCtrl
*text_win
= new wxTextCtrl(dlgbox
, -1, "",
243 wxPoint(0, 0), wxSize(400, 200),
245 (void)new wxButton(dlgbox
, ID_TEST_CLOSE
, "Close",
246 wxPoint(100, 210), wxSize(100, -1));
252 text_win
->WriteText("Initializing test 1 ...\n");
257 buf
= copystring(_T("Hi ! Hi ! Hi !\n"));
258 buf2
= new wxChar
[wxStrlen(buf
)+1];
263 text_win
->WriteText("Sending some byte to the server ...");
265 sock
->Write((char *)buf
, wxStrlen(buf
)+1);
266 text_win
->WriteText("done\n");
268 text_win
->WriteText("Receiving some byte from the server ...");
270 sock
->Read((char *)buf2
, wxStrlen(buf
)+1);
271 text_win
->WriteText("done\n");
274 text_win
->WriteText("Comparing the two buffers ...");
275 if (memcmp(buf
, buf2
, wxStrlen(buf
)+1) != 0) {
276 text_win
->WriteText("Fail\n");
280 text_win
->WriteText("done\nTest 1A passed !\n");
283 sock
->SetEventHandler(*this, SKDEMO_SCK
);
284 sock
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
287 text_win
->WriteText("Test 1B: sending bytes to the server\n");
289 sock
->Write((char *)buf
, wxStrlen(buf
)+1);
290 text_win
->WriteText("Waiting for incoming bytes (timeout = 2 sec) ...");
295 MyFrameSocketTimer timer
;
297 timer
.m_var
= &m_good
;
298 timer
.Start(2000, TRUE
);
304 text_win
->WriteText("Timeout ! Failed.\n");
308 text_win
->WriteText("Success.");
310 sock
->Read((char *)buf2
, wxStrlen(buf
)+1);
322 void MyFrame::Download(wxInputStream
*input
)
324 wxProgressDialog
progress("Downloading ...", "0% downloaded");
325 wxFileOutputStream
f_out("test.url");
327 int BUFSIZE
, bytes_read
;
334 if (input
->GetSize() == (size_t)-1) {
335 file_size
= (size_t)-1;
336 bytes_read
= BUFSIZE
= 10240;
338 file_size
= input
->GetSize();
339 if (file_size
> 10240)
340 bytes_read
= BUFSIZE
= file_size
/ 1024;
342 bytes_read
= BUFSIZE
= 1024;
344 buf
= new char[BUFSIZE
];
347 bytes_read
= BUFSIZE
;
348 while (downloaded
< file_size
&& bytes_read
!= 0) {
349 bytes_read
= input
->Read(buf
, BUFSIZE
).LastRead();
350 f_out
.Write(buf
, bytes_read
);
351 downloaded
+= bytes_read
;
353 percents
= downloaded
* 100 / file_size
;
356 message
<< percents
<< _T("% downloaded");
357 progress
.Update(percents
, message
);
363 void MyFrame::OnExecUrlTest(wxCommandEvent
& WXUNUSED(evt
))
365 wxString urlname
= wxGetTextFromUser("Enter an URL to get",
366 "URL:", "http://localhost");
369 wxInputStream
*datas
= url
.GetInputStream();
373 error
.Printf(_T("Error in getting data from the URL. (error = %d)"), url
.GetError());
374 wxMessageBox(error
, "Alert !");