]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/console/console.cpp
oops, compilation error - fixed
[wxWidgets.git] / samples / console / console.cpp
index 56a0493e379a8e9074877e0d1ad61d846a948f74..7f639229de7371a58af4bddc09bccf126ea5f1d4 100644 (file)
 //#define TEST_LOG
 //#define TEST_LONGLONG
 //#define TEST_MIME
-//#define TEST_SOCKETS
+#define TEST_SOCKETS
 //#define TEST_STRINGS
 //#define TEST_THREADS
-#define TEST_TIMER
+//#define TEST_TIMER
 
 // ============================================================================
 // implementation
 // ============================================================================
 
+// ----------------------------------------------------------------------------
+// helper functions
+// ----------------------------------------------------------------------------
+
+#if defined(TEST_STRINGS) || defined(TEST_SOCKETS)
+
+// replace TABs with \t and CRs with \n
+static wxString MakePrintable(const wxChar *s)
+{
+    wxString str(s);
+    (void)str.Replace(_T("\t"), _T("\\t"));
+    (void)str.Replace(_T("\n"), _T("\\n"));
+    (void)str.Replace(_T("\r"), _T("\\r"));
+
+    return str;
+}
+
+#endif // MakePrintable() is used
+
 // ----------------------------------------------------------------------------
 // wxCmdLineParser
 // ----------------------------------------------------------------------------
@@ -696,27 +715,199 @@ static void TestBitOperations()
 #ifdef TEST_SOCKETS
 
 #include <wx/socket.h>
+#include <wx/protocol/protocol.h>
+#include <wx/protocol/ftp.h>
+#include <wx/protocol/http.h>
+
+static void TestSocketServer()
+{
+    puts("*** Testing wxSocketServer ***\n");
+
+    static const int PORT = 3000;
+
+    wxIPV4address addr;
+    addr.Service(PORT);
+
+    wxSocketServer *server = new wxSocketServer(addr);
+    if ( !server->Ok() )
+    {
+        puts("ERROR: failed to bind");
+
+        return;
+    }
+
+    for ( ;; )
+    {
+        printf("Server: waiting for connection on port %d...\n", PORT);
+
+        wxSocketBase *socket = server->Accept();
+        if ( !socket )
+        {
+            puts("ERROR: wxSocketServer::Accept() failed.");
+            break;
+        }
+
+        puts("Server: got a client.");
+
+        server->SetTimeout(60); // 1 min
+
+        while ( socket->IsConnected() )
+        {
+            wxString s;
+            char ch = '\0';
+            for ( ;; )
+            {
+                if ( socket->Read(&ch, sizeof(ch)).Error() )
+                {
+                    // don't log error if the client just close the connection
+                    if ( socket->IsConnected() )
+                    {
+                        puts("ERROR: in wxSocket::Read.");
+                    }
+
+                    break;
+                }
+
+                if ( ch == '\r' )
+                    continue;
+
+                if ( ch == '\n' )
+                    break;
+
+                s += ch;
+            }
+
+            if ( ch != '\n' )
+            {
+                break;
+            }
+
+            printf("Server: got '%s'.\n", s.c_str());
+            if ( s == _T("bye") )
+            {
+                delete socket;
+
+                break;
+            }
+
+            socket->Write(s.MakeUpper().c_str(), s.length());
+            socket->Write("\r\n", 2);
+            printf("Server: wrote '%s'.\n", s.c_str());
+        }
+
+        puts("Server: lost a client.");
+
+        socket->Destroy();
+    }
+
+    // same as "delete server" but is consistent with GUI programs
+    server->Destroy();
+}
 
 static void TestSocketClient()
 {
     puts("*** Testing wxSocketClient ***\n");
 
-    wxIPV4address addrDst;
-    addrDst.Hostname("www.wxwindows.org");
-    addrDst.Service(80);
+    static const char *hostname = "www.wxwindows.org";
+
+    wxIPV4address addr;
+    addr.Hostname(hostname);
+    addr.Service(80);
+
+    printf("--- Attempting to connect to %s:80...\n", hostname);
 
     wxSocketClient client;
-    if ( !client.Connect(addrDst) )
+    if ( !client.Connect(addr) )
     {
-        printf("ERROR: failed to connect to %s\n", addrDst.Hostname().c_str());
+        printf("ERROR: failed to connect to %s\n", hostname);
     }
     else
     {
+        printf("--- Connected to %s:%u...\n",
+               addr.Hostname().c_str(), addr.Service());
+
         char buf[8192];
 
-        client.Write("get /front.htm\n", 17);
+        // could use simply "GET" here I suppose
+        wxString cmdGet =
+            wxString::Format("GET http://%s/\r\n", hostname);
+        client.Write(cmdGet, cmdGet.length());
+        printf("--- Sent command '%s' to the server\n",
+               MakePrintable(cmdGet).c_str());
         client.Read(buf, WXSIZEOF(buf));
-        printf("Server replied:\n%s", buf);
+        printf("--- Server replied:\n%s", buf);
+    }
+}
+
+static void TestProtocolFtp()
+{
+    puts("*** Testing wxFTP ***\n");
+
+    wxLog::AddTraceMask(_T("ftp"));
+
+    static const char *hostname = "ftp.wxwindows.org";
+
+    printf("--- Attempting to connect to %s:21...\n", hostname);
+
+    wxFTP ftp;
+    if ( !ftp.Connect(hostname) )
+    {
+        printf("ERROR: failed to connect to %s\n", hostname);
+    }
+    else
+    {
+        printf("--- Connected to %s, current directory is '%s'\n",
+               hostname, ftp.Pwd().c_str());
+        if ( !ftp.ChDir(_T("pub")) )
+        {
+            puts("ERROR: failed to cd to pub");
+        }
+
+        wxArrayString files;
+        if ( !ftp.GetList(files) )
+        {
+            puts("ERROR: failed to get list of files");
+        }
+        else
+        {
+            printf("List of files under '%s':\n", ftp.Pwd().c_str());
+            size_t count = files.GetCount();
+            for ( size_t n = 0; n < count; n++ )
+            {
+                printf("\t%s\n", files[n].c_str());
+            }
+            puts("End of the file list");
+        }
+
+        if ( !ftp.ChDir(_T("..")) )
+        {
+            puts("ERROR: failed to cd to ..");
+        }
+
+        static const char *filename = "welcome.msg";
+        wxInputStream *in = ftp.GetInputStream(filename);
+        if ( !in )
+        {
+            puts("ERROR: couldn't get input stream");
+        }
+        else
+        {
+            size_t size = in->StreamSize();
+            printf("Reading file %s (%u bytes)...", filename, size);
+
+            char *data = new char[size];
+            if ( !in->Read(data, size) )
+            {
+                puts("ERROR: read error");
+            }
+            else
+            {
+                printf("\nContents of %s:\n%s\n", filename, data);
+            }
+
+            delete [] data;
+            delete in;
+        }
     }
 }
 
