apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From roo...@apache.org
Subject svn commit: r349338 - in /apr/examples/trunk: README echoserver/ echoserver/echoserver.c
Date Mon, 28 Nov 2005 01:17:21 GMT
Author: rooneg
Date: Sun Nov 27 17:17:19 2005
New Revision: 349338

URL: http://svn.apache.org/viewcvs?rev=349338&view=rev
Log:
Add a simple echo server example, to show how to work with sockets.

Note that this is currently only able to handle one client at a time,
when I get some free time I'll look at making it use fork or threads
as appropriate.

* echoserver/echoserver.c: New example.

* README: Mention the new example.

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

Modified: apr/examples/trunk/README
URL: http://svn.apache.org/viewcvs/apr/examples/trunk/README?rev=349338&r1=349337&r2=349338&view=diff
==============================================================================
--- apr/examples/trunk/README (original)
+++ apr/examples/trunk/README Sun Nov 27 17:17:19 2005
@@ -11,3 +11,6 @@
 
 ls
     A basic implementation of the unix ls command.
+
+echoserver
+    A basic TCP server that writes back whatever you send to it.

Added: apr/examples/trunk/echoserver/echoserver.c
URL: http://svn.apache.org/viewcvs/apr/examples/trunk/echoserver/echoserver.c?rev=349338&view=auto
==============================================================================
--- apr/examples/trunk/echoserver/echoserver.c (added)
+++ apr/examples/trunk/echoserver/echoserver.c Sun Nov 27 17:17:19 2005
@@ -0,0 +1,94 @@
+/* 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 serve(apr_pool_t *pool)
+{
+    apr_status_t rv = APR_SUCCESS;
+    apr_sockaddr_t *saddr;
+    apr_pool_t *subpool;
+    apr_socket_t *lskt;
+
+    rv = apr_socket_create(&lskt, 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_bind(lskt, saddr);
+    if (rv)
+        return rv;
+
+    rv = apr_socket_listen(lskt, 5);
+    if (rv)
+        return rv;
+
+    apr_pool_create(&subpool, pool);
+
+    for (;;) {
+        apr_socket_t *cskt;
+
+        apr_pool_clear(subpool);
+
+        rv = apr_socket_accept(&cskt, lskt, subpool);
+        if (rv)
+            return rv;
+
+        for (;;) {
+            /* For the record, it's stupid to use single byte reads and
+             * writes like this, and if this was more than just an example
+             * you'd need some sort of buffering scheme here. */
+            apr_size_t len = 1;
+            char c;
+
+            rv = apr_socket_recv(cskt, &c, &len);
+            if (APR_STATUS_IS_EOF(rv))
+                break;
+            else if (rv)
+                return rv;
+            else if (! len)
+                break;
+
+            rv = apr_socket_send(cskt, &c, &len);
+            if (APR_STATUS_IS_EOF(rv))
+                break;
+            else if (rv)
+                return rv;
+            else if (! len)
+                break;
+        }
+    }
+
+    return rv;
+}
+    
+int main(int argc, const char * const argv[])
+{
+    apr_pool_t *pool;
+    apr_status_t rv;
+
+    apr_initialize();
+    atexit(apr_terminate);
+
+    apr_pool_create(&pool, NULL);
+
+    rv = serve(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