apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From roo...@apache.org
Subject svn commit: r421474 - in /apr/examples/trunk: README echoclient/ echoclient/echoclient.c
Date Thu, 13 Jul 2006 01:49:22 GMT
Author: rooneg
Date: Wed Jul 12 18:49:22 2006
New Revision: 421474

URL: http://svn.apache.org/viewvc?rev=421474&view=rev
Log:
Add a basic echo client example.

* echoclient/echoclient.c: New example.

* README: Note new example.

Added:
    apr/examples/trunk/echoclient/
    apr/examples/trunk/echoclient/echoclient.c
Modified:
    apr/examples/trunk/README

Modified: apr/examples/trunk/README
URL: http://svn.apache.org/viewvc/apr/examples/trunk/README?rev=421474&r1=421473&r2=421474&view=diff
==============================================================================
--- apr/examples/trunk/README (original)
+++ apr/examples/trunk/README Wed Jul 12 18:49:22 2006
@@ -17,3 +17,7 @@
 
 echoserver
     A basic TCP server that writes back whatever you send to it.
+
+echoclient
+    A basic TCP client that writes data from the command line to a
+    socket and reads the response back.

Added: apr/examples/trunk/echoclient/echoclient.c
URL: http://svn.apache.org/viewvc/apr/examples/trunk/echoclient/echoclient.c?rev=421474&view=auto
==============================================================================
--- apr/examples/trunk/echoclient/echoclient.c (added)
+++ apr/examples/trunk/echoclient/echoclient.c Wed Jul 12 18:49:22 2006
@@ -0,0 +1,84 @@
+/* This code is PUBLIC DOMAIN, and is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND. See the accompanying 
+ * LICENSE file.
+ */
+#include "apr.h"
+#include "apr_network_io.h"
+#include <stdlib.h>
+
+apr_status_t send_and_recv(const char *str, apr_pool_t *pool)
+{
+    apr_status_t rv = APR_SUCCESS;
+    apr_size_t len, total = 0;
+    apr_sockaddr_t *saddr;
+    apr_socket_t *skt;
+
+    rv = apr_socket_create(&skt, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
+                           pool);
+    if (rv)
+       return rv;
+
+    rv = apr_sockaddr_info_get(&saddr, "127.0.0.1", APR_UNSPEC, 4747, 0,
+                               pool);
+    if (rv)
+        return rv;
+
+    rv = apr_socket_connect(skt, saddr);
+    if (rv)
+        return rv;
+
+    len = strlen(str);
+
+    rv = apr_socket_send(skt, str, &len);
+    if (rv)
+        return rv;
+
+    for (;;) {
+        char c;
+
+        len = 1;
+
+        apr_socket_recv(skt, &c, &len);
+        if (APR_STATUS_IS_EOF(rv))
+            break;
+        else if (rv)
+            return rv;
+        else if (! len)
+            break;
+
+        printf("%c", c);
+
+        if (++total == strlen(str))
+            break;
+    }
+
+    return rv;
+}
+    
+int main(int argc, const char * const argv[])
+{
+    apr_pool_t *pool;
+    apr_status_t rv;
+
+    if (argc != 2) {
+        fprintf(stderr, "usage: echoclient <string>\n");
+        return EXIT_FAILURE;
+    }
+
+    apr_initialize();
+    atexit(apr_terminate);
+
+    apr_pool_create(&pool, NULL);
+
+    rv = send_and_recv(argv[1], pool);
+    if (rv) {
+        char buffer[256] = { 0 };
+
+        fprintf(stderr, "error: %s\n",
+                apr_strerror(rv, buffer, sizeof(buffer)));
+
+        return EXIT_FAILURE;
+    }
+
+    return EXIT_SUCCESS;
+}



Mime
View raw message