]>
git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/client.cpp
3 * Purpose: wxSocket: client demo
4 * Author: LAVAUX Guilhem
7 * Copyright: (c) 1997, LAVAUX Guilhem
11 #pragma implementation
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
26 #include "wx/socket.h"
28 #include "wx/protocol/http.h"
30 #if defined(__WXMOTIF__) || defined(__WXGTK__)
31 #include "mondrian.xpm"
34 // Define a new application type
35 class MyApp
: public wxApp
37 virtual bool OnInit(void);
42 // Define a new frame type
43 class MyFrame
: public wxFrame
45 DECLARE_CLASS(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
);
63 IMPLEMENT_CLASS(MyFrame
, wxFrame
)
66 * Define a new derived SocketClient
68 class MyClient
: public wxSocketClient
73 void OnNotify(wxRequestNotify
WXUNUSED(flags
)) { frame
->UpdateStatus(); }
76 // ID for the menu quit command
77 const int SKDEMO_QUIT
= 101;
78 const int SKDEMO_CONNECT
= 102;
79 const int SKDEMO_TEST1
= 103;
80 const int SKDEMO_TEST2
= 104;
81 const int SKDEMO_CLOSE
= 105;
82 const int SKDEMO_TEST3
= 106;
83 const int ID_TEST_CLOSE
= 107;
88 * `Main program' equivalent, creating windows and returning main app frame
90 bool MyApp::OnInit(void)
92 // Create the main frame window
93 MyFrame
*frame
= new MyFrame();
96 frame
->SetIcon(wxICON(mondrian
));
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
, -1, wxPoint(0, 0), wxSize(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("Hi ! Hi ! Hi !\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");