]>
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"
30 #include "wx/thread.h"
32 #if defined(__WXMOTIF__) || defined(__WXGTK__)
33 #include "mondrian.xpm"
36 // Define a new application type
37 class MyApp
: public wxApp
39 virtual bool OnInit(void);
44 // Define a new frame type
45 class MyFrame
: public wxFrame
47 DECLARE_CLASS(MyFrame
)
53 void OnCloseTest(wxCommandEvent
& evt
);
54 void OnExecTest1(wxCommandEvent
& evt
);
55 void OnExecUrlTest(wxCommandEvent
& evt
);
56 void OnQuitApp(wxCommandEvent
& evt
);
57 void OnExecOpenConnection(wxCommandEvent
& evt
);
58 void OnExecCloseConnection(wxCommandEvent
& evt
);
65 IMPLEMENT_CLASS(MyFrame
, wxFrame
)
68 * Define a new derived SocketClient
70 class MyClient
: public wxSocketClient
75 void OnNotify(wxRequestNotify
WXUNUSED(flags
)) { frame
->UpdateStatus(); }
78 // ID for the menu quit command
79 const int SKDEMO_QUIT
= 101;
80 const int SKDEMO_CONNECT
= 102;
81 const int SKDEMO_TEST1
= 103;
82 const int SKDEMO_TEST2
= 104;
83 const int SKDEMO_CLOSE
= 105;
84 const int SKDEMO_TEST3
= 106;
85 const int ID_TEST_CLOSE
= 107;
90 * `Main program' equivalent, creating windows and returning main app frame
92 bool MyApp::OnInit(void)
94 // Create the main frame window
95 MyFrame
*frame
= new MyFrame();
98 frame
->SetIcon(wxICON(mondrian
));
101 wxMenu
*file_menu
= new wxMenu();
103 file_menu
->Append(SKDEMO_QUIT
, "Exit");
104 wxMenuBar
*menu_bar
= new wxMenuBar
;
105 menu_bar
->Append(file_menu
, "File");
107 wxMenu
*socket_menu
= new wxMenu();
108 socket_menu
->Append(SKDEMO_CONNECT
, "Open session");
109 socket_menu
->AppendSeparator();
110 socket_menu
->Append(SKDEMO_TEST1
, "Start test 1");
111 socket_menu
->AppendSeparator();
112 socket_menu
->Append(SKDEMO_CLOSE
, "Close session");
113 socket_menu
->AppendSeparator();
114 socket_menu
->Append(SKDEMO_TEST3
, "Start URL test");
116 menu_bar
->Append(socket_menu
, "Socket");
118 frame
->SetMenuBar(menu_bar
);
120 // Make a panel with a message
121 (void)new wxPanel(frame
, -1, wxPoint(0, 0), wxSize(300, 100));
126 // Return the main frame window
131 * MyFrame Constructor
134 wxFrame(NULL
, -1, "wxSocket client demo",
135 wxDefaultPosition
, wxSize(300, 200), wxDEFAULT_FRAME_STYLE
)
138 wxSocketHandler::Master();
140 sock
= new MyClient();
141 sock
->SetFlags((wxSocketBase::wxSockFlags
) (wxSocketBase::WAITALL
| wxSocketBase::SPEED
));
142 wxSocketHandler::Master().Register(sock
);
144 sock
->SetNotify(wxSocketBase::REQ_LOST
);
154 void MyFrame::OnQuitApp(wxCommandEvent
& WXUNUSED(evt
))
159 void MyFrame::OnExecOpenConnection(wxCommandEvent
& WXUNUSED(evt
))
163 if (sock
->IsConnected())
166 wxString hname
= wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
167 "Connect ...", "localhost");
168 addr
.Hostname(hname
);
171 sock
->Connect(addr
, TRUE
);
172 if (!sock
->IsConnected())
173 wxMessageBox("Can't connect to the specified host", "Alert !");
178 void MyFrame::OnExecCloseConnection(wxCommandEvent
& WXUNUSED(evt
))
185 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
186 EVT_BUTTON(ID_TEST_CLOSE
, MyFrame::OnCloseTest
)
187 EVT_MENU(SKDEMO_TEST1
, MyFrame::OnExecTest1
)
188 EVT_MENU(SKDEMO_TEST3
, MyFrame::OnExecUrlTest
)
189 EVT_MENU(SKDEMO_QUIT
, MyFrame::OnQuitApp
)
190 EVT_MENU(SKDEMO_CONNECT
, MyFrame::OnExecOpenConnection
)
191 EVT_MENU(SKDEMO_CLOSE
, MyFrame::OnExecCloseConnection
)
194 void MyFrame::OnCloseTest(wxCommandEvent
& evt
)
196 wxButton
*button
= (wxButton
*)evt
.GetEventObject();
197 wxDialog
*dlg
= (wxDialog
*)button
->GetParent();
202 void MyFrame::UpdateStatus()
204 if (!sock
->IsConnected()) {
205 SetStatusText("Not connected", 0);
206 SetStatusText("", 1);
212 wxSprintf(s
, _T("Connected to %s"), WXSTRINGCAST addr
.Hostname());
214 wxSprintf(s
, _T("Service: %d"), addr
.Service());
219 void MyFrame::OnExecTest1(wxCommandEvent
& WXUNUSED(evt
))
221 if (!sock
->IsConnected())
224 wxDialog
*dlgbox
= new wxDialog(this, -1, "Test 1", wxDefaultPosition
, wxSize(414, 250));
225 wxTextCtrl
*text_win
= new wxTextCtrl(dlgbox
, -1, "",
226 wxPoint(0, 0), wxSize(400, 200),
228 (void)new wxButton(dlgbox
, ID_TEST_CLOSE
, "Close",
229 wxPoint(100, 210), wxSize(100, -1));
235 text_win
->WriteText("Initializing test 1 ...\n");
240 buf
= copystring(_T("Hi ! Hi ! Hi !\n"));
241 buf2
= new wxChar
[wxStrlen(buf
)+1];
246 text_win
->WriteText("Sending some byte to the server ...");
248 sock
->Write((char *)buf
, wxStrlen(buf
)+1);
249 text_win
->WriteText("done\n");
251 text_win
->WriteText("Receiving some byte from the server ...");
253 sock
->Read((char *)buf2
, wxStrlen(buf
)+1);
254 text_win
->WriteText("done\n");
257 text_win
->WriteText("Comparing the two buffers ...");
258 if (memcmp(buf
, buf2
, wxStrlen(buf
)+1) != 0) {
259 text_win
->WriteText("Fail\n");
263 text_win
->WriteText("done\nTest 1 passed !\n");
274 void MyFrame::OnExecUrlTest(wxCommandEvent
& WXUNUSED(evt
))
276 wxString urlname
= wxGetTextFromUser("Enter an URL to get",
277 "URL:", "http://localhost");
280 wxInputStream
*datas
= url
.GetInputStream();
284 error
.Printf(_T("Error in getting data from the URL. (error = %d)"), url
.GetError());
285 wxMessageBox(error
, "Alert !");
287 wxFileOutputStream
*str_out
= new wxFileOutputStream("test.url");
288 str_out
->Write(*datas
);
290 wxMessageBox(_T("Success !! Click on OK to see the text."), "OK");