added UDP test (see #10717)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60574 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -227,7 +227,7 @@ MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY,
|
|||||||
m_menuSocket->Append(CLIENT_CLOSE, _("&Close session\tCtrl-Q"), _("Close connection"));
|
m_menuSocket->Append(CLIENT_CLOSE, _("&Close session\tCtrl-Q"), _("Close connection"));
|
||||||
|
|
||||||
m_menuDatagramSocket = new wxMenu();
|
m_menuDatagramSocket = new wxMenu();
|
||||||
m_menuDatagramSocket->Append(CLIENT_DGRAM, _("Send Datagram"), _("Test UDP sockets"));
|
m_menuDatagramSocket->Append(CLIENT_DGRAM, _("&Datagram test\tCtrl-D"), _("Test UDP sockets"));
|
||||||
|
|
||||||
#if wxUSE_URL
|
#if wxUSE_URL
|
||||||
m_menuProtocols = new wxMenu();
|
m_menuProtocols = new wxMenu();
|
||||||
@@ -238,8 +238,8 @@ MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY,
|
|||||||
// Append menus to the menubar
|
// Append menus to the menubar
|
||||||
m_menuBar = new wxMenuBar();
|
m_menuBar = new wxMenuBar();
|
||||||
m_menuBar->Append(m_menuFile, _("&File"));
|
m_menuBar->Append(m_menuFile, _("&File"));
|
||||||
m_menuBar->Append(m_menuSocket, _("&SocketClient"));
|
m_menuBar->Append(m_menuSocket, _("&TCP"));
|
||||||
m_menuBar->Append(m_menuDatagramSocket, _("&DatagramSocket"));
|
m_menuBar->Append(m_menuDatagramSocket, _("&UDP"));
|
||||||
#if wxUSE_URL
|
#if wxUSE_URL
|
||||||
m_menuBar->Append(m_menuProtocols, _("&Protocols"));
|
m_menuBar->Append(m_menuProtocols, _("&Protocols"));
|
||||||
#endif
|
#endif
|
||||||
@@ -328,6 +328,8 @@ void MyFrame::OpenConnection(wxSockAddress::Family family)
|
|||||||
_("Enter the address of the wxSocket demo server:"),
|
_("Enter the address of the wxSocket demo server:"),
|
||||||
_("Connect ..."),
|
_("Connect ..."),
|
||||||
_("localhost"));
|
_("localhost"));
|
||||||
|
if ( hostname.empty() )
|
||||||
|
return;
|
||||||
|
|
||||||
addr->Hostname(hostname);
|
addr->Hostname(hostname);
|
||||||
addr->Service(3000);
|
addr->Service(3000);
|
||||||
@@ -578,9 +580,52 @@ void MyFrame::OnCloseConnection(wxCommandEvent& WXUNUSED(event))
|
|||||||
|
|
||||||
void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event))
|
void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event))
|
||||||
{
|
{
|
||||||
m_text->AppendText(_("\n=== Datagram test begins ===\n"));
|
wxString hostname = wxGetTextFromUser
|
||||||
m_text->AppendText(_("Sorry, not implemented\n"));
|
(
|
||||||
m_text->AppendText(_("=== Datagram test ends ===\n"));
|
"Enter the address of the wxSocket demo server:",
|
||||||
|
"UDP peer",
|
||||||
|
"localhost"
|
||||||
|
);
|
||||||
|
if ( hostname.empty() )
|
||||||
|
return;
|
||||||
|
|
||||||
|
TestLogger logtest("UDP");
|
||||||
|
|
||||||
|
wxIPV4address addrLocal;
|
||||||
|
addrLocal.Hostname();
|
||||||
|
wxDatagramSocket sock(addrLocal);
|
||||||
|
if ( !sock.IsOk() )
|
||||||
|
{
|
||||||
|
wxLogMessage("ERROR: failed to create UDP socket");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wxLogMessage("Created UDP socket at %s:%u",
|
||||||
|
addrLocal.IPAddress(), addrLocal.Service());
|
||||||
|
|
||||||
|
wxIPV4address addrPeer;
|
||||||
|
addrPeer.Hostname(hostname);
|
||||||
|
addrPeer.Service(3000);
|
||||||
|
|
||||||
|
wxLogMessage("Testing UDP with peer at %s:%u",
|
||||||
|
addrPeer.IPAddress(), addrPeer.Service());
|
||||||
|
|
||||||
|
char buf[] = "Uryyb sebz pyvrag!";
|
||||||
|
if ( sock.SendTo(addrPeer, buf, sizeof(buf)).LastCount() != sizeof(buf) )
|
||||||
|
{
|
||||||
|
wxLogMessage("ERROR: failed to send data");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( sock.RecvFrom(addrPeer, buf, sizeof(buf)).LastCount() != sizeof(buf) )
|
||||||
|
{
|
||||||
|
wxLogMessage("ERROR: failed to receive data");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wxLogMessage("Received \"%s\" from %s:%u.",
|
||||||
|
wxString::From8BitData(buf, sock.LastCount()),
|
||||||
|
addrPeer.IPAddress(), addrPeer.Service());
|
||||||
}
|
}
|
||||||
|
|
||||||
#if wxUSE_URL
|
#if wxUSE_URL
|
||||||
|
@@ -65,6 +65,7 @@ public:
|
|||||||
~MyFrame();
|
~MyFrame();
|
||||||
|
|
||||||
// event handlers (these functions should _not_ be virtual)
|
// event handlers (these functions should _not_ be virtual)
|
||||||
|
void OnUDPTest(wxCommandEvent& event);
|
||||||
void OnQuit(wxCommandEvent& event);
|
void OnQuit(wxCommandEvent& event);
|
||||||
void OnAbout(wxCommandEvent& event);
|
void OnAbout(wxCommandEvent& event);
|
||||||
void OnServerEvent(wxSocketEvent& event);
|
void OnServerEvent(wxSocketEvent& event);
|
||||||
@@ -115,6 +116,7 @@ private:
|
|||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
// menu items
|
// menu items
|
||||||
|
SERVER_UDPTEST = 10,
|
||||||
SERVER_QUIT = wxID_EXIT,
|
SERVER_QUIT = wxID_EXIT,
|
||||||
SERVER_ABOUT = wxID_ABOUT,
|
SERVER_ABOUT = wxID_ABOUT,
|
||||||
|
|
||||||
@@ -130,6 +132,7 @@ enum
|
|||||||
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
||||||
EVT_MENU(SERVER_QUIT, MyFrame::OnQuit)
|
EVT_MENU(SERVER_QUIT, MyFrame::OnQuit)
|
||||||
EVT_MENU(SERVER_ABOUT, MyFrame::OnAbout)
|
EVT_MENU(SERVER_ABOUT, MyFrame::OnAbout)
|
||||||
|
EVT_MENU(SERVER_UDPTEST, MyFrame::OnUDPTest)
|
||||||
EVT_SOCKET(SERVER_ID, MyFrame::OnServerEvent)
|
EVT_SOCKET(SERVER_ID, MyFrame::OnServerEvent)
|
||||||
EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent)
|
EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent)
|
||||||
END_EVENT_TABLE()
|
END_EVENT_TABLE()
|
||||||
@@ -176,6 +179,8 @@ MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY,
|
|||||||
|
|
||||||
// Make menus
|
// Make menus
|
||||||
m_menuFile = new wxMenu();
|
m_menuFile = new wxMenu();
|
||||||
|
m_menuFile->Append(SERVER_UDPTEST, "&UDP test\tCtrl-U");
|
||||||
|
m_menuFile->AppendSeparator();
|
||||||
m_menuFile->Append(SERVER_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog"));
|
m_menuFile->Append(SERVER_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog"));
|
||||||
m_menuFile->AppendSeparator();
|
m_menuFile->AppendSeparator();
|
||||||
m_menuFile->Append(SERVER_QUIT, _("E&xit\tAlt-X"), _("Quit server"));
|
m_menuFile->Append(SERVER_QUIT, _("E&xit\tAlt-X"), _("Quit server"));
|
||||||
@@ -251,6 +256,42 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
|||||||
wxOK | wxICON_INFORMATION, this);
|
wxOK | wxICON_INFORMATION, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MyFrame::OnUDPTest(wxCommandEvent& WXUNUSED(event))
|
||||||
|
{
|
||||||
|
TestLogger logtest("UDP test");
|
||||||
|
|
||||||
|
IPaddress addr;
|
||||||
|
addr.Service(3000);
|
||||||
|
wxDatagramSocket sock(addr);
|
||||||
|
|
||||||
|
char buf[1024];
|
||||||
|
size_t n = sock.RecvFrom(addr, buf, sizeof(buf)).LastCount();
|
||||||
|
if ( !n )
|
||||||
|
{
|
||||||
|
wxLogMessage("ERROR: failed to receive data");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wxLogMessage("Received \"%s\" from %s:%u.",
|
||||||
|
wxString::From8BitData(buf, n),
|
||||||
|
addr.IPAddress(), addr.Service());
|
||||||
|
|
||||||
|
for ( size_t i = 0; i < n; i++ )
|
||||||
|
{
|
||||||
|
char& c = buf[i];
|
||||||
|
if ( (c >= 'A' && c <= 'M') || (c >= 'a' && c <= 'm') )
|
||||||
|
c += 13;
|
||||||
|
else if ( (c >= 'N' && c <= 'Z') || (c >= 'n' && c <= 'z') )
|
||||||
|
c -= 13;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( sock.SendTo(addr, buf, n).LastCount() != n )
|
||||||
|
{
|
||||||
|
wxLogMessage("ERROR: failed to send data");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void MyFrame::Test1(wxSocketBase *sock)
|
void MyFrame::Test1(wxSocketBase *sock)
|
||||||
{
|
{
|
||||||
TestLogger logtest("Test 1");
|
TestLogger logtest("Test 1");
|
||||||
|
Reference in New Issue
Block a user