]>
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/wfstream.h"
27 #include "wx/socket.h"
29 #include "wx/protocol/http.h"
31 #if defined(__WXMOTIF__) || defined(__WXGTK__)
32 #include "mondrian.xpm"
35 // Define a new application type
36 class MyApp
: public wxApp
38 virtual bool OnInit(void);
43 // Define a new frame type
44 class MyFrame
: public wxFrame
46 DECLARE_CLASS(MyFrame
)
52 void OnCloseTest(wxCommandEvent
& evt
);
53 void OnExecTest1(wxCommandEvent
& evt
);
54 void OnExecUrlTest(wxCommandEvent
& evt
);
55 void OnQuitApp(wxCommandEvent
& evt
);
56 void OnExecOpenConnection(wxCommandEvent
& evt
);
57 void OnExecCloseConnection(wxCommandEvent
& evt
);
64 IMPLEMENT_CLASS(MyFrame
, wxFrame
)
67 * Define a new derived SocketClient
69 class MyClient
: public wxSocketClient
74 void OnNotify(wxRequestNotify
WXUNUSED(flags
)) { frame
->UpdateStatus(); }
77 // ID for the menu quit command
78 const int SKDEMO_QUIT
= 101;
79 const int SKDEMO_CONNECT
= 102;
80 const int SKDEMO_TEST1
= 103;
81 const int SKDEMO_TEST2
= 104;
82 const int SKDEMO_CLOSE
= 105;
83 const int SKDEMO_TEST3
= 106;
84 const int ID_TEST_CLOSE
= 107;
89 * `Main program' equivalent, creating windows and returning main app frame
91 bool MyApp::OnInit(void)
93 // Create the main frame window
94 MyFrame
*frame
= new MyFrame();
97 frame
->SetIcon(wxICON(mondrian
));
100 wxMenu
*file_menu
= new wxMenu();
102 file_menu
->Append(SKDEMO_QUIT
, "Exit");
103 wxMenuBar
*menu_bar
= new wxMenuBar
;
104 menu_bar
->Append(file_menu
, "File");
106 wxMenu
*socket_menu
= new wxMenu();
107 socket_menu
->Append(SKDEMO_CONNECT
, "Open session");
108 socket_menu
->AppendSeparator();
109 socket_menu
->Append(SKDEMO_TEST1
, "Start test 1");
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");
115 menu_bar
->Append(socket_menu
, "Socket");
117 frame
->SetMenuBar(menu_bar
);
119 // Make a panel with a message
120 (void)new wxPanel(frame
, -1, wxPoint(0, 0), wxSize(300, 100));
125 // Return the main frame window
130 * MyFrame Constructor
133 wxFrame(NULL
, -1, "wxSocket client demo",
134 wxDefaultPosition
, wxSize(300, 200), wxDEFAULT_FRAME_STYLE
)
137 wxSocketHandler::Master();
139 sock
= new MyClient();
140 sock
->SetFlags((wxSocketBase::wxSockFlags
) (wxSocketBase::WAITALL
| wxSocketBase::SPEED
));
141 wxSocketHandler::Master().Register(sock
);
143 sock
->SetNotify(wxSocketBase::REQ_LOST
);
153 void MyFrame::OnQuitApp(wxCommandEvent
& WXUNUSED(evt
))
158 void MyFrame::OnExecOpenConnection(wxCommandEvent
& WXUNUSED(evt
))
162 if (sock
->IsConnected())
166 wxString hname = wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
167 "Connect ...", "localhost");
169 wxString hname
= "localhost";
170 addr
.Hostname(hname
);
173 sock
->Connect(addr
, TRUE
);
174 if (!sock
->IsConnected())
175 wxMessageBox("Can't connect to the specified host", "Alert !");
180 void MyFrame::OnExecCloseConnection(wxCommandEvent
& WXUNUSED(evt
))
187 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
188 EVT_BUTTON(ID_TEST_CLOSE
, MyFrame::OnCloseTest
)
189 EVT_MENU(SKDEMO_TEST1
, MyFrame::OnExecTest1
)
190 EVT_MENU(SKDEMO_TEST3
, MyFrame::OnExecUrlTest
)
191 EVT_MENU(SKDEMO_QUIT
, MyFrame::OnQuitApp
)
192 EVT_MENU(SKDEMO_CONNECT
, MyFrame::OnExecOpenConnection
)
193 EVT_MENU(SKDEMO_CLOSE
, MyFrame::OnExecCloseConnection
)
196 void MyFrame::OnCloseTest(wxCommandEvent
& evt
)
198 wxButton
*button
= (wxButton
*)evt
.GetEventObject();
199 wxDialog
*dlg
= (wxDialog
*)button
->GetParent();
204 void MyFrame::UpdateStatus()
206 if (!sock
->IsConnected()) {
207 SetStatusText("Not connected", 0);
208 SetStatusText("", 1);
214 wxSprintf(s
, _T("Connected to %s"), WXSTRINGCAST addr
.Hostname());
216 wxSprintf(s
, _T("Service: %d"), addr
.Service());
221 void MyFrame::OnExecTest1(wxCommandEvent
& WXUNUSED(evt
))
223 if (!sock
->IsConnected())
226 wxDialog
*dlgbox
= new wxDialog(this, -1, "Test 1", wxDefaultPosition
, wxSize(414, 250));
227 wxTextCtrl
*text_win
= new wxTextCtrl(dlgbox
, -1, "",
228 wxPoint(0, 0), wxSize(400, 200),
230 (void)new wxButton(dlgbox
, ID_TEST_CLOSE
, "Close",
231 wxPoint(100, 210), wxSize(100, -1));
237 text_win
->WriteText("Initializing test 1 ...\n");
242 buf
= copystring(_T("Hi ! Hi ! Hi !\n"));
243 buf2
= new wxChar
[wxStrlen(buf
)+1];
248 text_win
->WriteText("Sending some byte to the server ...");
250 sock
->Write((char *)buf
, wxStrlen(buf
)+1);
251 text_win
->WriteText("done\n");
253 text_win
->WriteText("Receiving some byte from the server ...");
255 sock
->Read((char *)buf2
, wxStrlen(buf
)+1);
256 text_win
->WriteText("done\n");
259 text_win
->WriteText("Comparing the two buffers ...");
260 if (memcmp(buf
, buf2
, wxStrlen(buf
)+1) != 0) {
261 text_win
->WriteText("Fail\n");
265 text_win
->WriteText("done\nTest 1 passed !\n");
276 void MyFrame::OnExecUrlTest(wxCommandEvent
& WXUNUSED(evt
))
278 wxString urlname
= wxGetTextFromUser("Enter an URL to get",
279 "URL:", "http://localhost");
282 wxInputStream
*datas
= url
.GetInputStream();
285 wxMessageBox("Error in getting data from the URL.", "Alert !");
287 wxFileOutputStream
*str_out
= new wxFileOutputStream("test.url");
288 str_out
->Write(*datas
);
290 wxMessageBox("Success !! Click on OK to see the text.", "OK");