X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/70d26c3f4ffb24d50457d405c9595fd23f9e5b7c..f9a3f576cccfd2c2296a24d74b05b9543c47fac7:/samples/sockets/client.cpp diff --git a/samples/sockets/client.cpp b/samples/sockets/client.cpp index 2dba5c91b3..ec02f7eada 100644 --- a/samples/sockets/client.cpp +++ b/samples/sockets/client.cpp @@ -34,17 +34,16 @@ # include "wx/wx.h" #endif -# include "wx/socket.h" -# include "wx/url.h" -# include "wx/protocol/http.h" -# include "wx/progdlg.h" +#include "wx/socket.h" +#include "wx/url.h" +#include "wx/wfstream.h" // -------------------------------------------------------------------------- // resources // -------------------------------------------------------------------------- // the application icon -#if defined(__WXGTK__) || defined(__WXMOTIF__) +#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) # include "mondrian.xpm" #endif @@ -66,14 +65,24 @@ public: MyFrame(); ~MyFrame(); - // event handlers (these functions should _not_ be virtual) + // event handlers for File menu void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); + + // event handlers for Socket menu void OnOpenConnection(wxCommandEvent& event); void OnTest1(wxCommandEvent& event); void OnTest2(wxCommandEvent& event); void OnTest3(wxCommandEvent& event); void OnCloseConnection(wxCommandEvent& event); + + // event handlers for Protocols menu + void OnTestURL(wxCommandEvent& event); + + // event handlers for DatagramSocket menu (stub) + void OnDatagram(wxCommandEvent& event); + + // socket event handler void OnSocketEvent(wxSocketEvent& event); // convenience functions @@ -81,10 +90,11 @@ public: private: wxSocketClient *m_sock; - wxPanel *m_panel; wxTextCtrl *m_text; wxMenu *m_menuFile; wxMenu *m_menuSocket; + wxMenu *m_menuDatagramSocket; + wxMenu *m_menuProtocols; wxMenuBar *m_menuBar; bool m_busy; @@ -107,6 +117,8 @@ enum CLIENT_TEST2, CLIENT_TEST3, CLIENT_CLOSE, + CLIENT_TESTURL, + CLIENT_DGRAM, // id for socket SOCKET_ID @@ -117,14 +129,16 @@ enum // -------------------------------------------------------------------------- BEGIN_EVENT_TABLE(MyFrame, wxFrame) - 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_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent) + 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) + EVT_MENU(CLIENT_TESTURL, MyFrame::OnTestURL) + EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent) END_EVENT_TABLE() IMPLEMENT_APP(MyApp) @@ -156,7 +170,7 @@ bool MyApp::OnInit() // frame constructor MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, -1, - _T("wxSocket demo: Client"), + _("wxSocket demo: Client"), wxDefaultPosition, wxSize(300, 200)) { // Give the frame an icon @@ -164,38 +178,47 @@ MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, -1, // Make menus m_menuFile = new wxMenu(); - m_menuFile->Append(CLIENT_ABOUT, _T("&About...\tCtrl-A"), _T("Show about dialog")); + m_menuFile->Append(CLIENT_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog")); m_menuFile->AppendSeparator(); - m_menuFile->Append(CLIENT_QUIT, _T("E&xit\tAlt-X"), _T("Quit client")); + m_menuFile->Append(CLIENT_QUIT, _("E&xit\tAlt-X"), _("Quit client")); m_menuSocket = new wxMenu(); - m_menuSocket->Append(CLIENT_OPEN, _T("&Open session"), _T("Connect to server")); + m_menuSocket->Append(CLIENT_OPEN, _("&Open session"), _("Connect to server")); m_menuSocket->AppendSeparator(); - m_menuSocket->Append(CLIENT_TEST1, _T("Test &1"), _T("Test basic functionality")); - m_menuSocket->Append(CLIENT_TEST2, _T("Test &2"), _T("Test ReadMsg and WriteMsg")); - m_menuSocket->Append(CLIENT_TEST3, _T("Test &3"), _T("Test large data transfer")); + 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")); m_menuSocket->AppendSeparator(); - m_menuSocket->Append(CLIENT_CLOSE, _T("&Close session"), _T("Close connection")); + m_menuSocket->Append(CLIENT_CLOSE, _("&Close session"), _("Close connection")); + + m_menuDatagramSocket = new wxMenu(); + m_menuDatagramSocket->Append(CLIENT_DGRAM, _("Send Datagram"), _("Test UDP sockets")); + + m_menuProtocols = new wxMenu(); + m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL"), _("Get data from the specified URL")); // Append menus to the menubar m_menuBar = new wxMenuBar(); - m_menuBar->Append(m_menuFile, _T("&File")); - m_menuBar->Append(m_menuSocket, _T("&Socket")); + m_menuBar->Append(m_menuFile, _("&File")); + m_menuBar->Append(m_menuSocket, _("&SocketClient")); + m_menuBar->Append(m_menuDatagramSocket, _("&DatagramSocket")); + m_menuBar->Append(m_menuProtocols, _("&Protocols")); SetMenuBar(m_menuBar); // Status bar CreateStatusBar(2); - // Make a panel with a textctrl in it - m_panel = new wxPanel(this, -1, wxPoint(0, 0), GetClientSize()); - m_text = new wxTextCtrl(m_panel, -1, - _T("Welcome to wxSocket demo: Client\n") - _T("Client ready\n\n"), - wxPoint(0, 0), m_panel->GetClientSize(), + // Make a textctrl for logging + m_text = new wxTextCtrl(this, -1, + _("Welcome to wxSocket demo: Client\n" + "Client ready\n"), + wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY); // Create the socket m_sock = new wxSocketClient(); + + // Setup the event handler and subscribe to most events m_sock->SetEventHandler(*this, SOCKET_ID); m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_INPUT_FLAG | @@ -208,6 +231,7 @@ MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, -1, MyFrame::~MyFrame() { + // No delayed deletion here, as the frame is dying anyway delete m_sock; } @@ -221,9 +245,9 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { - wxMessageBox(_T("wxSocket demo: Client\n") - _T("(c) 1999 Guillermo Rodriguez Garcia\n"), - _T("About Client"), + wxMessageBox(_("wxSocket demo: Client\n" + "(c) 1999 Guillermo Rodriguez Garcia\n"), + _("About Client"), wxOK | wxICON_INFORMATION, this); } @@ -234,27 +258,75 @@ void MyFrame::OnOpenConnection(wxCommandEvent& WXUNUSED(event)) m_menuSocket->Enable(CLIENT_OPEN, FALSE); m_menuSocket->Enable(CLIENT_CLOSE, FALSE); - // Ask server address + // Ask user for server address wxString hostname = wxGetTextFromUser( - _T("Enter the address of the wxSocket demo server:"), - _T("Connect ..."), - _T("localhost")); + _("Enter the address of the wxSocket demo server:"), + _("Connect ..."), + _("localhost")); addr.Hostname(hostname); addr.Service(3000); - // Non-blocking connect - m_text->AppendText(_T("Trying to connect (timeout = 10 sec) ...\n")); + // 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. + // + // Connect(addr, TRUE) will wait until the connection completes, + // returning TRUE on success and FALSE on failure. This call blocks + // the GUI (this might be changed in future releases to honour the + // wxSOCKET_BLOCK flag). + // + // Connect(addr, FALSE) will issue a nonblocking connection request + // and return immediately. If the return value is TRUE, then the + // connection has been already succesfully established. If it is + // FALSE, you must wait for the request to complete, either with + // 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 + // return FALSE on timeout, or TRUE if the connection request + // 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. + // + // Use IsConnected() to distinguish between these two. + // + // So, in a brief, you should do one of the following things: + // + // For blocking Connect: + // + // bool success = client->Connect(addr, TRUE); + // + // For nonblocking Connect: + // + // client->Connect(addr, FALSE); + // + // bool waitmore = TRUE; + // while (! client->WaitOnConnect(seconds, millis) && waitmore ) + // { + // // possibly give some feedback to the user, + // // update waitmore if needed. + // } + // bool success = client->IsConnected(); + // + // And that's all :-) + + m_text->AppendText(_("\nTrying to connect (timeout = 10 sec) ...\n")); m_sock->Connect(addr, FALSE); m_sock->WaitOnConnect(10); if (m_sock->IsConnected()) - m_text->AppendText(_T("Succeeded ! Connection established\n")); + m_text->AppendText(_("Succeeded ! Connection established\n")); else { m_sock->Close(); - m_text->AppendText(_T("Failed ! Unable to connect\n")); - wxMessageBox(_T("Can't connect to the specified host"), _T("Alert !")); + m_text->AppendText(_("Failed ! Unable to connect\n")); + wxMessageBox(_("Can't connect to the specified host"), _("Alert !")); } UpdateStatusBar(); @@ -262,58 +334,59 @@ void MyFrame::OnOpenConnection(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event)) { - char *buf1, *buf2; - char len; + const char *buf1; + char *buf2; + unsigned char len; // Disable socket menu entries (exception: Close Session) m_busy = TRUE; UpdateStatusBar(); - m_text->AppendText(_T("\n=== Test 1 begins ===\n")); + m_text->AppendText(_("\n=== Test 1 begins ===\n")); // Tell the server which test we are running - char c = 0xBE; + unsigned char c = 0xBE; 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 - // sent or received and use the WAITALL flag. Also, we have - // disabled menu entries which could interfere with the test, - // so we can safely avoid the BLOCK (formerly SPEED) flag. + // 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. // // First we send a byte with the length of the string, then // we send the string itself (do NOT try to send any integral - // value larger than a byte "as is" acrosss the network, or + // value larger than a byte "as is" across the network, or // you might be in trouble! Ever heard about big and little // endian computers?) - // + m_sock->SetFlags(wxSOCKET_WAITALL); - buf1 = _T("Test string (less than 127 chars!)"); + buf1 = _("Test string (less than 256 chars!)"); len = wxStrlen(buf1) + 1; buf2 = new char[len]; - m_text->AppendText(_T("Sending a test buffer to the server ...")); + m_text->AppendText(_("Sending a test buffer to the server ...")); m_sock->Write(&len, 1); m_sock->Write(buf1, len); - m_text->AppendText(m_sock->Error() ? _T("failed !\n") : _T("done\n")); + m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); - m_text->AppendText(_T("Receiving the buffer back from server ...")); + m_text->AppendText(_("Receiving the buffer back from server ...")); m_sock->Read(buf2, len); - m_text->AppendText(m_sock->Error() ? _T("failed !\n") : _T("done\n")); + m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); - m_text->AppendText(_T("Comparing the two buffers ...")); + m_text->AppendText(_("Comparing the two buffers ...")); if (memcmp(buf1, buf2, len) != 0) { - m_text->AppendText(_T("failed!\n")); - m_text->AppendText(_T("Test 1 failed !\n")); + m_text->AppendText(_("failed!\n")); + m_text->AppendText(_("Test 1 failed !\n")); } else { - m_text->AppendText(_T("done\n")); - m_text->AppendText(_T("Test 1 passed !\n")); + m_text->AppendText(_("done\n")); + m_text->AppendText(_("Test 1 passed !\n")); } - m_text->AppendText(_T("=== Test 1 ends ===\n")); + m_text->AppendText(_("=== Test 1 ends ===\n")); delete[] buf2; m_busy = FALSE; @@ -330,10 +403,10 @@ void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event)) m_busy = TRUE; UpdateStatusBar(); - m_text->AppendText(_T("\n=== Test 2 begins ===\n")); + m_text->AppendText(_("\n=== Test 2 begins ===\n")); // Tell the server which test we are running - char c = 0xCE; + unsigned char c = 0xCE; m_sock->Write(&c, 1); // Here we use ReadMsg and WriteMsg to send messages with @@ -342,58 +415,109 @@ void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event)) // // We need to set no flags here (ReadMsg and WriteMsg are // not affected by flags) - // + m_sock->SetFlags(wxSOCKET_WAITALL); wxString s = wxGetTextFromUser( - _T("Enter an arbitrary string to send to the server:"), - _T("Test 2 ..."), - _T("Yes I like wxWindows!")); + _("Enter an arbitrary string to send to the server:"), + _("Test 2 ..."), + _("Yes I like wxWindows!")); msg1 = (char *)s.c_str(); len = wxStrlen(msg1) + 1; - msg2 = (char *)malloc(len); + msg2 = new char[len]; - m_text->AppendText(_T("Sending the string with WriteMsg ...")); + m_text->AppendText(_("Sending the string with WriteMsg ...")); m_sock->WriteMsg(msg1, len); - m_text->AppendText(m_sock->Error() ? _T("failed !\n") : _T("done\n")); - m_text->AppendText(_T("Waiting for an event (timeout = 2 sec)\n")); + m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); + m_text->AppendText(_("Waiting for an event (timeout = 2 sec)\n")); // Wait until data available (will also return if the connection is lost) m_sock->WaitForRead(2); if (m_sock->IsData()) { - m_text->AppendText(_T("Reading the string back with ReadMsg ...")); + m_text->AppendText(_("Reading the string back with ReadMsg ...")); m_sock->ReadMsg(msg2, len); - m_text->AppendText(m_sock->Error() ? _T("failed !\n") : _T("done\n")); - m_text->AppendText(_T("Comparing the two buffers ...")); + m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); + m_text->AppendText(_("Comparing the two buffers ...")); if (memcmp(msg1, msg2, len) != 0) { - m_text->AppendText(_T("failed!\n")); - m_text->AppendText(_T("Test 2 failed !\n")); + m_text->AppendText(_("failed!\n")); + m_text->AppendText(_("Test 2 failed !\n")); } else { - m_text->AppendText(_T("done\n")); - m_text->AppendText(_T("Test 2 passed !\n")); + m_text->AppendText(_("done\n")); + m_text->AppendText(_("Test 2 passed !\n")); } } else - m_text->AppendText(_T("Timeout ! Test 2 failed.\n")); + m_text->AppendText(_("Timeout ! Test 2 failed.\n")); - m_text->AppendText(_T("=== Test 2 ends ===\n")); + m_text->AppendText(_("=== Test 2 ends ===\n")); - free(msg2); + delete[] msg2; m_busy = FALSE; UpdateStatusBar(); } void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event)) { - m_text->AppendText(_T("\n=== Test 3 begins ===\n")); - m_text->AppendText(_T("Test 3 not implemented\n")); - m_text->AppendText(_T("=== Test 3 ends ===\n")); + char *buf1; + char *buf2; + unsigned char len; + + // Disable socket menu entries (exception: Close Session) + m_busy = TRUE; + UpdateStatusBar(); + + m_text->AppendText(_("\n=== Test 3 begins ===\n")); + + // Tell the server which test we are running + unsigned char c = 0xDE; + 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. + + m_sock->SetFlags(wxSOCKET_WAITALL); + + // Note that len is in kbytes here! + len = 32; + buf1 = new char[len * 1024]; + buf2 = new char[len * 1024]; + + for (int i = 0; i < len * 1024; i ++) + buf1[i] = (char)(i % 256); + + m_text->AppendText(_("Sending a large buffer (32K) to the server ...")); + m_sock->Write(&len, 1); + m_sock->Write(buf1, len * 1024); + m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); + + m_text->AppendText(_("Receiving the buffer back from server ...")); + m_sock->Read(buf2, len * 1024); + m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); + + m_text->AppendText(_("Comparing the two buffers ...")); + if (memcmp(buf1, buf2, len) != 0) + { + m_text->AppendText(_("failed!\n")); + m_text->AppendText(_("Test 3 failed !\n")); + } + else + { + m_text->AppendText(_("done\n")); + m_text->AppendText(_("Test 3 passed !\n")); + } + m_text->AppendText(_("=== Test 3 ends ===\n")); + + delete[] buf2; + m_busy = FALSE; + UpdateStatusBar(); } void MyFrame::OnCloseConnection(wxCommandEvent& WXUNUSED(event)) @@ -402,16 +526,81 @@ void MyFrame::OnCloseConnection(wxCommandEvent& WXUNUSED(event)) UpdateStatusBar(); } +void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event)) +{ + m_text->AppendText(_("\n=== Datagram test begins ===\n")); + m_text->AppendText(_("Sorry, not implemented\n")); + m_text->AppendText(_("=== Datagram test ends ===\n")); +} + +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:"), + _T("http://localhost")); + + // Parse the URL + wxURL url(urlname); + if (url.GetError() != wxURL_NOERR) + { + m_text->AppendText(_("Error: couldn't parse URL\n")); + m_text->AppendText(_("=== URL test ends ===\n")); + return; + } + + // 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; + s.Printf(_("Contents type: %s\n" + "File size: %i\n" + "Starting to download...\n"), + url.GetProtocol().GetContentType().c_str(), + data->GetSize()); + m_text->AppendText(s); + wxYield(); + + // Get the data + wxFileOutputStream sout(wxString("test.url")); + if (!sout.Ok()) + { + m_text->AppendText(_("Error: couldn't open file for output\n")); + m_text->AppendText(_("=== URL test ends ===\n")); + return; + } + + 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; +} + void MyFrame::OnSocketEvent(wxSocketEvent& event) { - wxString s = _T("OnSocketEvent: "); + wxString s = _("OnSocketEvent: "); - switch(event.SocketEvent()) + switch(event.GetSocketEvent()) { - case wxSOCKET_INPUT : s.Append(_T("wxSOCKET_INPUT\n")); break; - case wxSOCKET_LOST : s.Append(_T("wxSOCKET_LOST\n")); break; - case wxSOCKET_CONNECTION : s.Append(_T("wxSOCKET_CONNECTION\n")); break; - default : s.Append(_T("Unexpected event !\n")); break; + 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; } m_text->AppendText(s); @@ -426,14 +615,14 @@ void MyFrame::UpdateStatusBar() if (!m_sock->IsConnected()) { - s.Printf(_T("Not connected")); + s.Printf(_("Not connected")); } else { wxIPV4address addr; m_sock->GetPeer(addr); - s.Printf(_T("%s : %d"), (addr.Hostname()).c_str(), addr.Service()); + s.Printf(_("%s : %d"), (addr.Hostname()).c_str(), addr.Service()); } SetStatusText(s, 1);