在仔细搜索资源后,我仍然不完全确定如何在C中编写正确且可用的计时器函数。我不使用线程(或可并行化的代码)。我只想写一个秒表功能,我可以用它在经过一小段时间后触发一些代码。
这是一个非常常见的计时器用途,在"超时"的情况下,我有一个客户端 - 服务器设置客户端发送一些数据(UDP样式与sendto( ...)和recvfrom(...))。我编写了我的系统,以便客户端在我定义的数据包结构中发送一大块数据,然后服务器通过CRC处理它,然后发回一个确认数据包(ACK),确认收到的信息没有损坏。但是,我想实现超时,如果客户端在一段时间内没有收到ACK,则客户端重新发送数据块(当然服务器被操纵以检查重复)。我想在客户端嵌套这个定时器代码,并且出于某种原因不认为这应该是如此困难。
我从很久以前的工作中挖出了旧的信号处理代码,因为这似乎是我通常认为提到的解决方案的唯一方法,有人可以指导我如何使用以下信号处理代码不只是接收定时信号,而是触发某种动作。从概念上讲,我觉得它会是:"发送数据,启动计时器,在计时器到期后执行重发,重置计时器......重复直到收到ACK"。更好的是,编写一个计时器功能会更容易,但是对于C语言是一种低级语言,它看起来并不是很有希望....
#include <sys/time.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
extern char *strsignal(int sig);
void timer_handler(int a)
{
// handle signal
printf(">>>> signal caught\n");
printf(">>>> int parameter = %s\n", (char*) strsignal(a));
}
int main(int argc, char* argv[])
{
int retval;
struct itimerval timerValue;
struct itimerval oldTimerValue;
struct sigaction action;
action.sa_handler = &timer_handler;
action.sa_flags = SA_NODEFER;
// initialize timer parameters: expires in 5 seconds
timerValue.it_interval.tv_sec = 5;
timerValue.it_interval.tv_usec = 0;
timerValue.it_value.tv_sec = 5;
timerValue.it_value.tv_usec = 0;
// install signal handler to catch SIGALRM
//signal(SIGALRM, timer_handler);
sigaction(SIGALRM, &action, NULL);
retval = setitimer(ITIMER_REAL, &timerValue, &oldTimerValue);
if (-1 == retval)
perror("Could not set timer");
while(1);
return 0;
}
答案 0 :(得分:0)
Xymostech提供了我需要的确切功能,在咨询了“select”的API之后,其中包括一个小的用法示例,我修改了那里的代码以适应我的需要并编写了一个套接字计时器(用于读取,那些非常简单延伸到写入等,因为“select”具有用于启用此类检查的参数。确保您已包含“select”API指定的以下库:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <stdio.h>
以下是我从API示例创建的waittoread(...)函数,并且运行良好。这在我的特定问题的领域中运行良好,但是,如果一个人正在寻找更通用的定时器(即不仅仅用于定时套接字读写或文件描述符),请参考信号处理(有点在代码的精神我发表在我最初的问题)。
#define S1READY 0x01 // necessary for the function's bitwise OR operation
int waittoread(int s1, int timeout_value){
fd_set fds; // create set of sockets to be waited on
struct timeval timeout; // the time-out value
int rc; // # of sockets that are ready before timer expires
int result;
/* Set time limit. */
timeout.tv_sec = timeout_value;
timeout.tv_usec = 0;
/* Create a descriptor set containing the socket. */
FD_ZERO(&fds); // MACRO to reset the socket storage set so new ones can be added
FD_SET(s1, &fds); // add the socket descriptor into the socket set to wait on
rc = select(sizeof(fds)*4, &fds, NULL, NULL, &timeout); // build the socket-wait system
// another way of calling select that would be a better approach:
// rc = select(s1 + 1), &fds, NULL, NULL, &timeout);
if (rc==-1) {
perror("Error: Call to select failed.");
return -1;
}
result = 0;
if (rc > 0){
if (FD_ISSET(s1, &fds)) result |= S1READY; // if the result is non-zero, perform a BIT-wise OR to extract the true socket count #
}
return result;
}