@@ -738,17 +929,36 @@ static void TestStopWatch()
     wxStopWatch sw;
     printf("Sleeping 3 seconds...");
     wxSleep(3);
-    printf("\telapsed time: %ld\n", sw.Time());
+    printf("\telapsed time: %ldms\n", sw.Time());
 
     sw.Pause();
     printf("Sleeping 2 more seconds...");
     wxSleep(2);
-    printf("\telapsed time: %ld\n", sw.Time());
+    printf("\telapsed time: %ldms\n", sw.Time());
 
     sw.Resume();
     printf("And 3 more seconds...");
     wxSleep(3);
-    printf("\telapsed time: %ld\n", sw.Time());
+    printf("\telapsed time: %ldms\n", sw.Time());
+
+    wxStopWatch sw2;
+    puts("\nChecking for 'backwards clock' bug...");
+    for ( size_t n = 0; n < 70; n++ )
+    {
+        sw2.Start();
+
+        for ( size_t m = 0; m < 100000; m++ )
+        {
+            if ( sw.Time() < 0 || sw2.Time() < 0 )
+            {
+                puts("\ntime is negative - ERROR!");
+            }
+        }
+
+        putchar('.');
+    }
+
+    puts(", ok.");
 }
 
 #endif // TEST_TIMER
@@ -2120,17 +2330,6 @@ static void TestStringFind()
     puts("");
 }
 
-// replace TABs with \t and CRs with \n
-static wxString MakePrintable(const wxChar *s)
-{
-    wxString str(s);
-    (void)str.Replace(_T("\t"), _T("\\t"));
-    (void)str.Replace(_T("\n"), _T("\\n"));
-    (void)str.Replace(_T("\r"), _T("\\r"));
-
-    return str;
-}
-
 static void TestStringTokenizer()
 {
     puts("*** Testing wxStringTokenizer ***");
@@ -2424,7 +2623,13 @@ int main(int argc, char **argv)
 #endif // TEST_MIME
 
 #ifdef TEST_SOCKETS
-    TestSocketClient();
+    if ( 1 )
+        TestSocketServer();
+    if ( 0 )
+    {
+        TestSocketClient();
+        TestProtocolFtp();
+    }
 #endif // TEST_SOCKETS
 
 #ifdef TEST_TIMER