1998-09-06 18:28:00 +00:00
|
|
|
/*
|
|
|
|
* File: client.cpp
|
|
|
|
* Purpose: wxSocket: client demo
|
1999-01-25 18:33:08 +00:00
|
|
|
* Author: LAVAUX Guilhem
|
1998-09-06 18:28:00 +00:00
|
|
|
* Created: June 1997
|
1999-05-25 17:14:56 +00:00
|
|
|
* CVS ID: $Id$
|
1999-01-25 18:33:08 +00:00
|
|
|
* Copyright: (c) 1997, LAVAUX Guilhem
|
1998-09-06 18:28:00 +00:00
|
|
|
*/
|
1999-01-25 18:33:08 +00:00
|
|
|
|
1998-09-06 18:28:00 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#pragma interface
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
1999-01-25 18:33:08 +00:00
|
|
|
|
1999-04-26 18:16:56 +00:00
|
|
|
#include "wx/wfstream.h"
|
1998-09-06 18:28:00 +00:00
|
|
|
#include "wx/socket.h"
|
|
|
|
#include "wx/url.h"
|
|
|
|
#include "wx/protocol/http.h"
|
1999-05-22 10:11:03 +00:00
|
|
|
#include "wx/thread.h"
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-01-25 18:33:08 +00:00
|
|
|
#if defined(__WXMOTIF__) || defined(__WXGTK__)
|
|
|
|
#include "mondrian.xpm"
|
|
|
|
#endif
|
|
|
|
|
1998-09-06 18:28:00 +00:00
|
|
|
// Define a new application type
|
|
|
|
class MyApp: public wxApp
|
|
|
|
{ public:
|
|
|
|
virtual bool OnInit(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
class MyClient;
|
|
|
|
|
|
|
|
// Define a new frame type
|
|
|
|
class MyFrame: public wxFrame
|
|
|
|
{
|
|
|
|
DECLARE_CLASS(MyFrame)
|
|
|
|
public:
|
|
|
|
MyClient *sock;
|
|
|
|
|
|
|
|
MyFrame(void);
|
|
|
|
virtual ~MyFrame();
|
|
|
|
void OnCloseTest(wxCommandEvent& evt);
|
|
|
|
void OnExecTest1(wxCommandEvent& evt);
|
|
|
|
void OnExecUrlTest(wxCommandEvent& evt);
|
|
|
|
void OnQuitApp(wxCommandEvent& evt);
|
|
|
|
void OnExecOpenConnection(wxCommandEvent& evt);
|
|
|
|
void OnExecCloseConnection(wxCommandEvent& evt);
|
|
|
|
void UpdateStatus();
|
|
|
|
|
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_CLASS(MyFrame, wxFrame)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Define a new derived SocketClient
|
|
|
|
*/
|
|
|
|
class MyClient: public wxSocketClient
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyFrame *frame;
|
|
|
|
|
|
|
|
void OnNotify(wxRequestNotify WXUNUSED(flags)) { frame->UpdateStatus(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
// ID for the menu quit command
|
1998-12-29 17:18:41 +00:00
|
|
|
const int SKDEMO_QUIT = 101;
|
|
|
|
const int SKDEMO_CONNECT = 102;
|
|
|
|
const int SKDEMO_TEST1 = 103;
|
|
|
|
const int SKDEMO_TEST2 = 104;
|
|
|
|
const int SKDEMO_CLOSE = 105;
|
|
|
|
const int SKDEMO_TEST3 = 106;
|
|
|
|
const int ID_TEST_CLOSE = 107;
|
1998-09-06 18:28:00 +00:00
|
|
|
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* `Main program' equivalent, creating windows and returning main app frame
|
|
|
|
*/
|
|
|
|
bool MyApp::OnInit(void)
|
|
|
|
{
|
|
|
|
// Create the main frame window
|
|
|
|
MyFrame *frame = new MyFrame();
|
|
|
|
|
|
|
|
// Give it an icon
|
1999-01-25 18:33:08 +00:00
|
|
|
frame->SetIcon(wxICON(mondrian));
|
1998-09-06 18:28:00 +00:00
|
|
|
|
|
|
|
// Make a menubar
|
|
|
|
wxMenu *file_menu = new wxMenu();
|
|
|
|
|
|
|
|
file_menu->Append(SKDEMO_QUIT, "Exit");
|
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
|
|
|
menu_bar->Append(file_menu, "File");
|
|
|
|
|
|
|
|
wxMenu *socket_menu = new wxMenu();
|
|
|
|
socket_menu->Append(SKDEMO_CONNECT, "Open session");
|
|
|
|
socket_menu->AppendSeparator();
|
|
|
|
socket_menu->Append(SKDEMO_TEST1, "Start test 1");
|
|
|
|
socket_menu->AppendSeparator();
|
|
|
|
socket_menu->Append(SKDEMO_CLOSE, "Close session");
|
|
|
|
socket_menu->AppendSeparator();
|
|
|
|
socket_menu->Append(SKDEMO_TEST3, "Start URL test");
|
|
|
|
|
|
|
|
menu_bar->Append(socket_menu, "Socket");
|
|
|
|
|
|
|
|
frame->SetMenuBar(menu_bar);
|
|
|
|
|
|
|
|
// Make a panel with a message
|
1999-01-25 18:33:08 +00:00
|
|
|
(void)new wxPanel(frame, -1, wxPoint(0, 0), wxSize(300, 100));
|
1998-09-06 18:28:00 +00:00
|
|
|
|
|
|
|
// Show the frame
|
|
|
|
frame->Show(TRUE);
|
|
|
|
|
|
|
|
// Return the main frame window
|
1998-09-08 22:27:12 +00:00
|
|
|
return TRUE;
|
1998-09-06 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MyFrame Constructor
|
|
|
|
*/
|
|
|
|
MyFrame::MyFrame():
|
|
|
|
wxFrame(NULL, -1, "wxSocket client demo",
|
|
|
|
wxDefaultPosition, wxSize(300, 200), wxDEFAULT_FRAME_STYLE)
|
|
|
|
{
|
|
|
|
// Init all
|
|
|
|
wxSocketHandler::Master();
|
|
|
|
|
|
|
|
sock = new MyClient();
|
1999-05-17 18:10:57 +00:00
|
|
|
sock->SetFlags((wxSocketBase::wxSockFlags) (wxSocketBase::WAITALL | wxSocketBase::SPEED));
|
1998-09-06 18:28:00 +00:00
|
|
|
wxSocketHandler::Master().Register(sock);
|
|
|
|
sock->frame = this;
|
|
|
|
sock->SetNotify(wxSocketBase::REQ_LOST);
|
|
|
|
CreateStatusBar(2);
|
|
|
|
UpdateStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
MyFrame::~MyFrame()
|
|
|
|
{
|
|
|
|
delete sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnQuitApp(wxCommandEvent& WXUNUSED(evt))
|
|
|
|
{
|
|
|
|
Close(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnExecOpenConnection(wxCommandEvent& WXUNUSED(evt))
|
|
|
|
{
|
|
|
|
wxIPV4address addr;
|
|
|
|
|
|
|
|
if (sock->IsConnected())
|
|
|
|
sock->Close();
|
|
|
|
|
|
|
|
wxString hname = wxGetTextFromUser("Enter the address of the wxSocket Sample Server",
|
|
|
|
"Connect ...", "localhost");
|
|
|
|
addr.Hostname(hname);
|
|
|
|
addr.Service(3000);
|
|
|
|
sock->SetNotify(0);
|
|
|
|
sock->Connect(addr, TRUE);
|
|
|
|
if (!sock->IsConnected())
|
|
|
|
wxMessageBox("Can't connect to the specified host", "Alert !");
|
|
|
|
|
|
|
|
UpdateStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnExecCloseConnection(wxCommandEvent& WXUNUSED(evt))
|
|
|
|
{
|
|
|
|
if (sock)
|
|
|
|
sock->Close();
|
|
|
|
UpdateStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
|
|
|
EVT_BUTTON(ID_TEST_CLOSE, MyFrame::OnCloseTest)
|
|
|
|
EVT_MENU(SKDEMO_TEST1, MyFrame::OnExecTest1)
|
|
|
|
EVT_MENU(SKDEMO_TEST3, MyFrame::OnExecUrlTest)
|
|
|
|
EVT_MENU(SKDEMO_QUIT, MyFrame::OnQuitApp)
|
|
|
|
EVT_MENU(SKDEMO_CONNECT, MyFrame::OnExecOpenConnection)
|
|
|
|
EVT_MENU(SKDEMO_CLOSE, MyFrame::OnExecCloseConnection)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
void MyFrame::OnCloseTest(wxCommandEvent& evt)
|
|
|
|
{
|
|
|
|
wxButton *button = (wxButton *)evt.GetEventObject();
|
|
|
|
wxDialog *dlg = (wxDialog *)button->GetParent();
|
|
|
|
|
|
|
|
dlg->EndModal(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::UpdateStatus()
|
|
|
|
{
|
|
|
|
if (!sock->IsConnected()) {
|
|
|
|
SetStatusText("Not connected", 0);
|
|
|
|
SetStatusText("", 1);
|
|
|
|
} else {
|
|
|
|
wxIPV4address addr;
|
1999-04-26 18:16:56 +00:00
|
|
|
wxChar s[100];
|
1998-09-06 18:28:00 +00:00
|
|
|
|
|
|
|
sock->GetPeer(addr);
|
1999-04-26 18:16:56 +00:00
|
|
|
wxSprintf(s, _T("Connected to %s"), WXSTRINGCAST addr.Hostname());
|
1998-09-06 18:28:00 +00:00
|
|
|
SetStatusText(s, 0);
|
1999-04-26 18:16:56 +00:00
|
|
|
wxSprintf(s, _T("Service: %d"), addr.Service());
|
1998-09-06 18:28:00 +00:00
|
|
|
SetStatusText(s, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt))
|
|
|
|
{
|
|
|
|
if (!sock->IsConnected())
|
|
|
|
return;
|
|
|
|
|
1998-12-29 17:18:41 +00:00
|
|
|
wxDialog *dlgbox = new wxDialog(this, -1, "Test 1", wxDefaultPosition, wxSize(414, 250));
|
1998-09-06 18:28:00 +00:00
|
|
|
wxTextCtrl *text_win = new wxTextCtrl(dlgbox, -1, "",
|
|
|
|
wxPoint(0, 0), wxSize(400, 200),
|
|
|
|
wxTE_MULTILINE);
|
|
|
|
(void)new wxButton(dlgbox, ID_TEST_CLOSE, "Close",
|
1998-12-29 17:18:41 +00:00
|
|
|
wxPoint(100, 210), wxSize(100, -1));
|
1999-04-26 18:16:56 +00:00
|
|
|
wxChar *buf, *buf2;
|
1998-09-06 18:28:00 +00:00
|
|
|
|
|
|
|
dlgbox->Layout();
|
|
|
|
dlgbox->Show(TRUE);
|
|
|
|
|
|
|
|
text_win->WriteText("Initializing test 1 ...\n");
|
|
|
|
|
1998-12-29 17:18:41 +00:00
|
|
|
wxYield();
|
|
|
|
|
1998-09-06 18:28:00 +00:00
|
|
|
/* Init */
|
1999-04-26 18:16:56 +00:00
|
|
|
buf = copystring(_T("Hi ! Hi ! Hi !\n"));
|
|
|
|
buf2 = new wxChar[wxStrlen(buf)+1];
|
1998-09-06 18:28:00 +00:00
|
|
|
char c = 0xbe;
|
1999-04-26 18:16:56 +00:00
|
|
|
sock->Write(&c, 1);
|
1998-09-06 18:28:00 +00:00
|
|
|
|
|
|
|
/* No 1 */
|
|
|
|
text_win->WriteText("Sending some byte to the server ...");
|
1999-04-26 18:16:56 +00:00
|
|
|
wxYield();
|
|
|
|
sock->Write((char *)buf, wxStrlen(buf)+1);
|
1998-09-06 18:28:00 +00:00
|
|
|
text_win->WriteText("done\n");
|
1999-04-26 18:16:56 +00:00
|
|
|
wxYield();
|
1998-09-06 18:28:00 +00:00
|
|
|
text_win->WriteText("Receiving some byte from the server ...");
|
1999-04-26 18:16:56 +00:00
|
|
|
wxYield();
|
|
|
|
sock->Read((char *)buf2, wxStrlen(buf)+1);
|
1998-09-06 18:28:00 +00:00
|
|
|
text_win->WriteText("done\n");
|
1999-04-26 18:16:56 +00:00
|
|
|
wxYield();
|
1998-09-06 18:28:00 +00:00
|
|
|
|
|
|
|
text_win->WriteText("Comparing the two buffers ...");
|
1999-04-26 18:16:56 +00:00
|
|
|
if (memcmp(buf, buf2, wxStrlen(buf)+1) != 0) {
|
1998-09-06 18:28:00 +00:00
|
|
|
text_win->WriteText("Fail\n");
|
|
|
|
sock->Close();
|
|
|
|
UpdateStatus();
|
|
|
|
} else
|
|
|
|
text_win->WriteText("done\nTest 1 passed !\n");
|
|
|
|
|
|
|
|
dlgbox->Layout();
|
|
|
|
dlgbox->ShowModal();
|
|
|
|
|
|
|
|
delete [] buf;
|
|
|
|
delete [] buf2;
|
|
|
|
delete text_win;
|
|
|
|
delete dlgbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnExecUrlTest(wxCommandEvent& WXUNUSED(evt))
|
|
|
|
{
|
1998-09-17 17:30:13 +00:00
|
|
|
wxString urlname = wxGetTextFromUser("Enter an URL to get",
|
|
|
|
"URL:", "http://localhost");
|
1998-09-06 18:28:00 +00:00
|
|
|
|
|
|
|
wxURL url(urlname);
|
|
|
|
wxInputStream *datas = url.GetInputStream();
|
|
|
|
|
1999-05-25 17:14:56 +00:00
|
|
|
if (!datas) {
|
|
|
|
wxString error;
|
|
|
|
error.Printf(_T("Error in getting data from the URL. (error = %d)"), url.GetError());
|
|
|
|
wxMessageBox(error, "Alert !");
|
|
|
|
} else {
|
1999-04-26 18:16:56 +00:00
|
|
|
wxFileOutputStream *str_out = new wxFileOutputStream("test.url");
|
|
|
|
str_out->Write(*datas);
|
|
|
|
|
1999-05-25 17:14:56 +00:00
|
|
|
wxMessageBox(_T("Success !! Click on OK to see the text."), "OK");
|
1998-09-06 18:28:00 +00:00
|
|
|
delete datas;
|
1999-04-26 18:16:56 +00:00
|
|
|
delete str_out;
|
1998-09-06 18:28:00 +00:00
|
|
|
}
|
|
|
|
}
|