我已经实现了一个 udp 数据包发送方和接收方。我想计算交易时间,从发送者到接收者需要多少时间..
发件人代码:
void senderFunc() {
/*some other code */
if((s1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
error_handler("\nERROR: in Socket\n");
memset((char *) &me, 0, sizeof(me));
me.sin_family = AF_INET;
me.sin_port = PORT;
if (inet_aton(G_IP, &me.sin_addr)==0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
printf("\Tick - %d : %s",cntr++,str);
sprintf(b1, "%s",str); // Some Information in b1 buffer to transfer
if(sendto(s1, b1, sizeof(b1),0,(struct sockaddr *) &me,n)==-1)
error_handler("\nERROR: in sendto()\n");
close (s1);
return;
}
}
收货人代码:
int receiverFunc () {
struct sockaddr_in other, me;
int s2, n, i = 1;
char b2[BUFLEN];//, b2[BUFLEN];
s2 = socket(AF_INET, SOCK_DGRAM,0);
me.sin_family = AF_INET;
me.sin_port = PORT;
me.sin_addr.s_addr = htonl(INADDR_ANY);
bind(s2,(struct sockaddr *)&me, sizeof(me));
n=sizeof(other);
int incr = 0;
while (i){
recvfrom (s2,b2,BUFLEN,0,(struct sockaddr *) &other, &n);
printf ("\nSubnet 2: Tick - %d : %s",incr++, b2);
}
return 0;
}
谁能帮助我,假设我正在从发件人向收件人发送数据包,我该如何计算此交易之间的时间。那我怎么计算那个时间呢?
感谢这个论坛的最佳回复。我正在寻找您的另一个最佳回复。
谢谢
拉希。