- 论坛徽章:
- 0
|
监查连接是否存在
请大家帮我改改,系统是FreeBSD.- 1 #include <sys/types.h>;
- 2 #include <sys/socket.h>;
- 3 #include <stdio.h>;
- 4 #include <fcntl.h>;
- 5 #include <unistd.h>;
- 6 void get_keepalive(int);
- 7 int
- 8 main(void)
- 9 {
- 10 int sockfd;
- 11 const int set_opt=1;
- 12 int set_optlen=sizeof(set_opt);
- 13 if((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0) {
- 14 perror("un create a socket");
- 15 return(1);
- 16 }
- 17 get_keepalive(sockfd);
- 18 if(setsockopt(sockfd,SOL_SOCKET,SO_KEEPALIVE,&set_opt,set_optlen)<0) {
- 19 perror("setsockopt error");
- 20 exit(1);
- 21 }
- 22 get_keepalive(sockfd);
- 23 return(0);
- 24 }
- 25 void
- 26 get_keepalive(int sockfd)
- 27 {
- 28 char opt;
- 29 int optlen=sizeof(opt);
- 30 if(getsockopt(sockfd,SOL_SOCKET,SO_KEEPALIVE,&opt,&optlen) <0) {
- 31 perror("getsockopt error");
- 32 return(1);
- 33 }
- 34 printf(" KEEPALIVE status is %d\n",opt);
- 35 return;
- 36 }
- 37
复制代码 # # gcc set_SO_KEEPALIVE.c
set_SO_KEEPALIVE.c: In function `get_keepalive':
set_SO_KEEPALIVE.c:32: warning: `return' with a value, in function returning void
#./a.out
KEEPALIVE status is 0
KEEPALIVE status is 8
good sleep!! bye !!  |
|