gpt4 book ai didi

c - SUN RPC(ONC/RPC): Calculating round trip time (or pinging) using null procedure in C

转载 作者:行者123 更新时间:2023-11-30 18:00:55 27 4
gpt4 key购买 nike

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

解决这个问题的教程或示例也会有所帮助。

最佳答案

这是我所做的:

#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

提格兰。

关于c - SUN RPC(ONC/RPC): Calculating round trip time (or pinging) using null procedure in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135270/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com