[prev in list] [next in list] [prev in thread] [next in thread] 

List:       sptk-commits
Subject:    r667 - in trunk: examples src/net
From:       alexey () mail ! total-knowledge ! com
Date:       2009-05-17 13:12:06
Message-ID: courier.000000004A100D26.00007CE2 () mail ! total-knowledge ! com
[Download RAW message or body]

Author: alexey
Date: 2009-05-17 06:12:06 -0700 (Sun, 17 May 2009)
New Revision: 667

Modified:
   trunk/examples/client_test.cpp
   trunk/examples/server_test.cpp
   trunk/src/net/CServerSocket.cpp
Log:
Fixed server_test/client_test examples.


Modified: trunk/examples/client_test.cpp
===================================================================
--- trunk/examples/client_test.cpp	2009-05-17 12:00:00 UTC (rev 666)
+++ trunk/examples/client_test.cpp	2009-05-17 13:12:06 UTC (rev 667)
@@ -3,7 +3,7 @@
                           client_test.cpp  -  description
                              -------------------
     begin                : Wed Apr 20, 2005
-    copyright            : (C) 2005-2009 by Alexey Parshin
+    copyright            : (C) 2000-2009 by Alexey Parshin
     email                : alexeyp@gmail.com
  ***************************************************************************/
 
@@ -39,6 +39,9 @@
         client.port(3000);
         client.host("localhost");
 
+        client.readTimeout(60000);
+        client.writeTimeout(60000);
+
         client.open();
 
         cout << "Connected\n";
@@ -46,26 +49,26 @@
         CBuffer buffer(1024);
 
         client.read(buffer, "\n\r");
-        cout << "Receiving: " << buffer.data() << "\n";
+        cout << "Receiving: ";
+        cout << buffer.data() << "\n";
 
         string data = "Several copies of a single string";
         cout << "Sending: test data\n";
-        client.write(data + "\n" + data + " " + data + "\n" + data + " " + data
-                + " " + data + " " + data + "\n" + data + " " + data + "\n");
+        client.write(data + "\n\r" + data + " " + data + "\n\r" + data + " " + data
+                + " " + data + " " + data + "\n\r" + data + " " + data + "\n\r");
 
         cout << "Sending: end data\n";
-        client.write("EOD\n");
+        client.write("EOD\n\r");
 
         client.read(buffer, "\n\r");
-        cout << "Receiving: " << buffer.data() << "\n";
+        cout << "Receiving: ";
+        cout << buffer.data() << "\n";
 
         cout << "Sending: end session\n";
-        client.write("EOS\n");
-
-        client.read(buffer, "\n\r");
-        cout << "Receiving: " << buffer.data() << "\n";
+        client.write("EOS\n\r");
     } catch (exception& e) {
-        cout << "Exception was caught:" << e.what() << "\nExiting.\n";
+        cout << "Exception was caught:";
+        cout << e.what() << "\nExiting.\n";
     }
 
     cout << "Exiting\n";

Modified: trunk/examples/server_test.cpp
===================================================================
--- trunk/examples/server_test.cpp	2009-05-17 12:00:00 UTC (rev 666)
+++ trunk/examples/server_test.cpp	2009-05-17 13:12:06 UTC (rev 667)
@@ -3,7 +3,7 @@
                           server_test.cpp  -  description
                              -------------------
     begin                : Wed Apr 20, 2005
-    copyright            : (C) 2005-2009 by Alexey Parshin
+    copyright            : (C) 2000-2009 by Alexey Parshin
     email                : alexeyp@gmail.com
  ***************************************************************************/
 
@@ -44,27 +44,36 @@
         struct sockaddr_in clientInfo;
 
         server.open(3000);
-        server.accept(clientSocketFD, clientInfo);
 
+        cout << "Server is listening on port ";
+        cout << server.port() << endl;
+        if (!server.accept(clientSocketFD, clientInfo, 60000)) {
+            cerr << "Client didn't connect within one minute, exiting." << endl;
+            return 1;
+        }
+
         CTCPSocket new_sock;
+        new_sock.readTimeout(60000);
+        new_sock.writeTimeout(60000);
         new_sock.attach(clientSocketFD);
 
         try {
-            CBuffer buffer;
+            CBuffer buffer(1024);
             string data;
 
             cout << "Sending: Test SPTK server 1.00\n";
-            new_sock.write("Test SPTK server 1.00\n");
+            new_sock.write("Test SPTK server 1.00\n\r");
 
             cout << "Receving (strings): ";
 
             do {
                 new_sock.read(buffer, "\n\r");
                 cout << buffer.data() << "\n";
+                data = buffer.data();
             } while (data != "EOD");
 
             cout << "Sending: confirmation\n";
-            new_sock.write("Data accepted\n");
+            new_sock.write("Data accepted\n\r");
 
             // End of session
             try {
@@ -74,9 +83,12 @@
 
             server.close();
         } catch (exception& e) {
+            cout << "Exception was caught: ";
+            cout << e.what() << endl;
         }
     } catch (exception& e) {
-        cout << "Exception was caught: " << e.what() << "\nExiting.\n";
+        cout << "Exception was caught: ";
+        cout << e.what() << "\nExiting.\n";
     }
     cout << "Server session closed" << endl;
     return 0;

Modified: trunk/src/net/CServerSocket.cpp
===================================================================
--- trunk/src/net/CServerSocket.cpp	2009-05-17 12:00:00 UTC (rev 666)
+++ trunk/src/net/CServerSocket.cpp	2009-05-17 13:12:06 UTC (rev 667)
@@ -84,12 +84,13 @@
 
 bool CServerSocket::accept(int& clientSocketFD,struct sockaddr_in& clientInfo,uint32_t timeoutMS)
 {
-    if (readyToRead(timeoutMS)) {
-        socklen_t len = sizeof(clientInfo);
-        clientSocketFD = (int) ::accept(m_sockfd, (struct sockaddr *) &clientInfo, &len);
-        if (clientSocketFD < 0)
-            throwSocketError("Error on accept(). ");
-        return true;
-    }
-    return false;
+    if (timeoutMS && !readyToRead(timeoutMS))
+        return false;
+
+    socklen_t len = sizeof(clientInfo);
+    clientSocketFD = (int) ::accept(m_sockfd, (struct sockaddr *) &clientInfo, &len);
+    if (clientSocketFD < 0)
+        throwSocketError("Error on accept(). ");
+
+    return true;
 }

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic