有一个tcp client程序,原来可以用的,为什么加上一个timer的sigaction就不能用了?感觉是由于timer的sigaction,导致了TCP连接断线了
可以用的程序: int main(int argc, char **argv) { struct sockaddr_in client_addr; // 客户端(本地)的socket地址结构 struct sockaddr_in server_addr; // 服务器端(远程)的socket地址结构 int client_socket; // 客户端socket socklen_t server_addr_length; char buffer[BUFFER_SIZE]; // 缓冲区大小 int length = 0; bzero(&client_addr, sizeof(client_addr)); // 设置一个SOCKET地址结构client_addr,代表客户机INTERNET地址,端口 client_addr.sin_family = AF_INET; // internet协议族 client_addr.sin_addr.s_addr = htons(INADDR_ANY); // INADDR_ANY表示获取本机地址 client_addr.sin_port = htons(0); // 0表示让系统自动分配一个空闲端口 client_socket = socket(AF_INET, SOCK_STREAM, 0); // 创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket if(client_socket < 0) { printf("Create Socket Failed!
"); exit(1); }
if (bind(client_socket, (struct sockaddr *)&client_addr, sizeof(client_addr))) { // 把客户机的socket和客户机的socket地址结构联系起来 printf("Client Bind Port Failed!
"); exit(1); } bzero(&server_addr, sizeof(server_addr)); // 设置一个socket地址结构server_addr,代表服务器的internet地址,端口 server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(SERVER_IP); server_addr.sin_port = htons(SERVER_PORT); server_addr_length = sizeof(server_addr); while(1) { if(connect(client_socket, (struct sockaddr *)&server_addr, server_addr_length) < 0) { printf("Can Not Connect To %s!
", SERVER_IP); exit(1); } while(length=recv(client_socket, buffer, BUFFER_SIZE, 0)) { if(length < 0) { printf("Recieve Data From Server Failed!
"); break; }
printf("Recieve Data
"); } close(client_socket); } }
/**************************************************************************************/ 加了timer sigaction后的程序:
struct timeval tpstart,tpend; float timeuse;
static long int timer_count = 0; void prompt_info(int signo) { time_t t; if ((++timer_count)%100==0) { t = time(NULL); //获取秒数表示的当时系统时间,time返回从格林尼治时间1970年1月1日0:00开始到现在的秒数 printf("[%d]prompt_info called", timer_count); printf(" current time %s", ctime(&t)); //将秒数转换成字符串输出 gettimeofday(&tpend, NULL); timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec; printf(" Used Time:%f
", timeuse); } }
void init_sigaction(void) { struct sigaction act; printf("clock tick frequency is %d
",HZ); //输出时钟滴答的频率 act.sa_handler = prompt_info; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGALRM, &act, NULL); gettimeofday(&tpstart, NULL); }
void init_time(void) { struct itimerval val;
val.it_value.tv_sec = 0; //设置间隔定时器的当前值 val.it_value.tv_usec = 10000; //取值要大等于时钟滴答的周期,否则仍为时钟滴答的时间 val.it_interval = val.it_value; //间隔计数器的初始值 setitimer(ITIMER_REAL, &val, NULL); //设置真实间隔定时器,定时结束后将发出SIGALRM信号 }
int main(int argc, char **argv) { struct sockaddr_in client_addr; // 客户端(本地)的socket地址结构 struct sockaddr_in server_addr; // 服务器端(远程)的socket地址结构 int client_socket; // 客户端socket socklen_t server_addr_length; char buffer[BUFFER_SIZE]; // 缓冲区大小 int length = 0; init_sigaction(); init_time(); bzero(&client_addr, sizeof(client_addr)); // 设置一个SOCKET地址结构client_addr,代表客户机INTERNET地址,端口 client_addr.sin_family = AF_INET; // internet协议族 client_addr.sin_addr.s_addr = htons(INADDR_ANY); // INADDR_ANY表示获取本机地址 client_addr.sin_port = htons(0); // 0表示让系统自动分配一个空闲端口 client_socket = socket(AF_INET, SOCK_STREAM, 0); // 创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket if(client_socket < 0) { printf("Create Socket Failed!
"); exit(1); }
if (bind(client_socket, (struct sockaddr *)&client_addr, sizeof(client_addr))) { // 把客户机的socket和客户机的socket地址结构联系起来 printf("Client Bind Port Failed!
"); exit(1); } bzero(&server_addr, sizeof(server_addr)); // 设置一个socket地址结构server_addr,代表服务器的internet地址,端口 server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(SERVER_IP); server_addr.sin_port = htons(SERVER_PORT); server_addr_length = sizeof(server_addr); while(1) { if(connect(client_socket, (struct sockaddr *)&server_addr, server_addr_length) < 0) { printf("Can Not Connect To %s!
", SERVER_IP); exit(1); } while(length=recv(client_socket, buffer, BUFFER_SIZE, 0)) { if(length < 0) { printf("Recieve Data From Server Failed!
"); break; }
printf("Recieve Data
"); } close(client_socket); } }
|