SUN RPC (ONC/RPC):使用 C 中的空过程计算往返时间(或 ping)
Posted
技术标签:
【中文标题】SUN RPC (ONC/RPC):使用 C 中的空过程计算往返时间(或 ping)【英文标题】:SUN RPC (ONC/RPC): Calculating round trip time (or pinging) using null procedure in C 【发布时间】:2012-04-13 04:36:01 【问题描述】:如何计算或估计客户端和服务器之间的 RTT(往返时间)?
解决此问题的教程或示例也会有所帮助。
【问题讨论】:
mattgemmell.com/2008/12/08/what-have-you-tried 约阿希姆嗨。感谢您尝试提供帮助,即使是粗鲁的方式。这是我在提出问题时作为新手所做的(我仍然是新手):我用标题关键字的所有可能组合搜索了网络(关键字是:SUN + RPC + ONC + RTT + round trip +往返时间 + ping + null 过程 + c + 估计 + 客户端 + 服务器)。我阅读了我找到的所有关于 RPC 的文档。我什至阅读了一些 RFC 标准表。 (我记得他们让我大吃一惊。)我什么也没想到。所以下面的答案是我能找到的唯一解决方案。那是…… ...为什么它很珍贵。无论如何,感谢您的宝贵但粗鲁的贡献。 【参考方案1】:这是我的工作:
#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
蒂格兰。
【讨论】:
非常感谢蒂格兰。这可能是整个 www 上为数不多的几个例子之一。我还没有时间尝试,但我会尽快将其标记为已回答。再次,非常感谢。以上是关于SUN RPC (ONC/RPC):使用 C 中的空过程计算往返时间(或 ping)的主要内容,如果未能解决你的问题,请参考以下文章