⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendfile.c

📁 linux subdivision ying gai ke yi le ba
💻 C
📖 第 1 页 / 共 2 页
字号:
                tmplen = 0;            }            else {                tmplen -= len;                len = 0;                current_file_offset = 0;            }            /* Last, skip over any trailer data which might have             * been written.             */            while (tmplen && hdtr.numtrailers) {                if (tmplen >= hdtr.trailers[0].iov_len) {                    tmplen -= hdtr.trailers[0].iov_len;                    --hdtr.numtrailers;                    ++hdtr.trailers;                }                else {                    hdtr.trailers[0].iov_len -= tmplen;                    hdtr.trailers[0].iov_base = 			(char *)hdtr.trailers[0].iov_base + tmplen;                    tmplen = 0;                }            }        } while (total_bytes_sent < expected_len &&                 (rv == APR_SUCCESS ||                  (APR_STATUS_IS_EAGAIN(rv) && socket_mode != TIMEOUT)));        if (total_bytes_sent != expected_len) {            fprintf(stderr,                    "client problem: sent %ld of %ld bytes\n",                    (long)total_bytes_sent, (long)expected_len);            exit(1);        }        if (rv) {            fprintf(stderr,                    "client problem: rv %d\n",                    rv);            exit(1);        }    }        current_file_offset = 0;    rv = apr_file_seek(f, APR_CUR, &current_file_offset);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_file_seek()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    printf("After apr_socket_sendfile(), the kernel file pointer is "           "at offset %ld.\n",           (long int)current_file_offset);    rv = apr_socket_shutdown(sock, APR_SHUTDOWN_WRITE);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_socket_shutdown()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    /* in case this is the non-blocking test, set socket timeout;     * we're just waiting for EOF */    rv = apr_socket_timeout_set(sock, apr_time_from_sec(3));    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_socket_timeout_set()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }        bytes_read = 1;    rv = apr_socket_recv(sock, buf, &bytes_read);    if (rv != APR_EOF) {        fprintf(stderr, "apr_socket_recv()->%d/%s (expected APR_EOF)\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    if (bytes_read != 0) {        fprintf(stderr, "We expected to get 0 bytes read with APR_EOF\n"                "but instead we read %ld bytes.\n",                (long int)bytes_read);        exit(1);    }    printf("client: apr_socket_sendfile() worked as expected!\n");    rv = apr_file_remove(TESTFILE, p);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_file_remove()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    return 0;}static int server(void){    apr_status_t rv;    apr_socket_t *sock;    apr_pool_t *p;    char buf[120];    int i;    apr_socket_t *newsock = NULL;    apr_size_t bytes_read;    apr_sockaddr_t *localsa;    int family;    family = APR_UNSPEC;    apr_setup(&p, &sock, &family);    rv = apr_socket_opt_set(sock, APR_SO_REUSEADDR, 1);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_socket_opt_set()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    rv = apr_sockaddr_info_get(&localsa, NULL, family, TESTSF_PORT, 0, p);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_sockaddr_info_get()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    rv = apr_socket_bind(sock, localsa);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_socket_bind()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    rv = apr_socket_listen(sock, 5);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_socket_listen()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    printf("Waiting for a client to connect...\n");    rv = apr_socket_accept(&newsock, sock, p);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_socket_accept()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    printf("Processing a client...\n");    assert(sizeof buf > strlen(HDR1));    bytes_read = strlen(HDR1);    rv = apr_socket_recv(newsock, buf, &bytes_read);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_socket_recv()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    if (bytes_read != strlen(HDR1)) {        fprintf(stderr, "wrong data read (1)\n");        exit(1);    }    if (memcmp(buf, HDR1, strlen(HDR1))) {        fprintf(stderr, "wrong data read (2)\n");        fprintf(stderr, "received: `%.*s'\nexpected: `%s'\n",                (int)bytes_read, buf, HDR1);        exit(1);    }            assert(sizeof buf > strlen(HDR2));    bytes_read = strlen(HDR2);    rv = apr_socket_recv(newsock, buf, &bytes_read);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_socket_recv()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    if (bytes_read != strlen(HDR2)) {        fprintf(stderr, "wrong data read (3)\n");        exit(1);    }    if (memcmp(buf, HDR2, strlen(HDR2))) {        fprintf(stderr, "wrong data read (4)\n");        fprintf(stderr, "received: `%.*s'\nexpected: `%s'\n",                (int)bytes_read, buf, HDR2);        exit(1);    }    for (i = 0; i < HDR3_LEN; i++) {        bytes_read = 1;        rv = apr_socket_recv(newsock, buf, &bytes_read);        if (rv != APR_SUCCESS) {            fprintf(stderr, "apr_socket_recv()->%d/%s\n",                    rv,                    apr_strerror(rv, buf, sizeof buf));            exit(1);        }        if (bytes_read != 1) {            fprintf(stderr, "apr_socket_recv()->%ld bytes instead of 1\n",                    (long int)bytes_read);            exit(1);        }        if (buf[0] != HDR3_CHAR) {            fprintf(stderr,                    "problem with data read (byte %d of hdr 3):\n",                    i);            fprintf(stderr, "read `%c' (0x%x) from client; expected "                    "`%c'\n",                    buf[0], buf[0], HDR3_CHAR);            exit(1);        }    }            for (i = 0; i < FILE_LENGTH; i++) {        bytes_read = 1;        rv = apr_socket_recv(newsock, buf, &bytes_read);        if (rv != APR_SUCCESS) {            fprintf(stderr, "apr_socket_recv()->%d/%s\n",                    rv,                    apr_strerror(rv, buf, sizeof buf));            exit(1);        }        if (bytes_read != 1) {            fprintf(stderr, "apr_socket_recv()->%ld bytes instead of 1\n",                    (long int)bytes_read);            exit(1);        }        if (buf[0] != FILE_DATA_CHAR) {            fprintf(stderr,                    "problem with data read (byte %d of file):\n",                    i);            fprintf(stderr, "read `%c' (0x%x) from client; expected "                    "`%c'\n",                    buf[0], buf[0], FILE_DATA_CHAR);            exit(1);        }    }            assert(sizeof buf > strlen(TRL1));    bytes_read = strlen(TRL1);    rv = apr_socket_recv(newsock, buf, &bytes_read);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_socket_recv()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    if (bytes_read != strlen(TRL1)) {        fprintf(stderr, "wrong data read (5)\n");        exit(1);    }    if (memcmp(buf, TRL1, strlen(TRL1))) {        fprintf(stderr, "wrong data read (6)\n");        fprintf(stderr, "received: `%.*s'\nexpected: `%s'\n",                (int)bytes_read, buf, TRL1);        exit(1);    }            assert(sizeof buf > strlen(TRL2));    bytes_read = strlen(TRL2);    rv = apr_socket_recv(newsock, buf, &bytes_read);    if (rv != APR_SUCCESS) {        fprintf(stderr, "apr_socket_recv()->%d/%s\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    if (bytes_read != strlen(TRL2)) {        fprintf(stderr, "wrong data read (7)\n");        exit(1);    }    if (memcmp(buf, TRL2, strlen(TRL2))) {        fprintf(stderr, "wrong data read (8)\n");        fprintf(stderr, "received: `%.*s'\nexpected: `%s'\n",                (int)bytes_read, buf, TRL2);        exit(1);    }    for (i = 0; i < TRL3_LEN; i++) {        bytes_read = 1;        rv = apr_socket_recv(newsock, buf, &bytes_read);        if (rv != APR_SUCCESS) {            fprintf(stderr, "apr_socket_recv()->%d/%s\n",                    rv,                    apr_strerror(rv, buf, sizeof buf));            exit(1);        }        if (bytes_read != 1) {            fprintf(stderr, "apr_socket_recv()->%ld bytes instead of 1\n",                    (long int)bytes_read);            exit(1);        }        if (buf[0] != TRL3_CHAR) {            fprintf(stderr,                    "problem with data read (byte %d of trl 3):\n",                    i);            fprintf(stderr, "read `%c' (0x%x) from client; expected "                    "`%c'\n",                    buf[0], buf[0], TRL3_CHAR);            exit(1);        }    }            bytes_read = 1;    rv = apr_socket_recv(newsock, buf, &bytes_read);    if (rv != APR_EOF) {        fprintf(stderr, "apr_socket_recv()->%d/%s (expected APR_EOF)\n",                rv,		apr_strerror(rv, buf, sizeof buf));        exit(1);    }    if (bytes_read != 0) {        fprintf(stderr, "We expected to get 0 bytes read with APR_EOF\n"                "but instead we read %ld bytes (%c).\n",                (long int)bytes_read, buf[0]);        exit(1);    }    printf("server: apr_socket_sendfile() worked as expected!\n");    return 0;}int main(int argc, char *argv[]){#ifdef SIGPIPE    signal(SIGPIPE, SIG_IGN);#endif    /* Gee whiz this is goofy logic but I wanna drive sendfile right now,      * not dork around with the command line!     */    if (argc >= 3 && !strcmp(argv[1], "client")) {        char *host = 0;        if (argv[3]) {            host = argv[3];        }	        if (!strcmp(argv[2], "blocking")) {            return client(BLK, host);        }        else if (!strcmp(argv[2], "timeout")) {            return client(TIMEOUT, host);        }        else if (!strcmp(argv[2], "nonblocking")) {            return client(NONBLK, host);        }    }    else if (argc == 2 && !strcmp(argv[1], "server")) {        return server();    }    fprintf(stderr,             "Usage: %s client {blocking|nonblocking|timeout}\n"            "       %s server\n",            argv[0], argv[0]);    return -1;}#endif /* !APR_HAS_SENDFILE */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -