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