]>
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 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;
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 (void)new wxPanel(frame
, 0, 0, 300, 100);
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(414, 250));
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, -1));
233 text_win
->WriteText("Initializing test 1 ...\n");
238 buf
= copystring("Salut ! Salut ! Salut ! Salut Toto\n");
239 buf2
= new char[strlen(buf
)+1];
241 sock
->WriteMsg(&c
, 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");
251 text_win
->WriteText("Comparing the two buffers ...");
252 if (memcmp(buf
, buf2
, strlen(buf
)+1) != 0) {
253 text_win
->WriteText("Fail\n");
257 text_win
->WriteText("done\nTest 1 passed !\n");
268 void MyFrame::OnExecUrlTest(wxCommandEvent
& WXUNUSED(evt
))
270 wxString urlname
= wxGetTextFromUser("Enter an URL to get",
271 "URL:", "http://localhost");
274 wxInputStream
*datas
= url
.GetInputStream();
277 wxMessageBox("Error in getting data from the URL.", "Alert !");
279 wxMessageBox("Success !! Click on OK to see the text.", "OK");