]>
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
)
54 void OnCloseTest(wxCommandEvent
& evt
);
55 void OnExecTest1(wxCommandEvent
& evt
);
56 void OnExecUrlTest(wxCommandEvent
& evt
);
57 void OnQuitApp(wxCommandEvent
& evt
);
58 void OnExecOpenConnection(wxCommandEvent
& evt
);
59 void OnExecCloseConnection(wxCommandEvent
& evt
);
62 void Download(wxInputStream
*input
);
68 IMPLEMENT_CLASS(MyFrame
, wxFrame
)
71 * Define a new derived SocketClient
73 class MyClient
: public wxSocketClient
78 void OnNotify(GSocketEventFlags
WXUNUSED(flags
)) { frame
->UpdateStatus(); }
81 // ID for the menu quit command
82 const int SKDEMO_QUIT
= 101;
83 const int SKDEMO_CONNECT
= 102;
84 const int SKDEMO_TEST1
= 103;
85 const int SKDEMO_TEST2
= 104;
86 const int SKDEMO_CLOSE
= 105;
87 const int SKDEMO_TEST3
= 106;
88 const int ID_TEST_CLOSE
= 107;
93 * `Main program' equivalent, creating windows and returning main app frame
95 bool MyApp::OnInit(void)
97 // Create the main frame window
98 MyFrame
*frame
= new MyFrame();
101 frame
->SetIcon(wxICON(mondrian
));
104 wxMenu
*file_menu
= new wxMenu();
106 file_menu
->Append(SKDEMO_QUIT
, "Exit");
107 wxMenuBar
*menu_bar
= new wxMenuBar
;
108 menu_bar
->Append(file_menu
, "File");
110 wxMenu
*socket_menu
= new wxMenu();
111 socket_menu
->Append(SKDEMO_CONNECT
, "Open session");
112 socket_menu
->AppendSeparator();
113 socket_menu
->Append(SKDEMO_TEST1
, "Start test 1");
114 socket_menu
->AppendSeparator();
115 socket_menu
->Append(SKDEMO_CLOSE
, "Close session");
116 socket_menu
->AppendSeparator();
117 socket_menu
->Append(SKDEMO_TEST3
, "Start URL test");
119 menu_bar
->Append(socket_menu
, "Socket");
121 frame
->SetMenuBar(menu_bar
);
123 // Make a panel with a message
124 (void)new wxPanel(frame
, -1, wxPoint(0, 0), wxSize(300, 100));
129 // Return the main frame window
134 * MyFrame Constructor
137 wxFrame(NULL
, -1, "wxSocket client demo",
138 wxDefaultPosition
, wxSize(300, 200), wxDEFAULT_FRAME_STYLE
)
140 sock
= new MyClient();
141 sock
->SetFlags((wxSocketBase::wxSockFlags
) (wxSocketBase::WAITALL
| wxSocketBase::SPEED
));
143 sock
->SetNotify(GSOCK_LOST_FLAG
);
153 void MyFrame::OnQuitApp(wxCommandEvent
& WXUNUSED(evt
))
158 void MyFrame::OnExecOpenConnection(wxCommandEvent
& WXUNUSED(evt
))
162 if (sock
->IsConnected())
165 wxString hname
= wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
166 "Connect ...", "localhost");
167 addr
.Hostname(hname
);
170 sock
->Connect(addr
, TRUE
);
171 sock
->SetFlags(wxSocketBase::NONE
);
172 if (!sock
->IsConnected())
173 wxMessageBox("Can't connect to the specified host", "Alert !");
178 void MyFrame::OnExecCloseConnection(wxCommandEvent
& WXUNUSED(evt
))
184 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
185 EVT_BUTTON(ID_TEST_CLOSE
, MyFrame::OnCloseTest
)
186 EVT_MENU(SKDEMO_TEST1
, MyFrame::OnExecTest1
)
187 EVT_MENU(SKDEMO_TEST3
, MyFrame::OnExecUrlTest
)
188 EVT_MENU(SKDEMO_QUIT
, MyFrame::OnQuitApp
)
189 EVT_MENU(SKDEMO_CONNECT
, MyFrame::OnExecOpenConnection
)
190 EVT_MENU(SKDEMO_CLOSE
, MyFrame::OnExecCloseConnection
)
193 void MyFrame::OnCloseTest(wxCommandEvent
& evt
)
195 wxButton
*button
= (wxButton
*)evt
.GetEventObject();
196 wxDialog
*dlg
= (wxDialog
*)button
->GetParent();
201 void MyFrame::UpdateStatus()
203 if (!sock
->IsConnected()) {
204 SetStatusText("Not connected", 0);
205 SetStatusText("", 1);
211 wxSprintf(s
, _T("Connected to %s"), WXSTRINGCAST addr
.Hostname());
213 wxSprintf(s
, _T("Service: %d"), addr
.Service());
218 void MyFrame::OnExecTest1(wxCommandEvent
& WXUNUSED(evt
))
220 if (!sock
->IsConnected())
223 wxDialog
*dlgbox
= new wxDialog(this, -1, "Test 1", wxDefaultPosition
, wxSize(414, 250));
224 wxTextCtrl
*text_win
= new wxTextCtrl(dlgbox
, -1, "",
225 wxPoint(0, 0), wxSize(400, 200),
227 (void)new wxButton(dlgbox
, ID_TEST_CLOSE
, "Close",
228 wxPoint(100, 210), wxSize(100, -1));
234 text_win
->WriteText("Initializing test 1 ...\n");
239 buf
= copystring(_T("Hi ! Hi ! Hi !\n"));
240 buf2
= new wxChar
[wxStrlen(buf
)+1];
245 text_win
->WriteText("Sending some byte to the server ...");
247 sock
->Write((char *)buf
, wxStrlen(buf
)+1);
248 text_win
->WriteText("done\n");
250 text_win
->WriteText("Receiving some byte from the server ...");
252 sock
->Read((char *)buf2
, wxStrlen(buf
)+1);
253 text_win
->WriteText("done\n");
256 text_win
->WriteText("Comparing the two buffers ...");
257 if (memcmp(buf
, buf2
, wxStrlen(buf
)+1) != 0) {
258 text_win
->WriteText("Fail\n");
262 text_win
->WriteText("done\nTest 1 passed !\n");
274 void MyFrame::Download(wxInputStream
*input
)
276 wxProgressDialog
progress("Downloading ...", "0% downloaded");
277 wxBufferedInputStream
buf_in(*input
);
278 wxFileOutputStream
f_out("test.url");
280 size_t file_size
= input
->StreamSize();
282 int BUFSIZE
= (file_size
> 100) ? (file_size
/ 100) : file_size
;
283 int bytes_read
= BUFSIZE
;
289 // TODO: Support for streams which don't support StreamSize
291 buf
= new char[BUFSIZE
];
294 bytes_read
= BUFSIZE
;
295 while (downloaded
< file_size
&& bytes_read
!= 0) {
296 bytes_read
= buf_in
.Read(buf
, BUFSIZE
).LastRead();
297 f_out
.Write(buf
, bytes_read
);
298 downloaded
+= bytes_read
;
300 percents
= downloaded
* 100 / file_size
;
303 message
<< percents
<< _T("% downloaded");
304 progress
.Update(percents
, message
);
310 void MyFrame::OnExecUrlTest(wxCommandEvent
& WXUNUSED(evt
))
312 wxString urlname
= wxGetTextFromUser("Enter an URL to get",
313 "URL:", "http://localhost");
316 wxInputStream
*datas
= url
.GetInputStream();
320 error
.Printf(_T("Error in getting data from the URL. (error = %d)"), url
.GetError());
321 wxMessageBox(error
, "Alert !");