【发布时间】:2016-09-23 10:15:41
【问题描述】:
我有一个程序可以计算发送或接收大量数据的时间。当我收到数据时,时钟说它只需要实际时间的一半左右。
接收数据的终端输出:
Amount of data recived: 60296112/300000000
Time: 4
Start time: 3269
End time: 4849790
Clocks per sec: 1000000
以及发送数据的终端的输出:
Sent 300000000 bytes of data
Time to send was 10.793425
发送数据的终端将在发送完所有其他数据后发送停止信号。当我观察接收数据的终端时,我可以看到它在另一个终端开始发送数据时开始计数,并且我可以看到它从clock() 打印输出的时间比输出显示的时间长。
我的接收部分代码:
static void recive_sock(int socket_fd,char *buffert, size_t buffert_size, struct sockaddr *other, socklen_t *other_size){
clock_t start_t, end_t;
long int total_time = 0;
printf("Listning for data\n" );
fflush(stdout);
int run = 1;
char start = 1;
int amount = 0;
int recive_length;
while(run){
recive_length = recvfrom(socket_fd, buffert, buffert_size, 0, other, other_size );
if(recive_length < 0){
die("Recvfrom failed");
}
if(strncmp(buffert, "HELLO!", 6) == 0){
amount += recive_length;
if(start == 1){
start = 0;
start_t = clock();
printf("Start: %ld\n",start_t );
}
printf("%ld\n",clock() );
}
else if (strncmp(buffert, "die", 3) == 0) {
run = 0;
end_t = clock();
printf("End %ld\n",end_t );
total_time = (end_t - start_t) / CLOCKS_PER_SEC;
printf("Amount of data recived: %d/%d\nTime: %ld\nStart time: %ld\nEnd time: %ld\n,Clocks per sec: %ld", amount, AMOUNT, total_time, start_t, end_t, CLOCKS_PER_SEC);
}
}
}
【问题讨论】:
-
clock()返回使用的处理器时间,而不是经过的时间。更多信息:pubs.opengroup.org/onlinepubs/9699919799/functions/… -
您确定数据在您开始发送后立即发送出去吗?可能存在延迟发送的内部缓冲,直到有足够的数据发送。我认为发送数据的代码在这里非常相关。
-
1)
if(recive_length < 0){在此处检查 EAGAIN 2)if (strncmp(buffert, "die", 3) == 0) {TCP 是基于流的,它不保留“消息边界”,您的“die”关键字可以在缓冲区中的任何位置,甚至可能跨细分市场。