]> git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/client.cpp
* Added wxsocket lib and sample (I hope I don't forget some file)
[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 SKDEMO_QUIT = 101;
73 const SKDEMO_CONNECT = 102;
74 const SKDEMO_TEST1 = 103;
75 const SKDEMO_TEST2 = 104;
76 const SKDEMO_CLOSE = 105;
77 const SKDEMO_TEST3 = 106;
78 const 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->Append(SKDEMO_TEST2, "Start test 2");
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
120 (void)new wxPanel(frame, 0, 0, 300, 100);
121
122 // Show the frame
123 frame->Show(TRUE);
124
125 // Return the main frame window
126 return true;
127 }
128
129 /*
130 * MyFrame Constructor
131 */
132 MyFrame::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();
140 sock->SetFlags(wxSocketBase::WAITALL);
141 wxSocketHandler::Master().Register(sock);
142 sock->frame = this;
143 sock->SetNotify(wxSocketBase::REQ_LOST);
144 CreateStatusBar(2);
145 UpdateStatus();
146 }
147
148 MyFrame::~MyFrame()
149 {
150 delete sock;
151 }
152
153 void MyFrame::OnQuitApp(wxCommandEvent& WXUNUSED(evt))
154 {
155 Close(TRUE);
156 }
157
158 void MyFrame::OnExecOpenConnection(wxCommandEvent& WXUNUSED(evt))
159 {
160 wxIPV4address addr;
161
162 if (sock->IsConnected())
163 sock->Close();
164
165 wxString hname = wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
166 "Connect ...", "localhost");
167 addr.Hostname(hname);
168 addr.Service(3000);
169 sock->SetNotify(0);
170 sock->Connect(addr, TRUE);
171 if (!sock->IsConnected())
172 wxMessageBox("Can't connect to the specified host", "Alert !");
173
174 UpdateStatus();
175 }
176
177 void MyFrame::OnExecCloseConnection(wxCommandEvent& WXUNUSED(evt))
178 {
179 if (sock)
180 sock->Close();
181 UpdateStatus();
182 }
183
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)
191 END_EVENT_TABLE()
192
193 void MyFrame::OnCloseTest(wxCommandEvent& evt)
194 {
195 wxButton *button = (wxButton *)evt.GetEventObject();
196 wxDialog *dlg = (wxDialog *)button->GetParent();
197
198 dlg->EndModal(0);
199 }
200
201 void MyFrame::UpdateStatus()
202 {
203 if (!sock->IsConnected()) {
204 SetStatusText("Not connected", 0);
205 SetStatusText("", 1);
206 } else {
207 wxIPV4address addr;
208 char s[100];
209
210 sock->GetPeer(addr);
211 sprintf(s, "Connected to %s", (const char *)addr.Hostname());
212 SetStatusText(s, 0);
213 sprintf(s, "Service: %d", addr.Service());
214 SetStatusText(s, 1);
215 }
216 }
217
218 void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt))
219 {
220 if (!sock->IsConnected())
221 return;
222
223 wxDialog *dlgbox = new wxDialog(this, -1, "Test 1", wxDefaultPosition, wxSize(410, 270));
224 wxTextCtrl *text_win = new wxTextCtrl(dlgbox, -1, "",
225 wxPoint(0, 0), wxSize(400, 200),
226 wxTE_MULTILINE);
227 (void)new wxButton(dlgbox, ID_TEST_CLOSE, "Close",
228 wxPoint(100, 210), wxSize(100, 40));
229 char *buf, *buf2;
230
231 dlgbox->Layout();
232 dlgbox->Show(TRUE);
233
234 text_win->WriteText("Initializing test 1 ...\n");
235
236 /* Init */
237 buf = copystring("Salut ! Salut ! Salut ! Salut Toto\n");
238 buf2 = new char[strlen(buf)+1];
239 char c = 0xbe;
240 sock->WriteMsg(&c, 1);
241
242 /* No 1 */
243 text_win->WriteText("Sending some byte to the server ...");
244 sock->Write(buf, strlen(buf)+1);
245 text_win->WriteText("done\n");
246 text_win->WriteText("Receiving some byte from the server ...");
247 sock->Read(buf2, strlen(buf)+1);
248 text_win->WriteText("done\n");
249
250 text_win->WriteText("Comparing the two buffers ...");
251 if (memcmp(buf, buf2, strlen(buf)+1) != 0) {
252 text_win->WriteText("Fail\n");
253 sock->Close();
254 UpdateStatus();
255 } else
256 text_win->WriteText("done\nTest 1 passed !\n");
257
258 dlgbox->Layout();
259 dlgbox->ShowModal();
260
261 delete [] buf;
262 delete [] buf2;
263 delete text_win;
264 delete dlgbox;
265 }
266
267 void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt))
268 {
269 wxString urlname = wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
270 "Connect ...", "localhost");
271
272 wxURL url(urlname);
273 wxInputStream *datas = url.GetInputStream();
274
275 if (!datas)
276 wxMessageBox("Error in getting data from the URL.", "Alert !");
277 else {
278 wxMessageBox("Success !!", "OK !");
279 delete datas;
280 }
281 }