日期:2014-05-16 浏览次数:21088 次
/**
* apr tutorial sample code
* http://dev.ariel-networks.com/apr/
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <assert.h>
#include <apr_general.h>
#include <apr_file_io.h>
#include <apr_strings.h>
#include <apr_network_io.h>
/* default listen port number */
#define DEF_LISTEN_PORT 8081
/* default socket backlog number. SOMAXCONN is a system default value */
#define DEF_SOCKET_BACKLOG SOMAXCONN
/* default buffer size */
#define BUFSIZE 4096
/* useful macro */
#define CRLF_STR "\r\n"
static apr_status_t do_listen(apr_socket_t **sock, apr_pool_t *mp);
static int do_serv_task(apr_socket_t *serv_sock, apr_pool_t *mp);
/**
* Network server sample code
* Just response to an HTTP GET request, but this is not a true HTTP server.
* For example, you can test this program as follows,
* $ wget http://localhost:8081/etc/hosts
* @remark Error checks omitted
*/
int main(int argc, const char *argv[])
{
apr_status_t rv;
apr_pool_t *mp;
apr_socket_t *s;/* listening socket */
apr_initialize();
apr_pool_create(&mp, NULL);
rv = do_listen(&s, mp);
if (rv != APR_SUCCESS) {
goto error;
}
while (1) {
apr_socket_t *ns;/* accepted socket */
rv = apr_socket_accept(&ns, s, mp);
if (rv != APR_SUCCESS) {
goto error;
}
/* it is a good idea to specify socket options for the newly accepted socket explicitly */
apr_socket_opt_set(ns, APR_SO_NONBLOCK, 0);
apr_socket_timeout_set(ns, -1);
if (!do_serv_task(ns, mp)) {
goto error;
}
apr_socket