Apache HTTPD
echod.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* Simple echo daemon, designed to be used for network throughput
18 * benchmarks. The aim is to allow us to monitor changes in performance
19 * of APR networking code, nothing more.
20 */
21
22#include <stdio.h>
23#include <stdlib.h> /* for atexit() */
24
25#include "apr.h"
26#include "apr_network_io.h"
27#include "apr_strings.h"
28
29#define BUF_SIZE 4096
30
31static void reportError(const char *msg, apr_status_t rv,
33{
34 fprintf(stderr, "%s\nError: %d\n'%s'\n", msg, rv,
35 apr_psprintf(pool, "%pm", &rv));
36}
37
39{
42 char *buf;
43 apr_status_t rv;
44
46 return APR_ENOPOOL;
47
48
50 if (!buf)
51 return ENOMEM;
52
53 do {
54 len = BUF_SIZE;
56 if (APR_STATUS_IS_EOF(rv) || len == 0 || rv != APR_SUCCESS)
57 break;
59 if (len == 0 || rv != APR_SUCCESS)
60 break;
61 } while (rv == APR_SUCCESS);
62
64 return APR_SUCCESS;
65}
66
68{
70 apr_socket_t *listener, *accepted;
71 apr_status_t rv;
72
74 parent);
75 if (rv != APR_SUCCESS) {
76 reportError("Unable to create socket", rv, parent);
77 return rv;
78 }
79
80 rv = apr_sockaddr_info_get(&sockAddr, "127.0.0.1", APR_UNSPEC,
81 port, 0, parent);
82 if (rv != APR_SUCCESS) {
83 reportError("Unable to get socket info", rv, parent);
84 apr_socket_close(listener);
85 return rv;
86 }
87
88 if ((rv = apr_socket_bind(listener, sockAddr)) != APR_SUCCESS ||
89 (rv = apr_socket_listen(listener, 5)) != APR_SUCCESS) {
90 reportError("Unable to bind or listen to socket", rv, parent);
91 apr_socket_close(listener);
92 return rv;
93 }
94
95 for (;;) {
96 rv = apr_socket_accept(&accepted, listener, parent);
97 if (rv != APR_SUCCESS) {
98 reportError("Error accepting on socket", rv, parent);
99 break;
100 }
101 printf("\tAnswering connection\n");
102 rv = talkTalk(accepted, parent);
103 apr_socket_close(accepted);
104 printf("\tConnection closed\n");
105 if (rv != APR_SUCCESS)
106 break;
107 }
108
109 apr_socket_close(listener);
110 return APR_SUCCESS;
111}
112
113int main(int argc, char **argv)
114{
116 apr_port_t theport = 4747;
117
118 printf("APR Test Application: echod\n");
119
122
124
125 if (argc >= 2) {
126 printf("argc = %d, port = '%s'\n", argc, argv[1]);
127 theport = atoi(argv[1]);
128 }
129
130 fprintf(stdout, "Starting to listen on port %d\n", theport);
132
133 return 0;
134}
const char apr_size_t len
Definition ap_regex.h:187
#define socket
APR Network library.
APR Strings library.
static void reportError(const char *msg, apr_status_t rv, apr_pool_t *pool)
Definition echod.c:31
#define BUF_SIZE
Definition echod.c:29
static apr_status_t talkTalk(apr_socket_t *socket, apr_pool_t *parent)
Definition echod.c:38
static apr_status_t glassToWall(apr_port_t port, apr_pool_t *parent)
Definition echod.c:67
const unsigned char * buf
Definition util_md5.h:50
const char apr_port_t port
Definition http_vhost.h:125
#define APR_ENOPOOL
Definition apr_errno.h:290
#define APR_STATUS_IS_EOF(s)
Definition apr_errno.h:567
#define APR_PROTO_TCP
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_pool_t int argc
Definition apr_getopt.h:104
apr_uint16_t apr_port_t
#define APR_UNSPEC
#define APR_INET
apr_pool_t * parent
Definition apr_pools.h:197
#define apr_pool_create(newpool, parent)
Definition apr_pools.h:322
const char * argv[3]
return NULL
Definition mod_so.c:359
int main(void)
Definition occhild.c:9
static apr_sockaddr_t * sockAddr
Definition sockperf.c:59
apr_status_t apr_socket_send(apr_socket_t *sock, const char *buf, apr_size_t *len)
Definition sendrecv.c:30
apr_status_t apr_socket_recv(apr_socket_t *sock, char *buf, apr_size_t *len)
Definition sendrecv.c:70
apr_status_t apr_socket_accept(apr_socket_t **new, apr_socket_t *sock, apr_pool_t *connection_context)
Definition sockets.c:247
apr_status_t apr_socket_listen(apr_socket_t *sock, apr_int32_t backlog)
Definition sockets.c:239
apr_status_t apr_socket_bind(apr_socket_t *sock, apr_sockaddr_t *sa)
Definition sockets.c:216
apr_status_t apr_socket_close(apr_socket_t *thesocket)
Definition sockets.c:211
apr_status_t apr_socket_create(apr_socket_t **new, int ofamily, int type, int protocol, apr_pool_t *cont)
Definition sockets.c:116