]> git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/client.cpp
6a163e47f7c18150e30c18cb6e497189fe6d0f59
[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 * Updated:
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 /*
167 wxString hname = wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
168 "Connect ...", "localhost");
169 */
170 wxString hname = "localhost";
171 addr.Hostname(hname);
172 addr.Service(3000);
173 sock->SetNotify(0);
174 sock->Connect(addr, TRUE);
175 if (!sock->IsConnected())
176 wxMessageBox("Can't connect to the specified host", "Alert !");
177
178 UpdateStatus();
179 }
180
181 void MyFrame::OnExecCloseConnection(wxCommandEvent& WXUNUSED(evt))
182 {
183 if (sock)
184 sock->Close();
185 UpdateStatus();
186 }
187
188 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
189 EVT_BUTTON(ID_TEST_CLOSE, MyFrame::OnCloseTest)
190 EVT_MENU(SKDEMO_TEST1, MyFrame::OnExecTest1)
191 EVT_MENU(SKDEMO_TEST3, MyFrame::OnExecUrlTest)
192 EVT_MENU(SKDEMO_QUIT, MyFrame::OnQuitApp)
193 EVT_MENU(SKDEMO_CONNECT, MyFrame::OnExecOpenConnection)
194 EVT_MENU(SKDEMO_CLOSE, MyFrame::OnExecCloseConnection)
195 END_EVENT_TABLE()
196
197 void MyFrame::OnCloseTest(wxCommandEvent& evt)
198 {
199 wxButton *button = (wxButton *)evt.GetEventObject();
200 wxDialog *dlg = (wxDialog *)button->GetParent();
201
202 dlg->EndModal(0);
203 }
204
205 void MyFrame::UpdateStatus()
206 {
207 if (!sock->IsConnected()) {
208 SetStatusText("Not connected", 0);
209 SetStatusText("", 1);
210 } else {
211 wxIPV4address addr;
212 wxChar s[100];
213
214 sock->GetPeer(addr);
215 wxSprintf(s, _T("Connected to %s"), WXSTRINGCAST addr.Hostname());
216 SetStatusText(s, 0);
217 wxSprintf(s, _T("Service: %d"), addr.Service());
218 SetStatusText(s, 1);
219 }
220 }
221
222 void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt))
223 {
224 if (!sock->IsConnected())
225 return;
226
227 wxDialog *dlgbox = new wxDialog(this, -1, "Test 1", wxDefaultPosition, wxSize(414, 250));
228 wxTextCtrl *text_win = new wxTextCtrl(dlgbox, -1, "",
229 wxPoint(0, 0), wxSize(400, 200),
230 wxTE_MULTILINE);
231 (void)new wxButton(dlgbox, ID_TEST_CLOSE, "Close",
232 wxPoint(100, 210), wxSize(100, -1));
233 wxChar *buf, *buf2;
234
235 dlgbox->Layout();
236 dlgbox->Show(TRUE);
237
238 text_win->WriteText("Initializing test 1 ...\n");
239
240 wxYield();
241
242 /* Init */
243 buf = copystring(_T("Hi ! Hi ! Hi !\n"));
244 buf2 = new wxChar[wxStrlen(buf)+1];
245 char c = 0xbe;
246 sock->Write(&c, 1);
247
248 /* No 1 */
249 text_win->WriteText("Sending some byte to the server ...");
250 wxYield();
251 sock->Write((char *)buf, wxStrlen(buf)+1);
252 text_win->WriteText("done\n");
253 wxYield();
254 text_win->WriteText("Receiving some byte from the server ...");
255 wxYield();
256 sock->Read((char *)buf2, wxStrlen(buf)+1);
257 text_win->WriteText("done\n");
258 wxYield();
259
260 text_win->WriteText("Comparing the two buffers ...");
261 if (memcmp(buf, buf2, wxStrlen(buf)+1) != 0) {
262 text_win->WriteText("Fail\n");
263 sock->Close();
264 UpdateStatus();
265 } else
266 text_win->WriteText("done\nTest 1 passed !\n");
267
268 dlgbox->Layout();
269 dlgbox->ShowModal();
270
271 delete [] buf;
272 delete [] buf2;
273 delete text_win;
274 delete dlgbox;
275 }
276
277 void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt))
278 {
279 wxString urlname = wxGetTextFromUser("Enter an URL to get",
280 "URL:", "http://localhost");
281
282 wxURL url(urlname);
283 wxInputStream *datas = url.GetInputStream();
284
285 if (!datas)
286 wxMessageBox("Error in getting data from the URL.", "Alert !");
287 else {
288 wxFileOutputStream *str_out = new wxFileOutputStream("test.url");
289 str_out->Write(*datas);
290
291 wxMessageBox("Success !! Click on OK to see the text.", "OK");
292 delete datas;
293 delete str_out;
294 }
295 }