0

如何计算或估计客户端和服务器之间的 RTT(往返时间)?

解决此问题的教程或示例也可以提供帮助。

4

1 回答 1

0

我在这里做什么:

#include <rpc/rpc.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/times.h>
#include <fcntl.h>
#include <time.h>

int main(int argc, char *argv[]) {

    enum clnt_stat status;
    CLIENT *handle;
    struct timeval t;
    clock_t rtime;
    struct tms dumm;
    int count = 100000;
    int i;
    time_t now;
    char stamp[27];
    int programm;
    int version;

    if (argc != 4) {
        printf("Usage: rpcping <host> <program> <version>\n");
        exit(1);
    }

    /*
     *   Create Client Handle
     */
    programm = atoi(argv[2]);
    version = atoi(argv[3]);
    handle = clnt_create(argv[1], programm, version, "tcp");
    if (handle == NULL) {
        printf("clnt failed\n");
        exit(1);
    }

    /*
     *   use 30 seconds timeout
     */
    t.tv_sec = 30;
    t.tv_usec = 0;

    while (1) {
        rtime = times(&dumm);
        for (i = 0; i < count; i++) {
            status = clnt_call(handle, 0, (xdrproc_t) xdr_void,
                NULL, (xdrproc_t) xdr_void, NULL, t);

            if (status == RPC_SUCCESS) { /* NOP */ }
        }
        now = time(NULL);
        ctime_r(&now, stamp);
        stamp[strlen(stamp) - 1] = '\0';
        fprintf(stdout, "[%s]: Speed:  %2.4fs.\n", stamp,
            count / ((double) (times(&dumm) - rtime) / (double) sysconf(_SC_CLK_TCK)));
        fflush(stdout);
    }

    clnt_destroy(handle);
}

我也有一个多线程版本

https://gist.github.com/2401404

蒂格兰

于 2012-04-25T17:44:29.507 回答