]>
git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/client.cpp
3 * Purpose: wxSocket: client demo
4 * Author: LAVAUX Guilhem (from minimal.cc)
7 * Copyright: (c) 1993, AIAI, University of Edinburgh
8 * (C) 1997, LAVAUX Guilhem
11 #pragma implementation
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
25 #include "wx/socket.h"
27 #include "wx/protocol/http.h"
29 // Define a new application type
30 class MyApp
: public wxApp
32 virtual bool OnInit(void);
37 // Define a new frame type
38 class MyFrame
: public wxFrame
40 DECLARE_CLASS(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
);
58 IMPLEMENT_CLASS(MyFrame
, wxFrame
)
61 * Define a new derived SocketClient
63 class MyClient
: public wxSocketClient
68 void OnNotify(wxRequestNotify
WXUNUSED(flags
)) { frame
->UpdateStatus(); }
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;
83 * `Main program' equivalent, creating windows and returning main app frame
85 bool MyApp::OnInit(void)
87 // Create the main frame window
88 MyFrame
*frame
= new MyFrame();
92 frame
->SetIcon(new wxIcon("mondrian"));
95 frame
->SetIcon(new wxIcon("mondrian.xbm"));
99 wxMenu
*file_menu
= new wxMenu();
101 file_menu
->Append(SKDEMO_QUIT
, "Exit");
102 wxMenuBar
*menu_bar
= new wxMenuBar
;
103 menu_bar
->Append(file_menu
, "File");
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");
114 menu_bar
->Append(socket_menu
, "Socket");
116 frame
->SetMenuBar(menu_bar
);
118 // Make a panel with a message
119 txtctrl
= new wxTextCtrl(this);
124 // Return the main frame window
129 * MyFrame Constructor
132 wxFrame(NULL
, -1, "wxSocket client demo",
133 wxDefaultPosition
, wxSize(300, 200), wxDEFAULT_FRAME_STYLE
)
136 wxSocketHandler::Master();
138 sock
= new MyClient();
139 sock
->SetFlags(wxSocketBase::WAITALL
);
140 wxSocketHandler::Master().Register(sock
);
142 sock
->SetNotify(wxSocketBase::REQ_LOST
);
152 void MyFrame::OnQuitApp(wxCommandEvent
& WXUNUSED(evt
))
157 void MyFrame::OnExecOpenConnection(wxCommandEvent
& WXUNUSED(evt
))
161 if (sock
->IsConnected())
164 wxString hname
= wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
165 "Connect ...", "localhost");
166 addr
.Hostname(hname
);
169 sock
->Connect(addr
, TRUE
);
170 if (!sock
->IsConnected())
171 wxMessageBox("Can't connect to the specified host", "Alert !");
176 void MyFrame::OnExecCloseConnection(wxCommandEvent
& WXUNUSED(evt
))
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
)
192 void MyFrame::OnCloseTest(wxCommandEvent
& evt
)
194 wxButton
*button
= (wxButton
*)evt
.GetEventObject();
195 wxDialog
*dlg
= (wxDialog
*)button
->GetParent();
200 void MyFrame::UpdateStatus()
202 if (!sock
->IsConnected()) {
203 SetStatusText("Not connected", 0);
204 SetStatusText("", 1);
210 sprintf(s
, "Connected to %s", (const char *)addr
.Hostname());
212 sprintf(s
, "Service: %d", addr
.Service());
217 void MyFrame::OnExecTest1(wxCommandEvent
& WXUNUSED(evt
))
219 if (!sock
->IsConnected())
222 wxDialog
*dlgbox
= new wxDialog(this, -1, "Test 1", wxDefaultPosition
, wxSize(410, 270));
223 wxTextCtrl
*text_win
= new wxTextCtrl(dlgbox
, -1, "",
224 wxPoint(0, 0), wxSize(400, 200),
226 (void)new wxButton(dlgbox
, ID_TEST_CLOSE
, "Close",
227 wxPoint(100, 210), wxSize(100, 40));
233 text_win
->WriteText("Initializing test 1 ...\n");
236 buf
= copystring("Salut ! Salut ! Salut ! Salut Toto\n");
237 buf2
= new char[strlen(buf
)+1];
239 sock
->WriteMsg(&c
, 1);
242 text_win
->WriteText("Sending some byte to the server ...");
243 sock
->Write(buf
, strlen(buf
)+1);
244 text_win
->WriteText("done\n");
245 text_win
->WriteText("Receiving some byte from the server ...");
246 sock
->Read(buf2
, strlen(buf
)+1);
247 text_win
->WriteText("done\n");
249 text_win
->WriteText("Comparing the two buffers ...");
250 if (memcmp(buf
, buf2
, strlen(buf
)+1) != 0) {
251 text_win
->WriteText("Fail\n");
255 text_win
->WriteText("done\nTest 1 passed !\n");
266 void MyFrame::OnExecUrlTest(wxCommandEvent
& WXUNUSED(evt
))
268 wxString urlname
= wxGetTextFromUser("Enter an URL to get",
269 "URL:", "http://localhost");
272 wxInputStream
*datas
= url
.GetInputStream();
275 wxMessageBox("Error in getting data from the URL.", "Alert !");
277 wxMessageBox("Success !! Click on OK to see the text.", "OK");