bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / client.c
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 #include <stdlib.h>
18 #include "apr_network_io.h"
19 #include "apr_errno.h"
20 #include "apr_general.h"
21 #include <errno.h>
22
23 #define STRLEN 15
24
25 int main(int argc, char *argv[])
26 {
27     apr_pool_t *context;
28     apr_socket_t *sock;
29     apr_size_t length;
30     apr_status_t stat;
31     char datasend[STRLEN] = "Send data test";
32     char datarecv[STRLEN];
33     char msgbuf[80];
34     char *local_ipaddr, *remote_ipaddr;
35     char *dest = "127.0.0.1";
36     apr_port_t local_port, remote_port;
37     apr_interval_time_t timeout = apr_time_from_sec(2);
38     apr_sockaddr_t *local_sa, *remote_sa;
39
40     setbuf(stdout, NULL);
41     if (argc > 1) {
42         dest = argv[1];
43     }
44
45     if (argc > 2) {
46         timeout = atoi(argv[2]);
47     }
48
49     fprintf(stdout, "Initializing.........");
50     if (apr_initialize() != APR_SUCCESS) {
51         fprintf(stderr, "Something went wrong\n");
52         exit(-1);
53     }
54     fprintf(stdout, "OK\n");
55     atexit(apr_terminate);
56
57     fprintf(stdout, "Creating context.......");
58     if (apr_pool_create(&context, NULL) != APR_SUCCESS) {
59         fprintf(stderr, "Something went wrong\n");
60         exit(-1);
61     }
62     fprintf(stdout, "OK\n");
63
64     fprintf(stdout,"\tClient:  Making socket address...............");
65     if ((stat = apr_sockaddr_info_get(&remote_sa, dest, APR_UNSPEC, 8021, 0, context)) 
66         != APR_SUCCESS) {
67         fprintf(stdout, "Failed!\n");
68         fprintf(stdout, "Address resolution failed for %s: %s\n", 
69                 dest, apr_strerror(stat, msgbuf, sizeof(msgbuf)));
70         exit(-1);
71     }
72     fprintf(stdout,"OK\n");
73
74     fprintf(stdout, "\tClient:  Creating new socket.......");
75     if (apr_socket_create(&sock, remote_sa->family, SOCK_STREAM,
76                           context) != APR_SUCCESS) {
77         fprintf(stderr, "Couldn't create socket\n");
78         exit(-1);
79     }
80     fprintf(stdout, "OK\n");
81
82     fprintf(stdout, "\tClient:  Setting socket timeout.......");
83     stat = apr_socket_timeout_set(sock, timeout);
84     if (stat) {
85         fprintf(stderr, "Problem setting timeout: %d\n", stat);
86         exit(-1);
87     }
88     fprintf(stdout, "OK\n");
89
90     fprintf(stdout, "\tClient:  Connecting to socket.......");
91
92     stat = apr_socket_connect(sock, remote_sa);
93
94     if (stat != APR_SUCCESS) {
95         apr_socket_close(sock);
96         fprintf(stderr, "Could not connect: %s (%d)\n", 
97                 apr_strerror(stat, msgbuf, sizeof(msgbuf)), stat);
98         fflush(stderr);
99         exit(-1);
100     }
101     fprintf(stdout, "OK\n");
102
103     apr_socket_addr_get(&remote_sa, APR_REMOTE, sock);
104     apr_sockaddr_ip_get(&remote_ipaddr, remote_sa);
105     apr_sockaddr_port_get(&remote_port, remote_sa);
106     apr_socket_addr_get(&local_sa, APR_LOCAL, sock);
107     apr_sockaddr_ip_get(&local_ipaddr, local_sa);
108     apr_sockaddr_port_get(&local_port, local_sa);
109     fprintf(stdout, "\tClient socket: %s:%u -> %s:%u\n", local_ipaddr, local_port, remote_ipaddr, remote_port);
110
111     fprintf(stdout, "\tClient:  Trying to send data over socket.......");
112     length = STRLEN;
113     if ((stat = apr_socket_send(sock, datasend, &length) != APR_SUCCESS)) {
114         apr_socket_close(sock);
115         fprintf(stderr, "Problem sending data: %s (%d)\n",
116                 apr_strerror(stat, msgbuf, sizeof(msgbuf)), stat);
117         exit(-1);
118     }
119     fprintf(stdout, "OK\n");
120    
121     length = STRLEN; 
122     fprintf(stdout, "\tClient:  Trying to receive data over socket.......");
123
124     if ((stat = apr_socket_recv(sock, datarecv, &length)) != APR_SUCCESS) {
125         apr_socket_close(sock);
126         fprintf(stderr, "Problem receiving data: %s (%d)\n", 
127                 apr_strerror(stat, msgbuf, sizeof(msgbuf)), stat);
128         exit(-1);
129     }
130     if (strcmp(datarecv, "Recv data test")) {
131         apr_socket_close(sock);
132         fprintf(stderr, "I did not receive the correct data %s\n", datarecv);
133         exit(-1);
134     }
135     fprintf(stdout, "OK\n");
136
137     fprintf(stdout, "\tClient:  Shutting down socket.......");
138     if (apr_socket_shutdown(sock, APR_SHUTDOWN_WRITE) != APR_SUCCESS) {
139         apr_socket_close(sock);
140         fprintf(stderr, "Could not shutdown socket\n");
141         exit(-1);
142     }
143     fprintf(stdout, "OK\n");
144
145     fprintf(stdout, "\tClient:  Closing down socket.......");
146     if (apr_socket_close(sock) != APR_SUCCESS) {
147         fprintf(stderr, "Could not shutdown socket\n");
148         exit(-1);
149     }
150     fprintf(stdout, "OK\n");
151
152     return 1;
153 }