继续研究 SO_KEEPALIVE 问题

借助于网上他人文章,研究 SO_KEEPLIVE 问题:http://www.iteye.com/topic/1118643

client端加了 setsockopt 函数调用。看看能否在服务器端未启动时,或下线时候,一直等待两小时左右。

#include <sys/types.h>                        
#include <sys/socket.h>                        
#include <stdio.h>                        
#include <netinet/in.h>                        
#include <arpa/inet.h>                        
#include <unistd.h>                        
#include <stdlib.h>                        
int main(){                        
   int sockfd;                        
   int len;                        
   struct sockaddr_in address;                        
   int result;                        
   int i,byte;                        
   char char_recv,char_send;
   int optval;                        
   socklen_t optlen = sizeof(optval); 
                        
   if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){                        
           perror("socket");                        
           exit(EXIT_FAILURE);                        
   }                        
                        
   /* Set the option active */                        
   optval = 1;                        
   optlen = sizeof(optval);                        
   if(setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0){ 
      perror("setsockopt()");                        
      close(s);                        
      exit(EXIT_FAILURE);                        
   }                        
                        
   address.sin_family = AF_INET;                        
   address.sin_addr.s_addr = inet_addr("127.0.0.1");                        
   address.sin_port = 8080;                        
   len = sizeof(address);                        
                        
   if((result = connect(sockfd,(struct sockaddr *)&address,len)) == -1){ 
            perror("connect");                        
            exit(EXIT_FAILURE);                        
   }                        
                        
   printf("please enter the context you want to send to server :"); 
   scanf("%c",&char_send);                        
                        
   if(byte = send(sockfd,&char_send,1,0) == -1){                        
           perror("send");                        
           exit(EXIT_FAILURE);                        
   }                        
                        
   if(byte = recv(sockfd,&char_recv,1,0) == -1){                        
           perror("recv");                        
           exit(EXIT_FAILURE);                        
   }                        
                        
   printf("receive from server %c\n",char_recv); 
   close(sockfd);                        
   exit(0);                        
}                        

服务器端逻辑不变:

#include <sys/types.h>                
#include <sys/socket.h>                
#include <stdio.h>                
#include <netinet/in.h>                
#include <arpa/inet.h>                
#include <unistd.h>                
#include <stdlib.h>                              
char dict(char temp){                
   if(temp == 'Z'){                
         return 'A';                
   }                
   return temp+1;                
}              
                
int main(){                
      int server_sockfd,client_sockfd;                
      int server_len,client_len;                
      struct sockaddr_in server_address;                
      struct sockaddr_in client_address;                
      int i,btye;                
      char char_recv,char_send;                
                
      server_address.sin_family = AF_INET;                
      server_address.sin_addr.s_addr = inet_addr("127.0.0.1");                
      server_address.sin_port = 8080;                
      server_len = sizeof(server_address);                
                
      server_sockfd = socket(AF_INET,SOCK_STREAM,0);
      bind(server_sockfd,(struct sockaddr *)&server_address,server_len);  
      listen(server_sockfd,5);                
      printf("server waiting for connect\n"); 
      client_len = sizeof(client_address);                
      client_sockfd = accept(server_sockfd,
(struct sockaddr *)&client_address,(socklen_t *)&client_len); if(btye = recv(client_sockfd,&char_recv,1,0) == -1){ perror("recv"); exit(EXIT_FAILURE); } printf("receive from client is %c\n",char_recv); char_send = dict(char_recv); if(btye = send(client_sockfd,&char_send,1,0) == -1){ perror("send"); exit(EXIT_FAILURE); } shutdown(client_sockfd,2); shutdown(server_sockfd,2); return 0; }

试验结果表明,SO_KEEPLIVE 设定,对此情形无效。

posted @ 2012-08-16 11:19  健哥的数据花园  阅读(843)  评论(0编辑  收藏  举报