]> git.saurik.com Git - wxWidgets.git/blame - samples/wxsocket/client.cpp
* prgodlgg.h: Update() use wxString instead of 'char *'
[wxWidgets.git] / samples / wxsocket / client.cpp
CommitLineData
f4ada568
GL
1/*
2 * File: client.cpp
3 * Purpose: wxSocket: client demo
e2a6f233 4 * Author: LAVAUX Guilhem
f4ada568
GL
5 * Created: June 1997
6 * Updated:
e2a6f233 7 * Copyright: (c) 1997, LAVAUX Guilhem
f4ada568 8 */
e2a6f233 9
f4ada568
GL
10#ifdef __GNUG__
11#pragma implementation
12#pragma interface
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19#pragma hdrstop
20#endif
21
22#ifndef WX_PRECOMP
23#include "wx/wx.h"
24#endif
e2a6f233 25
a737331d 26#include "wx/wfstream.h"
f4ada568
GL
27#include "wx/socket.h"
28#include "wx/url.h"
29#include "wx/protocol/http.h"
30
e2a6f233
JS
31#if defined(__WXMOTIF__) || defined(__WXGTK__)
32#include "mondrian.xpm"
33#endif
34
f4ada568
GL
35// Define a new application type
36class MyApp: public wxApp
37{ public:
38 virtual bool OnInit(void);
39};
40
41class MyClient;
42
43// Define a new frame type
44class MyFrame: public wxFrame
45{
46 DECLARE_CLASS(MyFrame)
47public:
48 MyClient *sock;
49
50 MyFrame(void);
51 virtual ~MyFrame();
52 void OnCloseTest(wxCommandEvent& evt);
53 void OnExecTest1(wxCommandEvent& evt);
54 void OnExecUrlTest(wxCommandEvent& evt);
55 void OnQuitApp(wxCommandEvent& evt);
56 void OnExecOpenConnection(wxCommandEvent& evt);
57 void OnExecCloseConnection(wxCommandEvent& evt);
58 void UpdateStatus();
59
60 DECLARE_EVENT_TABLE()
61};
62
63
64IMPLEMENT_CLASS(MyFrame, wxFrame)
65
66/*
67 * Define a new derived SocketClient
68 */
69class MyClient: public wxSocketClient
70{
71public:
72 MyFrame *frame;
73
74 void OnNotify(wxRequestNotify WXUNUSED(flags)) { frame->UpdateStatus(); }
75};
76
77// ID for the menu quit command
db131261
RR
78const int SKDEMO_QUIT = 101;
79const int SKDEMO_CONNECT = 102;
80const int SKDEMO_TEST1 = 103;
81const int SKDEMO_TEST2 = 104;
82const int SKDEMO_CLOSE = 105;
83const int SKDEMO_TEST3 = 106;
84const int ID_TEST_CLOSE = 107;
f4ada568
GL
85
86IMPLEMENT_APP(MyApp)
87
88/*
89 * `Main program' equivalent, creating windows and returning main app frame
90 */
91bool MyApp::OnInit(void)
92{
93 // Create the main frame window
94 MyFrame *frame = new MyFrame();
95
96 // Give it an icon
e2a6f233 97 frame->SetIcon(wxICON(mondrian));
f4ada568
GL
98
99 // Make a menubar
100 wxMenu *file_menu = new wxMenu();
101
102 file_menu->Append(SKDEMO_QUIT, "Exit");
103 wxMenuBar *menu_bar = new wxMenuBar;
104 menu_bar->Append(file_menu, "File");
105
106 wxMenu *socket_menu = new wxMenu();
107 socket_menu->Append(SKDEMO_CONNECT, "Open session");
108 socket_menu->AppendSeparator();
109 socket_menu->Append(SKDEMO_TEST1, "Start test 1");
f4ada568
GL
110 socket_menu->AppendSeparator();
111 socket_menu->Append(SKDEMO_CLOSE, "Close session");
112 socket_menu->AppendSeparator();
113 socket_menu->Append(SKDEMO_TEST3, "Start URL test");
114
115 menu_bar->Append(socket_menu, "Socket");
116
117 frame->SetMenuBar(menu_bar);
118
119 // Make a panel with a message
e2a6f233 120 (void)new wxPanel(frame, -1, wxPoint(0, 0), wxSize(300, 100));
f4ada568
GL
121
122 // Show the frame
123 frame->Show(TRUE);
124
125 // Return the main frame window
27529614 126 return TRUE;
f4ada568
GL
127}
128
129/*
130 * MyFrame Constructor
131 */
132MyFrame::MyFrame():
133 wxFrame(NULL, -1, "wxSocket client demo",
134 wxDefaultPosition, wxSize(300, 200), wxDEFAULT_FRAME_STYLE)
135{
136 // Init all
137 wxSocketHandler::Master();
138
139 sock = new MyClient();
9111db68 140 sock->SetFlags((wxSocketBase::wxSockFlags) (wxSocketBase::WAITALL | wxSocketBase::SPEED));
f4ada568
GL
141 wxSocketHandler::Master().Register(sock);
142 sock->frame = this;
143 sock->SetNotify(wxSocketBase::REQ_LOST);
144 CreateStatusBar(2);
145 UpdateStatus();
146}
147
148MyFrame::~MyFrame()
149{
150 delete sock;
151}
152
153void MyFrame::OnQuitApp(wxCommandEvent& WXUNUSED(evt))
154{
155 Close(TRUE);
156}
157
158void MyFrame::OnExecOpenConnection(wxCommandEvent& WXUNUSED(evt))
159{
160 wxIPV4address addr;
161
162 if (sock->IsConnected())
163 sock->Close();
164
9111db68 165/*
f4ada568
GL
166 wxString hname = wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
167 "Connect ...", "localhost");
9111db68
GL
168*/
169 wxString hname = "localhost";
f4ada568
GL
170 addr.Hostname(hname);
171 addr.Service(3000);
172 sock->SetNotify(0);
173 sock->Connect(addr, TRUE);
174 if (!sock->IsConnected())
175 wxMessageBox("Can't connect to the specified host", "Alert !");
176
177 UpdateStatus();
178}
179
180void MyFrame::OnExecCloseConnection(wxCommandEvent& WXUNUSED(evt))
181{
182 if (sock)
183 sock->Close();
184 UpdateStatus();
185}
186
187BEGIN_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)
194END_EVENT_TABLE()
195
196void MyFrame::OnCloseTest(wxCommandEvent& evt)
197{
198 wxButton *button = (wxButton *)evt.GetEventObject();
199 wxDialog *dlg = (wxDialog *)button->GetParent();
200
201 dlg->EndModal(0);
202}
203
204void MyFrame::UpdateStatus()
205{
206 if (!sock->IsConnected()) {
207 SetStatusText("Not connected", 0);
208 SetStatusText("", 1);
209 } else {
210 wxIPV4address addr;
a737331d 211 wxChar s[100];
f4ada568
GL
212
213 sock->GetPeer(addr);
a737331d 214 wxSprintf(s, _T("Connected to %s"), WXSTRINGCAST addr.Hostname());
f4ada568 215 SetStatusText(s, 0);
a737331d 216 wxSprintf(s, _T("Service: %d"), addr.Service());
f4ada568
GL
217 SetStatusText(s, 1);
218 }
219}
220
221void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt))
222{
223 if (!sock->IsConnected())
224 return;
225
db131261 226 wxDialog *dlgbox = new wxDialog(this, -1, "Test 1", wxDefaultPosition, wxSize(414, 250));
f4ada568
GL
227 wxTextCtrl *text_win = new wxTextCtrl(dlgbox, -1, "",
228 wxPoint(0, 0), wxSize(400, 200),
229 wxTE_MULTILINE);
230 (void)new wxButton(dlgbox, ID_TEST_CLOSE, "Close",
db131261 231 wxPoint(100, 210), wxSize(100, -1));
a737331d 232 wxChar *buf, *buf2;
f4ada568
GL
233
234 dlgbox->Layout();
235 dlgbox->Show(TRUE);
236
237 text_win->WriteText("Initializing test 1 ...\n");
238
db131261
RR
239 wxYield();
240
f4ada568 241 /* Init */
a737331d
GL
242 buf = copystring(_T("Hi ! Hi ! Hi !\n"));
243 buf2 = new wxChar[wxStrlen(buf)+1];
f4ada568 244 char c = 0xbe;
a737331d 245 sock->Write(&c, 1);
f4ada568
GL
246
247 /* No 1 */
248 text_win->WriteText("Sending some byte to the server ...");
a737331d
GL
249 wxYield();
250 sock->Write((char *)buf, wxStrlen(buf)+1);
f4ada568 251 text_win->WriteText("done\n");
a737331d 252 wxYield();
f4ada568 253 text_win->WriteText("Receiving some byte from the server ...");
a737331d
GL
254 wxYield();
255 sock->Read((char *)buf2, wxStrlen(buf)+1);
f4ada568 256 text_win->WriteText("done\n");
a737331d 257 wxYield();
f4ada568
GL
258
259 text_win->WriteText("Comparing the two buffers ...");
a737331d 260 if (memcmp(buf, buf2, wxStrlen(buf)+1) != 0) {
f4ada568
GL
261 text_win->WriteText("Fail\n");
262 sock->Close();
263 UpdateStatus();
264 } else
265 text_win->WriteText("done\nTest 1 passed !\n");
266
267 dlgbox->Layout();
268 dlgbox->ShowModal();
269
270 delete [] buf;
271 delete [] buf2;
272 delete text_win;
273 delete dlgbox;
274}
275
276void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt))
277{
856d2e52
GL
278 wxString urlname = wxGetTextFromUser("Enter an URL to get",
279 "URL:", "http://localhost");
f4ada568
GL
280
281 wxURL url(urlname);
282 wxInputStream *datas = url.GetInputStream();
283
284 if (!datas)
285 wxMessageBox("Error in getting data from the URL.", "Alert !");
286 else {
a737331d
GL
287 wxFileOutputStream *str_out = new wxFileOutputStream("test.url");
288 str_out->Write(*datas);
289
856d2e52 290 wxMessageBox("Success !! Click on OK to see the text.", "OK");
f4ada568 291 delete datas;
a737331d 292 delete str_out;
f4ada568
GL
293 }
294}