]> git.saurik.com Git - wxWidgets.git/blame - samples/wxsocket/client.cpp
Added dde sample; added docs/index.htm
[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
f4ada568
GL
26#include "wx/socket.h"
27#include "wx/url.h"
28#include "wx/protocol/http.h"
29
e2a6f233
JS
30#if defined(__WXMOTIF__) || defined(__WXGTK__)
31#include "mondrian.xpm"
32#endif
33
f4ada568
GL
34// Define a new application type
35class MyApp: public wxApp
36{ public:
37 virtual bool OnInit(void);
38};
39
40class MyClient;
41
42// Define a new frame type
43class MyFrame: public wxFrame
44{
45 DECLARE_CLASS(MyFrame)
46public:
47 MyClient *sock;
48
49 MyFrame(void);
50 virtual ~MyFrame();
51 void OnCloseTest(wxCommandEvent& evt);
52 void OnExecTest1(wxCommandEvent& evt);
53 void OnExecUrlTest(wxCommandEvent& evt);
54 void OnQuitApp(wxCommandEvent& evt);
55 void OnExecOpenConnection(wxCommandEvent& evt);
56 void OnExecCloseConnection(wxCommandEvent& evt);
57 void UpdateStatus();
58
59 DECLARE_EVENT_TABLE()
60};
61
62
63IMPLEMENT_CLASS(MyFrame, wxFrame)
64
65/*
66 * Define a new derived SocketClient
67 */
68class MyClient: public wxSocketClient
69{
70public:
71 MyFrame *frame;
72
73 void OnNotify(wxRequestNotify WXUNUSED(flags)) { frame->UpdateStatus(); }
74};
75
76// ID for the menu quit command
db131261
RR
77const int SKDEMO_QUIT = 101;
78const int SKDEMO_CONNECT = 102;
79const int SKDEMO_TEST1 = 103;
80const int SKDEMO_TEST2 = 104;
81const int SKDEMO_CLOSE = 105;
82const int SKDEMO_TEST3 = 106;
83const int ID_TEST_CLOSE = 107;
f4ada568
GL
84
85IMPLEMENT_APP(MyApp)
86
87/*
88 * `Main program' equivalent, creating windows and returning main app frame
89 */
90bool MyApp::OnInit(void)
91{
92 // Create the main frame window
93 MyFrame *frame = new MyFrame();
94
95 // Give it an icon
e2a6f233 96 frame->SetIcon(wxICON(mondrian));
f4ada568
GL
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");
f4ada568
GL
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
e2a6f233 119 (void)new wxPanel(frame, -1, wxPoint(0, 0), wxSize(300, 100));
f4ada568
GL
120
121 // Show the frame
122 frame->Show(TRUE);
123
124 // Return the main frame window
27529614 125 return TRUE;
f4ada568
GL
126}
127
128/*
129 * MyFrame Constructor
130 */
131MyFrame::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
147MyFrame::~MyFrame()
148{
149 delete sock;
150}
151
152void MyFrame::OnQuitApp(wxCommandEvent& WXUNUSED(evt))
153{
154 Close(TRUE);
155}
156
157void 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
176void MyFrame::OnExecCloseConnection(wxCommandEvent& WXUNUSED(evt))
177{
178 if (sock)
179 sock->Close();
180 UpdateStatus();
181}
182
183BEGIN_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)
190END_EVENT_TABLE()
191
192void MyFrame::OnCloseTest(wxCommandEvent& evt)
193{
194 wxButton *button = (wxButton *)evt.GetEventObject();
195 wxDialog *dlg = (wxDialog *)button->GetParent();
196
197 dlg->EndModal(0);
198}
199
200void 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
217void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt))
218{
219 if (!sock->IsConnected())
220 return;
221
db131261 222 wxDialog *dlgbox = new wxDialog(this, -1, "Test 1", wxDefaultPosition, wxSize(414, 250));
f4ada568
GL
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",
db131261 227 wxPoint(100, 210), wxSize(100, -1));
f4ada568
GL
228 char *buf, *buf2;
229
230 dlgbox->Layout();
231 dlgbox->Show(TRUE);
232
233 text_win->WriteText("Initializing test 1 ...\n");
234
db131261
RR
235 wxYield();
236
f4ada568
GL
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
268void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt))
269{
856d2e52
GL
270 wxString urlname = wxGetTextFromUser("Enter an URL to get",
271 "URL:", "http://localhost");
f4ada568
GL
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 {
856d2e52 279 wxMessageBox("Success !! Click on OK to see the text.", "OK");
f4ada568
GL
280 delete datas;
281 }
282}