1999-09-30 23:56:10 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: client.cpp
|
|
|
|
// Purpose: Client for wxSocket demo
|
|
|
|
// Author: Guillermo Rodriguez Garcia <guille@iies.es>
|
|
|
|
// Modified by:
|
|
|
|
// Created: 1999/09/19
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1999 Guillermo Rodriguez Garcia
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// declarations
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// --------------------------------------------------------------------------
|
1999-01-25 18:33:08 +00:00
|
|
|
|
2002-08-31 22:31:03 +00:00
|
|
|
#if defined(__GNUG__) && !defined(__APPLE__)
|
2005-03-30 19:10:46 +00:00
|
|
|
#pragma implementation
|
|
|
|
#pragma interface
|
1998-09-06 18:28:00 +00:00
|
|
|
#endif
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
1998-09-06 18:28:00 +00:00
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
1999-09-30 23:56:10 +00:00
|
|
|
# pragma hdrstop
|
1998-09-06 18:28:00 +00:00
|
|
|
#endif
|
|
|
|
|
2004-10-06 20:54:57 +00:00
|
|
|
// for all others, include the necessary headers
|
1998-09-06 18:28:00 +00:00
|
|
|
#ifndef WX_PRECOMP
|
1999-09-30 23:56:10 +00:00
|
|
|
# include "wx/wx.h"
|
1999-10-25 14:27:04 +00:00
|
|
|
#endif
|
|
|
|
|
2000-01-21 03:16:02 +00:00
|
|
|
#include "wx/socket.h"
|
|
|
|
#include "wx/url.h"
|
2000-01-26 01:54:14 +00:00
|
|
|
#include "wx/wfstream.h"
|
1999-01-25 18:33:08 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// resources
|
|
|
|
// --------------------------------------------------------------------------
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// the application icon
|
2002-03-21 10:32:18 +00:00
|
|
|
#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
|
1999-09-30 23:56:10 +00:00
|
|
|
# include "mondrian.xpm"
|
1999-01-25 18:33:08 +00:00
|
|
|
#endif
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// classes
|
|
|
|
// --------------------------------------------------------------------------
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// Define a new application type
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
1998-09-06 18:28:00 +00:00
|
|
|
public:
|
1999-09-30 23:56:10 +00:00
|
|
|
virtual bool OnInit();
|
1998-09-06 18:28:00 +00:00
|
|
|
};
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// Define a new frame type: this is going to be our main frame
|
|
|
|
class MyFrame : public wxFrame
|
1998-09-06 18:28:00 +00:00
|
|
|
{
|
|
|
|
public:
|
1999-09-30 23:56:10 +00:00
|
|
|
MyFrame();
|
|
|
|
~MyFrame();
|
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
// event handlers for File menu
|
1999-09-30 23:56:10 +00:00
|
|
|
void OnQuit(wxCommandEvent& event);
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
2000-01-26 01:54:14 +00:00
|
|
|
|
|
|
|
// event handlers for Socket menu
|
1999-09-30 23:56:10 +00:00
|
|
|
void OnOpenConnection(wxCommandEvent& event);
|
|
|
|
void OnTest1(wxCommandEvent& event);
|
|
|
|
void OnTest2(wxCommandEvent& event);
|
|
|
|
void OnTest3(wxCommandEvent& event);
|
|
|
|
void OnCloseConnection(wxCommandEvent& event);
|
2000-01-26 01:54:14 +00:00
|
|
|
|
2005-02-17 13:13:01 +00:00
|
|
|
#if wxUSE_URL
|
2000-01-26 01:54:14 +00:00
|
|
|
// event handlers for Protocols menu
|
|
|
|
void OnTestURL(wxCommandEvent& event);
|
2005-02-17 13:13:01 +00:00
|
|
|
#endif
|
2000-01-26 01:54:14 +00:00
|
|
|
|
2000-03-03 19:22:35 +00:00
|
|
|
// event handlers for DatagramSocket menu (stub)
|
2000-01-21 03:16:02 +00:00
|
|
|
void OnDatagram(wxCommandEvent& event);
|
1999-09-30 23:56:10 +00:00
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
// socket event handler
|
|
|
|
void OnSocketEvent(wxSocketEvent& event);
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// convenience functions
|
|
|
|
void UpdateStatusBar();
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxSocketClient *m_sock;
|
|
|
|
wxTextCtrl *m_text;
|
|
|
|
wxMenu *m_menuFile;
|
|
|
|
wxMenu *m_menuSocket;
|
2000-01-21 03:16:02 +00:00
|
|
|
wxMenu *m_menuDatagramSocket;
|
2000-01-26 01:54:14 +00:00
|
|
|
wxMenu *m_menuProtocols;
|
1999-09-30 23:56:10 +00:00
|
|
|
wxMenuBar *m_menuBar;
|
|
|
|
bool m_busy;
|
|
|
|
|
2004-05-25 11:20:37 +00:00
|
|
|
// any class wishing to process wxWidgets events must use this macro
|
1999-09-30 23:56:10 +00:00
|
|
|
DECLARE_EVENT_TABLE()
|
1998-09-06 18:28:00 +00:00
|
|
|
};
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// --------------------------------------------------------------------------
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// IDs for the controls and the menu commands
|
|
|
|
enum
|
1998-09-06 18:28:00 +00:00
|
|
|
{
|
1999-09-30 23:56:10 +00:00
|
|
|
// menu items
|
|
|
|
CLIENT_QUIT = 1000,
|
|
|
|
CLIENT_ABOUT,
|
|
|
|
CLIENT_OPEN,
|
|
|
|
CLIENT_TEST1,
|
|
|
|
CLIENT_TEST2,
|
|
|
|
CLIENT_TEST3,
|
|
|
|
CLIENT_CLOSE,
|
2005-02-17 13:13:01 +00:00
|
|
|
#if wxUSE_URL
|
2000-01-26 01:54:14 +00:00
|
|
|
CLIENT_TESTURL,
|
2005-02-17 13:13:01 +00:00
|
|
|
#endif
|
2000-01-21 03:16:02 +00:00
|
|
|
CLIENT_DGRAM,
|
1999-09-30 23:56:10 +00:00
|
|
|
|
|
|
|
// id for socket
|
|
|
|
SOCKET_ID
|
|
|
|
};
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// --------------------------------------------------------------------------
|
2004-05-25 11:20:37 +00:00
|
|
|
// event tables and other macros for wxWidgets
|
1999-09-30 23:56:10 +00:00
|
|
|
// --------------------------------------------------------------------------
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2000-01-26 01:54:14 +00:00
|
|
|
EVT_MENU(CLIENT_QUIT, MyFrame::OnQuit)
|
|
|
|
EVT_MENU(CLIENT_ABOUT, MyFrame::OnAbout)
|
|
|
|
EVT_MENU(CLIENT_OPEN, MyFrame::OnOpenConnection)
|
|
|
|
EVT_MENU(CLIENT_TEST1, MyFrame::OnTest1)
|
|
|
|
EVT_MENU(CLIENT_TEST2, MyFrame::OnTest2)
|
|
|
|
EVT_MENU(CLIENT_TEST3, MyFrame::OnTest3)
|
|
|
|
EVT_MENU(CLIENT_CLOSE, MyFrame::OnCloseConnection)
|
|
|
|
EVT_MENU(CLIENT_DGRAM, MyFrame::OnDatagram)
|
2005-02-17 13:13:01 +00:00
|
|
|
#if wxUSE_URL
|
2000-01-26 01:54:14 +00:00
|
|
|
EVT_MENU(CLIENT_TESTURL, MyFrame::OnTestURL)
|
2005-02-17 13:13:01 +00:00
|
|
|
#endif
|
2000-01-26 01:54:14 +00:00
|
|
|
EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent)
|
1999-09-30 23:56:10 +00:00
|
|
|
END_EVENT_TABLE()
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
IMPLEMENT_APP(MyApp)
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// ==========================================================================
|
|
|
|
// implementation
|
|
|
|
// ==========================================================================
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// the application class
|
|
|
|
// --------------------------------------------------------------------------
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
|
|
|
// Create the main application window
|
|
|
|
MyFrame *frame = new MyFrame();
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// Show it and tell the application that it's our main window
|
2004-06-02 17:03:20 +00:00
|
|
|
frame->Show(true);
|
1999-09-30 23:56:10 +00:00
|
|
|
SetTopWindow(frame);
|
|
|
|
|
|
|
|
// success
|
2004-06-02 17:03:20 +00:00
|
|
|
return true;
|
1998-09-06 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// main frame
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// frame constructor
|
2004-06-02 17:03:20 +00:00
|
|
|
MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY,
|
2000-01-26 01:54:14 +00:00
|
|
|
_("wxSocket demo: Client"),
|
1999-09-30 23:56:10 +00:00
|
|
|
wxDefaultPosition, wxSize(300, 200))
|
1998-09-06 18:28:00 +00:00
|
|
|
{
|
1999-09-30 23:56:10 +00:00
|
|
|
// Give the frame an icon
|
|
|
|
SetIcon(wxICON(mondrian));
|
|
|
|
|
|
|
|
// Make menus
|
|
|
|
m_menuFile = new wxMenu();
|
2000-01-26 01:54:14 +00:00
|
|
|
m_menuFile->Append(CLIENT_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog"));
|
1999-09-30 23:56:10 +00:00
|
|
|
m_menuFile->AppendSeparator();
|
2000-01-26 01:54:14 +00:00
|
|
|
m_menuFile->Append(CLIENT_QUIT, _("E&xit\tAlt-X"), _("Quit client"));
|
1999-09-30 23:56:10 +00:00
|
|
|
|
|
|
|
m_menuSocket = new wxMenu();
|
2000-01-26 01:54:14 +00:00
|
|
|
m_menuSocket->Append(CLIENT_OPEN, _("&Open session"), _("Connect to server"));
|
1999-09-30 23:56:10 +00:00
|
|
|
m_menuSocket->AppendSeparator();
|
2000-01-26 01:54:14 +00:00
|
|
|
m_menuSocket->Append(CLIENT_TEST1, _("Test &1"), _("Test basic functionality"));
|
|
|
|
m_menuSocket->Append(CLIENT_TEST2, _("Test &2"), _("Test ReadMsg and WriteMsg"));
|
|
|
|
m_menuSocket->Append(CLIENT_TEST3, _("Test &3"), _("Test large data transfer"));
|
1999-09-30 23:56:10 +00:00
|
|
|
m_menuSocket->AppendSeparator();
|
2000-01-26 01:54:14 +00:00
|
|
|
m_menuSocket->Append(CLIENT_CLOSE, _("&Close session"), _("Close connection"));
|
1999-09-30 23:56:10 +00:00
|
|
|
|
2000-01-21 03:16:02 +00:00
|
|
|
m_menuDatagramSocket = new wxMenu();
|
2000-01-26 01:54:14 +00:00
|
|
|
m_menuDatagramSocket->Append(CLIENT_DGRAM, _("Send Datagram"), _("Test UDP sockets"));
|
|
|
|
|
2005-02-17 13:13:01 +00:00
|
|
|
#if wxUSE_URL
|
2000-01-26 01:54:14 +00:00
|
|
|
m_menuProtocols = new wxMenu();
|
|
|
|
m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL"), _("Get data from the specified URL"));
|
2005-02-17 13:13:01 +00:00
|
|
|
#endif
|
2000-01-21 03:16:02 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// Append menus to the menubar
|
|
|
|
m_menuBar = new wxMenuBar();
|
2000-01-26 01:54:14 +00:00
|
|
|
m_menuBar->Append(m_menuFile, _("&File"));
|
|
|
|
m_menuBar->Append(m_menuSocket, _("&SocketClient"));
|
|
|
|
m_menuBar->Append(m_menuDatagramSocket, _("&DatagramSocket"));
|
2005-02-17 13:13:01 +00:00
|
|
|
#if wxUSE_URL
|
2000-01-26 01:54:14 +00:00
|
|
|
m_menuBar->Append(m_menuProtocols, _("&Protocols"));
|
2005-02-17 13:13:01 +00:00
|
|
|
#endif
|
1999-09-30 23:56:10 +00:00
|
|
|
SetMenuBar(m_menuBar);
|
|
|
|
|
2004-07-19 15:36:01 +00:00
|
|
|
#if wxUSE_STATUSBAR
|
1999-09-30 23:56:10 +00:00
|
|
|
// Status bar
|
1998-09-06 18:28:00 +00:00
|
|
|
CreateStatusBar(2);
|
2004-07-19 15:36:01 +00:00
|
|
|
#endif // wxUSE_STATUSBAR
|
1999-09-30 23:56:10 +00:00
|
|
|
|
2000-03-04 02:51:45 +00:00
|
|
|
// Make a textctrl for logging
|
2004-06-02 17:03:20 +00:00
|
|
|
m_text = new wxTextCtrl(this, wxID_ANY,
|
2001-09-21 20:21:44 +00:00
|
|
|
_("Welcome to wxSocket demo: Client\nClient ready\n"),
|
2000-03-04 02:51:45 +00:00
|
|
|
wxDefaultPosition, wxDefaultSize,
|
1999-09-30 23:56:10 +00:00
|
|
|
wxTE_MULTILINE | wxTE_READONLY);
|
|
|
|
|
|
|
|
// Create the socket
|
|
|
|
m_sock = new wxSocketClient();
|
2000-03-04 02:51:45 +00:00
|
|
|
|
|
|
|
// Setup the event handler and subscribe to most events
|
1999-09-30 23:56:10 +00:00
|
|
|
m_sock->SetEventHandler(*this, SOCKET_ID);
|
|
|
|
m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG |
|
|
|
|
wxSOCKET_INPUT_FLAG |
|
|
|
|
wxSOCKET_LOST_FLAG);
|
2004-06-02 17:03:20 +00:00
|
|
|
m_sock->Notify(true);
|
1999-09-30 23:56:10 +00:00
|
|
|
|
2004-06-02 17:03:20 +00:00
|
|
|
m_busy = false;
|
1999-09-30 23:56:10 +00:00
|
|
|
UpdateStatusBar();
|
1998-09-06 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MyFrame::~MyFrame()
|
|
|
|
{
|
2000-03-04 02:51:45 +00:00
|
|
|
// No delayed deletion here, as the frame is dying anyway
|
1999-09-30 23:56:10 +00:00
|
|
|
delete m_sock;
|
1998-09-06 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// event handlers
|
|
|
|
|
|
|
|
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
1998-09-06 18:28:00 +00:00
|
|
|
{
|
2004-06-02 17:03:20 +00:00
|
|
|
// true is to force the frame to close
|
|
|
|
Close(true);
|
1998-09-06 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
2001-09-21 20:21:44 +00:00
|
|
|
wxMessageBox(_("wxSocket demo: Client\n(c) 1999 Guillermo Rodriguez Garcia\n"),
|
2000-01-26 01:54:14 +00:00
|
|
|
_("About Client"),
|
1999-09-30 23:56:10 +00:00
|
|
|
wxOK | wxICON_INFORMATION, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnOpenConnection(wxCommandEvent& WXUNUSED(event))
|
1998-09-06 18:28:00 +00:00
|
|
|
{
|
|
|
|
wxIPV4address addr;
|
|
|
|
|
2004-06-02 17:03:20 +00:00
|
|
|
m_menuSocket->Enable(CLIENT_OPEN, false);
|
|
|
|
m_menuSocket->Enable(CLIENT_CLOSE, false);
|
1999-09-30 23:56:10 +00:00
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
// Ask user for server address
|
1999-09-30 23:56:10 +00:00
|
|
|
wxString hostname = wxGetTextFromUser(
|
2000-01-26 01:54:14 +00:00
|
|
|
_("Enter the address of the wxSocket demo server:"),
|
|
|
|
_("Connect ..."),
|
|
|
|
_("localhost"));
|
1999-09-30 23:56:10 +00:00
|
|
|
|
|
|
|
addr.Hostname(hostname);
|
1998-09-06 18:28:00 +00:00
|
|
|
addr.Service(3000);
|
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
// Mini-tutorial for Connect() :-)
|
|
|
|
// ---------------------------
|
|
|
|
//
|
|
|
|
// There are two ways to use Connect(): blocking and non-blocking,
|
|
|
|
// depending on the value passed as the 'wait' (2nd) parameter.
|
|
|
|
//
|
2004-06-02 17:03:20 +00:00
|
|
|
// Connect(addr, true) will wait until the connection completes,
|
|
|
|
// returning true on success and false on failure. This call blocks
|
2000-01-26 01:54:14 +00:00
|
|
|
// the GUI (this might be changed in future releases to honour the
|
|
|
|
// wxSOCKET_BLOCK flag).
|
|
|
|
//
|
2004-06-02 17:03:20 +00:00
|
|
|
// Connect(addr, false) will issue a nonblocking connection request
|
|
|
|
// and return immediately. If the return value is true, then the
|
2000-01-26 01:54:14 +00:00
|
|
|
// connection has been already succesfully established. If it is
|
2004-06-02 17:03:20 +00:00
|
|
|
// false, you must wait for the request to complete, either with
|
2000-01-26 01:54:14 +00:00
|
|
|
// WaitOnConnect() or by watching wxSOCKET_CONNECTION / LOST
|
|
|
|
// events (please read the documentation).
|
|
|
|
//
|
|
|
|
// WaitOnConnect() itself never blocks the GUI (this might change
|
|
|
|
// in the future to honour the wxSOCKET_BLOCK flag). This call will
|
2004-06-02 17:03:20 +00:00
|
|
|
// return false on timeout, or true if the connection request
|
2000-03-04 02:51:45 +00:00
|
|
|
// completes, which in turn might mean:
|
|
|
|
//
|
|
|
|
// a) That the connection was successfully established
|
|
|
|
// b) That the connection request failed (for example, because
|
|
|
|
// it was refused by the peer.
|
|
|
|
//
|
2000-01-26 01:54:14 +00:00
|
|
|
// Use IsConnected() to distinguish between these two.
|
|
|
|
//
|
|
|
|
// So, in a brief, you should do one of the following things:
|
|
|
|
//
|
|
|
|
// For blocking Connect:
|
|
|
|
//
|
2004-06-02 17:03:20 +00:00
|
|
|
// bool success = client->Connect(addr, true);
|
2000-01-26 01:54:14 +00:00
|
|
|
//
|
|
|
|
// For nonblocking Connect:
|
|
|
|
//
|
2004-06-02 17:03:20 +00:00
|
|
|
// client->Connect(addr, false);
|
2000-03-04 02:51:45 +00:00
|
|
|
//
|
2004-06-02 17:03:20 +00:00
|
|
|
// bool waitmore = true;
|
2000-03-04 13:20:35 +00:00
|
|
|
// while (! client->WaitOnConnect(seconds, millis) && waitmore )
|
2000-03-04 02:51:45 +00:00
|
|
|
// {
|
|
|
|
// // possibly give some feedback to the user,
|
|
|
|
// // update waitmore if needed.
|
|
|
|
// }
|
2000-03-04 13:20:35 +00:00
|
|
|
// bool success = client->IsConnected();
|
2004-10-06 20:54:57 +00:00
|
|
|
//
|
2000-01-26 01:54:14 +00:00
|
|
|
// And that's all :-)
|
2000-03-03 19:22:35 +00:00
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("\nTrying to connect (timeout = 10 sec) ...\n"));
|
2004-06-02 17:03:20 +00:00
|
|
|
m_sock->Connect(addr, false);
|
2000-01-26 01:54:14 +00:00
|
|
|
m_sock->WaitOnConnect(10);
|
1999-09-30 23:56:10 +00:00
|
|
|
|
|
|
|
if (m_sock->IsConnected())
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Succeeded ! Connection established\n"));
|
1999-09-30 23:56:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_sock->Close();
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Failed ! Unable to connect\n"));
|
|
|
|
wxMessageBox(_("Can't connect to the specified host"), _("Alert !"));
|
1999-09-30 23:56:10 +00:00
|
|
|
}
|
2004-10-06 20:54:57 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
UpdateStatusBar();
|
1998-09-06 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
|
1998-09-06 18:28:00 +00:00
|
|
|
{
|
2001-09-21 20:21:44 +00:00
|
|
|
const wxChar *buf1;
|
|
|
|
wxChar *buf2;
|
2000-01-21 03:16:02 +00:00
|
|
|
unsigned char len;
|
1999-09-30 23:56:10 +00:00
|
|
|
|
|
|
|
// Disable socket menu entries (exception: Close Session)
|
2004-06-02 17:03:20 +00:00
|
|
|
m_busy = true;
|
1999-09-30 23:56:10 +00:00
|
|
|
UpdateStatusBar();
|
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("\n=== Test 1 begins ===\n"));
|
1999-09-30 23:56:10 +00:00
|
|
|
|
|
|
|
// Tell the server which test we are running
|
2000-03-15 13:29:07 +00:00
|
|
|
unsigned char c = 0xBE;
|
1999-09-30 23:56:10 +00:00
|
|
|
m_sock->Write(&c, 1);
|
|
|
|
|
|
|
|
// Send some data and read it back. We know the size of the
|
|
|
|
// buffer, so we can specify the exact number of bytes to be
|
2000-01-21 03:16:02 +00:00
|
|
|
// sent or received and use the wxSOCKET_WAITALL flag. Also,
|
|
|
|
// we have disabled menu entries which could interfere with
|
|
|
|
// the test, so we can safely avoid the wxSOCKET_BLOCK flag.
|
1999-09-30 23:56:10 +00:00
|
|
|
//
|
|
|
|
// First we send a byte with the length of the string, then
|
|
|
|
// we send the string itself (do NOT try to send any integral
|
2000-01-21 03:16:02 +00:00
|
|
|
// value larger than a byte "as is" across the network, or
|
1999-09-30 23:56:10 +00:00
|
|
|
// you might be in trouble! Ever heard about big and little
|
|
|
|
// endian computers?)
|
2000-03-03 19:22:35 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
m_sock->SetFlags(wxSOCKET_WAITALL);
|
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
buf1 = _("Test string (less than 256 chars!)");
|
2004-10-06 20:54:57 +00:00
|
|
|
len = (unsigned char)((wxStrlen(buf1) + 1) * sizeof(wxChar));
|
2001-09-21 20:21:44 +00:00
|
|
|
buf2 = new wxChar[wxStrlen(buf1) + 1];
|
1999-09-30 23:56:10 +00:00
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Sending a test buffer to the server ..."));
|
2000-03-08 08:28:20 +00:00
|
|
|
m_sock->Write(&len, 1);
|
1999-09-30 23:56:10 +00:00
|
|
|
m_sock->Write(buf1, len);
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
|
1999-09-30 23:56:10 +00:00
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Receiving the buffer back from server ..."));
|
1999-09-30 23:56:10 +00:00
|
|
|
m_sock->Read(buf2, len);
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
|
1999-09-30 23:56:10 +00:00
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Comparing the two buffers ..."));
|
1999-09-30 23:56:10 +00:00
|
|
|
if (memcmp(buf1, buf2, len) != 0)
|
|
|
|
{
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("failed!\n"));
|
|
|
|
m_text->AppendText(_("Test 1 failed !\n"));
|
1999-09-30 23:56:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("done\n"));
|
|
|
|
m_text->AppendText(_("Test 1 passed !\n"));
|
1999-09-30 23:56:10 +00:00
|
|
|
}
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("=== Test 1 ends ===\n"));
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
delete[] buf2;
|
2004-06-02 17:03:20 +00:00
|
|
|
m_busy = false;
|
1999-09-30 23:56:10 +00:00
|
|
|
UpdateStatusBar();
|
|
|
|
}
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
2001-09-21 20:21:44 +00:00
|
|
|
const wxChar *msg1;
|
|
|
|
wxChar *msg2;
|
1999-09-30 23:56:10 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
// Disable socket menu entries (exception: Close Session)
|
2004-06-02 17:03:20 +00:00
|
|
|
m_busy = true;
|
1999-09-30 23:56:10 +00:00
|
|
|
UpdateStatusBar();
|
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("\n=== Test 2 begins ===\n"));
|
1999-09-30 23:56:10 +00:00
|
|
|
|
|
|
|
// Tell the server which test we are running
|
2000-03-15 13:29:07 +00:00
|
|
|
unsigned char c = 0xCE;
|
1999-09-30 23:56:10 +00:00
|
|
|
m_sock->Write(&c, 1);
|
|
|
|
|
|
|
|
// Here we use ReadMsg and WriteMsg to send messages with
|
|
|
|
// a header with size information. Also, the reception is
|
|
|
|
// event triggered, so we test input events as well.
|
|
|
|
//
|
|
|
|
// We need to set no flags here (ReadMsg and WriteMsg are
|
|
|
|
// not affected by flags)
|
2000-03-03 19:22:35 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
m_sock->SetFlags(wxSOCKET_WAITALL);
|
|
|
|
|
|
|
|
wxString s = wxGetTextFromUser(
|
2000-01-26 01:54:14 +00:00
|
|
|
_("Enter an arbitrary string to send to the server:"),
|
|
|
|
_("Test 2 ..."),
|
2004-05-25 11:20:37 +00:00
|
|
|
_("Yes I like wxWidgets!"));
|
1999-09-30 23:56:10 +00:00
|
|
|
|
2001-09-21 20:21:44 +00:00
|
|
|
msg1 = s.c_str();
|
|
|
|
len = (wxStrlen(msg1) + 1) * sizeof(wxChar);
|
|
|
|
msg2 = new wxChar[wxStrlen(msg1) + 1];
|
1999-09-30 23:56:10 +00:00
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Sending the string with WriteMsg ..."));
|
1999-09-30 23:56:10 +00:00
|
|
|
m_sock->WriteMsg(msg1, len);
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
|
|
|
|
m_text->AppendText(_("Waiting for an event (timeout = 2 sec)\n"));
|
1999-09-30 23:56:10 +00:00
|
|
|
|
|
|
|
// Wait until data available (will also return if the connection is lost)
|
|
|
|
m_sock->WaitForRead(2);
|
|
|
|
|
|
|
|
if (m_sock->IsData())
|
|
|
|
{
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Reading the string back with ReadMsg ..."));
|
1999-09-30 23:56:10 +00:00
|
|
|
m_sock->ReadMsg(msg2, len);
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
|
|
|
|
m_text->AppendText(_("Comparing the two buffers ..."));
|
1999-09-30 23:56:10 +00:00
|
|
|
if (memcmp(msg1, msg2, len) != 0)
|
|
|
|
{
|
2004-10-06 20:54:57 +00:00
|
|
|
m_text->AppendText(_("failed!\n"));
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Test 2 failed !\n"));
|
1999-09-30 23:56:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("done\n"));
|
|
|
|
m_text->AppendText(_("Test 2 passed !\n"));
|
1999-09-30 23:56:10 +00:00
|
|
|
}
|
1999-09-02 19:11:38 +00:00
|
|
|
}
|
1999-09-30 23:56:10 +00:00
|
|
|
else
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Timeout ! Test 2 failed.\n"));
|
1999-09-02 19:11:38 +00:00
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("=== Test 2 ends ===\n"));
|
1999-09-02 19:11:38 +00:00
|
|
|
|
2000-01-21 03:16:02 +00:00
|
|
|
delete[] msg2;
|
2004-06-02 17:03:20 +00:00
|
|
|
m_busy = false;
|
1999-09-30 23:56:10 +00:00
|
|
|
UpdateStatusBar();
|
1999-09-02 19:11:38 +00:00
|
|
|
}
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
|
1998-09-06 18:28:00 +00:00
|
|
|
{
|
2000-01-21 03:16:02 +00:00
|
|
|
char *buf1;
|
|
|
|
char *buf2;
|
|
|
|
unsigned char len;
|
|
|
|
|
|
|
|
// Disable socket menu entries (exception: Close Session)
|
2004-06-02 17:03:20 +00:00
|
|
|
m_busy = true;
|
2000-01-21 03:16:02 +00:00
|
|
|
UpdateStatusBar();
|
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("\n=== Test 3 begins ===\n"));
|
2000-01-21 03:16:02 +00:00
|
|
|
|
|
|
|
// Tell the server which test we are running
|
2000-03-15 13:29:07 +00:00
|
|
|
unsigned char c = 0xDE;
|
2000-01-21 03:16:02 +00:00
|
|
|
m_sock->Write(&c, 1);
|
|
|
|
|
|
|
|
// This test also is similar to the first one but it sends a
|
|
|
|
// large buffer so that wxSocket is actually forced to split
|
|
|
|
// it into pieces and take care of sending everything before
|
|
|
|
// returning.
|
2000-03-03 19:22:35 +00:00
|
|
|
|
2000-01-21 03:16:02 +00:00
|
|
|
m_sock->SetFlags(wxSOCKET_WAITALL);
|
|
|
|
|
|
|
|
// Note that len is in kbytes here!
|
2000-03-15 13:29:07 +00:00
|
|
|
len = 32;
|
2000-01-21 03:16:02 +00:00
|
|
|
buf1 = new char[len * 1024];
|
|
|
|
buf2 = new char[len * 1024];
|
|
|
|
|
|
|
|
for (int i = 0; i < len * 1024; i ++)
|
|
|
|
buf1[i] = (char)(i % 256);
|
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Sending a large buffer (32K) to the server ..."));
|
2000-03-08 08:28:20 +00:00
|
|
|
m_sock->Write(&len, 1);
|
2000-01-21 03:16:02 +00:00
|
|
|
m_sock->Write(buf1, len * 1024);
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
|
2000-01-21 03:16:02 +00:00
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Receiving the buffer back from server ..."));
|
2000-01-21 03:16:02 +00:00
|
|
|
m_sock->Read(buf2, len * 1024);
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n"));
|
2000-01-21 03:16:02 +00:00
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("Comparing the two buffers ..."));
|
2000-01-21 03:16:02 +00:00
|
|
|
if (memcmp(buf1, buf2, len) != 0)
|
|
|
|
{
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("failed!\n"));
|
|
|
|
m_text->AppendText(_("Test 3 failed !\n"));
|
2000-01-21 03:16:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("done\n"));
|
|
|
|
m_text->AppendText(_("Test 3 passed !\n"));
|
2000-01-21 03:16:02 +00:00
|
|
|
}
|
2000-01-26 01:54:14 +00:00
|
|
|
m_text->AppendText(_("=== Test 3 ends ===\n"));
|
2000-01-21 03:16:02 +00:00
|
|
|
|
|
|
|
delete[] buf2;
|
2004-06-02 17:03:20 +00:00
|
|
|
m_busy = false;
|
2000-01-21 03:16:02 +00:00
|
|
|
UpdateStatusBar();
|
1998-09-06 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
void MyFrame::OnCloseConnection(wxCommandEvent& WXUNUSED(event))
|
1998-09-06 18:28:00 +00:00
|
|
|
{
|
1999-09-30 23:56:10 +00:00
|
|
|
m_sock->Close();
|
|
|
|
UpdateStatusBar();
|
1998-09-06 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
2000-01-21 03:16:02 +00:00
|
|
|
void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
2000-03-08 08:28:20 +00:00
|
|
|
m_text->AppendText(_("\n=== Datagram test begins ===\n"));
|
2000-03-04 02:54:56 +00:00
|
|
|
m_text->AppendText(_("Sorry, not implemented\n"));
|
|
|
|
m_text->AppendText(_("=== Datagram test ends ===\n"));
|
2000-01-21 03:16:02 +00:00
|
|
|
}
|
|
|
|
|
2005-02-17 13:13:01 +00:00
|
|
|
#if wxUSE_URL
|
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
void MyFrame::OnTestURL(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
// Note that we are creating a new socket here, so this
|
|
|
|
// won't mess with the client/server demo.
|
|
|
|
|
|
|
|
// Ask for the URL
|
|
|
|
m_text->AppendText(_("\n=== URL test begins ===\n"));
|
|
|
|
wxString urlname = wxGetTextFromUser(_("Enter an URL to get"),
|
|
|
|
_("URL:"),
|
2000-03-15 19:19:44 +00:00
|
|
|
_T("http://localhost"));
|
2000-01-26 01:54:14 +00:00
|
|
|
|
|
|
|
// Parse the URL
|
|
|
|
wxURL url(urlname);
|
2000-02-29 21:02:16 +00:00
|
|
|
if (url.GetError() != wxURL_NOERR)
|
|
|
|
{
|
|
|
|
m_text->AppendText(_("Error: couldn't parse URL\n"));
|
|
|
|
m_text->AppendText(_("=== URL test ends ===\n"));
|
|
|
|
return;
|
|
|
|
}
|
2000-01-26 01:54:14 +00:00
|
|
|
|
|
|
|
// Try to get the input stream (connects to the given URL)
|
|
|
|
m_text->AppendText(_("Trying to establish connection...\n"));
|
|
|
|
wxYield();
|
|
|
|
wxInputStream *data = url.GetInputStream();
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
m_text->AppendText(_("Error: couldn't read from URL\n"));
|
|
|
|
m_text->AppendText(_("=== URL test ends ===\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the contents type and file size
|
|
|
|
wxString s;
|
2001-09-21 20:21:44 +00:00
|
|
|
s.Printf(_("Contents type: %s\nFile size: %i\nStarting to download...\n"),
|
2000-01-26 01:54:14 +00:00
|
|
|
url.GetProtocol().GetContentType().c_str(),
|
|
|
|
data->GetSize());
|
|
|
|
m_text->AppendText(s);
|
|
|
|
wxYield();
|
|
|
|
|
|
|
|
// Get the data
|
2005-05-22 14:52:06 +00:00
|
|
|
wxFile fileTest(wxT("test.url"), wxFile::write);
|
2003-08-04 11:18:02 +00:00
|
|
|
wxFileOutputStream sout(fileTest);
|
2000-02-29 21:02:16 +00:00
|
|
|
if (!sout.Ok())
|
|
|
|
{
|
|
|
|
m_text->AppendText(_("Error: couldn't open file for output\n"));
|
|
|
|
m_text->AppendText(_("=== URL test ends ===\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-01-26 01:54:14 +00:00
|
|
|
data->Read(sout);
|
|
|
|
m_text->AppendText(_("Results written to file: test.url\n"));
|
|
|
|
m_text->AppendText(_("Done.\n"));
|
|
|
|
m_text->AppendText(_("=== URL test ends ===\n"));
|
|
|
|
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
|
2005-02-17 13:13:01 +00:00
|
|
|
#endif
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
void MyFrame::OnSocketEvent(wxSocketEvent& event)
|
1998-09-06 18:28:00 +00:00
|
|
|
{
|
2000-01-26 01:54:14 +00:00
|
|
|
wxString s = _("OnSocketEvent: ");
|
1999-09-30 23:56:10 +00:00
|
|
|
|
2000-03-14 09:56:38 +00:00
|
|
|
switch(event.GetSocketEvent())
|
1999-09-30 23:56:10 +00:00
|
|
|
{
|
2000-01-26 01:54:14 +00:00
|
|
|
case wxSOCKET_INPUT : s.Append(_("wxSOCKET_INPUT\n")); break;
|
|
|
|
case wxSOCKET_LOST : s.Append(_("wxSOCKET_LOST\n")); break;
|
|
|
|
case wxSOCKET_CONNECTION : s.Append(_("wxSOCKET_CONNECTION\n")); break;
|
|
|
|
default : s.Append(_("Unexpected event !\n")); break;
|
1999-09-30 23:56:10 +00:00
|
|
|
}
|
1998-09-06 18:28:00 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
m_text->AppendText(s);
|
|
|
|
UpdateStatusBar();
|
1998-09-06 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
// convenience functions
|
1999-07-22 17:51:54 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
void MyFrame::UpdateStatusBar()
|
1999-07-22 17:51:54 +00:00
|
|
|
{
|
1999-09-30 23:56:10 +00:00
|
|
|
wxString s;
|
1999-07-22 17:51:54 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
if (!m_sock->IsConnected())
|
|
|
|
{
|
2000-01-26 01:54:14 +00:00
|
|
|
s.Printf(_("Not connected"));
|
1999-07-22 17:51:54 +00:00
|
|
|
}
|
1999-09-30 23:56:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
wxIPV4address addr;
|
1999-07-22 17:51:54 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
m_sock->GetPeer(addr);
|
2000-01-26 01:54:14 +00:00
|
|
|
s.Printf(_("%s : %d"), (addr.Hostname()).c_str(), addr.Service());
|
1999-09-30 23:56:10 +00:00
|
|
|
}
|
1998-09-06 18:28:00 +00:00
|
|
|
|
2004-07-19 15:36:01 +00:00
|
|
|
#if wxUSE_STATUSBAR
|
1999-09-30 23:56:10 +00:00
|
|
|
SetStatusText(s, 1);
|
2004-07-19 15:36:01 +00:00
|
|
|
#endif // wxUSE_STATUSBAR
|
1999-04-26 18:16:56 +00:00
|
|
|
|
1999-09-30 23:56:10 +00:00
|
|
|
m_menuSocket->Enable(CLIENT_OPEN, !m_sock->IsConnected() && !m_busy);
|
|
|
|
m_menuSocket->Enable(CLIENT_TEST1, m_sock->IsConnected() && !m_busy);
|
|
|
|
m_menuSocket->Enable(CLIENT_TEST2, m_sock->IsConnected() && !m_busy);
|
|
|
|
m_menuSocket->Enable(CLIENT_TEST3, m_sock->IsConnected() && !m_busy);
|
|
|
|
m_menuSocket->Enable(CLIENT_CLOSE, m_sock->IsConnected());
|
1998-09-06 18:28:00 +00:00
|
|
|
}
|