From commits-return-7843-apmail-apr-commits-archive=apr.apache.org@apr.apache.org Thu Jul 13 01:49:44 2006 Return-Path: Delivered-To: apmail-apr-commits-archive@www.apache.org Received: (qmail 87731 invoked from network); 13 Jul 2006 01:49:44 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 13 Jul 2006 01:49:44 -0000 Received: (qmail 11273 invoked by uid 500); 13 Jul 2006 01:49:44 -0000 Delivered-To: apmail-apr-commits-archive@apr.apache.org Received: (qmail 11180 invoked by uid 500); 13 Jul 2006 01:49:43 -0000 Mailing-List: contact commits-help@apr.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: Reply-To: dev@apr.apache.org List-Id: Delivered-To: mailing list commits@apr.apache.org Received: (qmail 11169 invoked by uid 99); 13 Jul 2006 01:49:43 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Jul 2006 18:49:43 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Jul 2006 18:49:43 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 2130F1A981A; Wed, 12 Jul 2006 18:49:23 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r421474 - in /apr/examples/trunk: README echoclient/ echoclient/echoclient.c Date: Thu, 13 Jul 2006 01:49:22 -0000 To: commits@apr.apache.org From: rooneg@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060713014923.2130F1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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 + +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 \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; +}