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