三种网络超时检测
三种网络超时检测
1:setsockopt
服务器:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<strings.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/time.h>
static int connfd;
int main(int argc, char **argv)
{
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
int on = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
struct sockaddr_in my_addr, cli_addr;
bzero(&my_addr, sizeof my_addr);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(atoi(argv[1]));
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr));
listen(sockfd, 4);
socklen_t len;
len = sizeof(cli_addr);
bzero(&cli_addr, len);
connfd = accept(sockfd, (struct sockaddr *)&cli_addr, &len);
printf("new connection: %s:%hu\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(connfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
char buf[1024];
int n;
while(1){
bzero(buf, 1024);
if((n = read(connfd, buf, 1023)) <= 0)
break;
write(connfd, buf, strlen(buf));
printf("srv: %s\n",buf);
}
return 0;
}
客服端:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<strings.h>
#include<string.h>
int main(int argc, char **argv)
{
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in src_addr;
bzero(&src_addr, sizeof src_addr);
src_addr.sin_family = AF_INET;
inet_pton(AF_INET, argv[1],(struct sockaddr *)&src_addr.sin_addr);
src_addr.sin_port = htons(atoi(argv[2]));
connect(sockfd, (struct sockaddr *)&src_addr, sizeof src_addr);
char buf[1024];
char b[1024];
while(1){
bzero(b, 1024);
fgets(buf, 1024, stdin);
write(sockfd, buf, strlen(buf));
read(sockfd, b ,1023);
printf("b: %s\n",b);
}
return 0;
}
2:select
服务器:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<strings.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/time.h>
#include<unistd.h>
static int connfd;
int main(int argc, char **argv)
{
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
int on = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
struct sockaddr_in my_addr, cli_addr;
bzero(&my_addr, sizeof my_addr);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(atoi(argv[1]));
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr));
listen(sockfd, 4);
socklen_t len;
len = sizeof(cli_addr);
bzero(&cli_addr, len);
connfd = accept(sockfd, (struct sockaddr *)&cli_addr, &len);
printf("new connection: %s:%hu\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
char buf[1024];
int n;
fd_set ret;
struct timeval tv = {5, 0};
while(1){
FD_ZERO(&ret);
FD_SET(connfd, &ret);
int re;
re = select(connfd+1, &ret, NULL, NULL, &tv);
if(re == 0)
{
printf("time out.");
exit(0);
}
if(FD_ISSET(connfd, &ret))
{
bzero(buf, 1024);
if((n = read(connfd, buf, 1023)) <= 0)
break;
write(connfd, buf, strlen(buf));
printf("srv: %s\n",buf);
}
}
return 0;
}
客服端:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<strings.h>
#include<string.h>
int main(int argc, char **argv)
{
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in src_addr;
bzero(&src_addr, sizeof src_addr);
src_addr.sin_family = AF_INET;
inet_pton(AF_INET, argv[1],(struct sockaddr *)&src_addr.sin_addr);
src_addr.sin_port = htons(atoi(argv[2]));
connect(sockfd, (struct sockaddr *)&src_addr, sizeof src_addr);
char buf[1024];
char b[1024];
while(1){
bzero(b, 1024);
fgets(buf, 1024, stdin);
write(sockfd, buf, strlen(buf));
read(sockfd, b ,1023);
printf("b: %s\n",b);
}
return 0;
}
3:设置定时器
服务器:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<strings.h>
#include<string.h>
#include<arpa/inet.h>
#include<signal.h>
static int connfd;
void f(int reg)
{
printf("time out.");
exit(0);
}
int main(int argc, char **argv)
{
signal(SIGALRM,f);
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
int on = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
struct sockaddr_in my_addr, cli_addr;
bzero(&my_addr, sizeof my_addr);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(atoi(argv[1]));
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr));
listen(sockfd, 4);
socklen_t len;
len = sizeof(cli_addr);
bzero(&cli_addr, len);
connfd = accept(sockfd, (struct sockaddr *)&cli_addr, &len);
printf("new connection: %s:%hu\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
char buf[1024];
int n;
while(1){
alarm(5);
bzero(buf, 1024);
if((n = read(connfd, buf, 1023)) <= 0)
break;
write(connfd, buf, strlen(buf));
printf("srv: %s\n",buf);
}
return 0;
}
客服端:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<strings.h>
#include<string.h>
int main(int argc, char **argv)
{
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in src_addr;
bzero(&src_addr, sizeof src_addr);
src_addr.sin_family = AF_INET;
inet_pton(AF_INET, argv[1],(struct sockaddr *)&src_addr.sin_addr);
src_addr.sin_port = htons(atoi(argv[2]));
connect(sockfd, (struct sockaddr *)&src_addr, sizeof src_addr);
char buf[1024];
char b[1024];
while(1){
bzero(b, 1024);
fgets(buf, 1024, stdin);
write(sockfd, buf, strlen(buf));
read(sockfd, b ,1023);
printf("b: %s\n",b);
}
return 0;
}