]>
git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/client.cpp
6a163e47f7c18150e30c18cb6e497189fe6d0f59
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())
167 wxString hname = wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
168 "Connect ...", "localhost");
170 wxString hname
= "localhost";
171 addr
.Hostname(hname
);
174 sock
->Connect(addr
, TRUE
);
175 if (!sock
->IsConnected())
176 wxMessageBox("Can't connect to the specified host", "Alert !");
181 void MyFrame::OnExecCloseConnection(wxCommandEvent
& WXUNUSED(evt
))
188 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
189 EVT_BUTTON(ID_TEST_CLOSE
, MyFrame::OnCloseTest
)
190 EVT_MENU(SKDEMO_TEST1
, MyFrame::OnExecTest1
)
191 EVT_MENU(SKDEMO_TEST3
, MyFrame::OnExecUrlTest
)
192 EVT_MENU(SKDEMO_QUIT
, MyFrame::OnQuitApp
)
193 EVT_MENU(SKDEMO_CONNECT
, MyFrame::OnExecOpenConnection
)
194 EVT_MENU(SKDEMO_CLOSE
, MyFrame::OnExecCloseConnection
)
197 void MyFrame::OnCloseTest(wxCommandEvent
& evt
)
199 wxButton
*button
= (wxButton
*)evt
.GetEventObject();
200 wxDialog
*dlg
= (wxDialog
*)button
->GetParent();
205 void MyFrame::UpdateStatus()
207 if (!sock
->IsConnected()) {
208 SetStatusText("Not connected", 0);
209 SetStatusText("", 1);
215 wxSprintf(s
, _T("Connected to %s"), WXSTRINGCAST addr
.Hostname());
217 wxSprintf(s
, _T("Service: %d"), addr
.Service());
222 void MyFrame::OnExecTest1(wxCommandEvent
& WXUNUSED(evt
))
224 if (!sock
->IsConnected())
227 wxDialog
*dlgbox
= new wxDialog(this, -1, "Test 1", wxDefaultPosition
, wxSize(414, 250));
228 wxTextCtrl
*text_win
= new wxTextCtrl(dlgbox
, -1, "",
229 wxPoint(0, 0), wxSize(400, 200),
231 (void)new wxButton(dlgbox
, ID_TEST_CLOSE
, "Close",
232 wxPoint(100, 210), wxSize(100, -1));
238 text_win
->WriteText("Initializing test 1 ...\n");
243 buf
= copystring(_T("Hi ! Hi ! Hi !\n"));
244 buf2
= new wxChar
[wxStrlen(buf
)+1];
249 text_win
->WriteText("Sending some byte to the server ...");
251 sock
->Write((char *)buf
, wxStrlen(buf
)+1);
252 text_win
->WriteText("done\n");
254 text_win
->WriteText("Receiving some byte from the server ...");
256 sock
->Read((char *)buf2
, wxStrlen(buf
)+1);
257 text_win
->WriteText("done\n");
260 text_win
->WriteText("Comparing the two buffers ...");
261 if (memcmp(buf
, buf2
, wxStrlen(buf
)+1) != 0) {
262 text_win
->WriteText("Fail\n");
266 text_win
->WriteText("done\nTest 1 passed !\n");
277 void MyFrame::OnExecUrlTest(wxCommandEvent
& WXUNUSED(evt
))
279 wxString urlname
= wxGetTextFromUser("Enter an URL to get",
280 "URL:", "http://localhost");
283 wxInputStream
*datas
= url
.GetInputStream();
286 wxMessageBox("Error in getting data from the URL.", "Alert !");
288 wxFileOutputStream
*str_out
= new wxFileOutputStream("test.url");
289 str_out
->Write(*datas
);
291 wxMessageBox("Success !! Click on OK to see the text.", "OK");