关闭

嵌入式 服务器端与客户端通信当客户端异常关闭的解决服务器跟着关闭一服务器

393人阅读 评论(0) 收藏 举报
分类:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>


#define SOCK_UNIX_FILE "/tmp/video_sock"
#define MAX_TRANSMIT_DATA_LEN 10240
#define S_TIME_OUT 2000 //2s

int tcp_write(int socketfd, char* buf, int len,unsigned inttimeout_ms)
{
 int ret;
 int total_lenth = 0;
 int len_remain = len;
 char *write_position = buf;
 struct timeval timeout;
 fd_set  wset, eset;
 int status = 1;

 timeout.tv_sec =timeout_ms/1000;
 timeout.tv_usec = timeout_ms00;
  printf("%s  %d\n",__func__,__LINE__);
 while(1){
  
  FD_ZERO(&wset);
  FD_ZERO(&eset);
  FD_SET(socketfd,&eset);
  FD_SET(socketfd,&wset); 
  ret = select(socketfd+1, NULL,&wset, &eset, &timeout);
  printf("%s  %d\n",__func__,__LINE__);
  if( ( timeout.tv_sec ==0)  && ( timeout.tv_usec == 0) ){
   //timeout
   printf("write timeout \n");
   break;
  }
  printf("%s  %d\n",__func__,__LINE__);
  if(FD_ISSET(socketfd,&wset)){
  printf("%s  %d\n",__func__,__LINE__);
   ret =send(socketfd, write_position, len_remain, 0);
   printf("%s %d \n",__func__,__LINE__);
   if(ret <0){
    printf("write err \n");
    return-1;
   }

   if(ret ==0){
    printf("write ==0 ... \n");

   }
  printf("%s  %d\n",__func__,__LINE__);
   write_position+= ret;
   total_lenth+= ret;
   len_remain -=ret;
   if(0 ==len_remain) {
    break;
   }
   
  }

  if(FD_ISSET(socketfd,&eset)){
   printf("writeerr \n");
   break;

  }


 }

 //printf("write %d \n",len -len_remain);
 return len -len_remain ; 
}


int tcp_read(int socketfd, char* buf, int len,unsigned inttimeout_ms)
{
 int ret;
 int total_lenth = 0;
 int len_remain = len;
 char *read_position = buf;
 struct timeval timeout;
 fd_set  rset, eset;

 timeout.tv_sec =timeout_ms/1000;
 timeout.tv_usec = timeout_ms00;
 //printf("read timeout set %d%d\n",timeout.tv_sec,timeout.tv_usec);

  
 while(1){
  
  FD_ZERO(&rset);
  FD_ZERO(&eset);
  FD_SET(socketfd,&eset);
  FD_SET(socketfd,&rset); 

  
  ret = select(socketfd+1,&rset, NULL, &eset, &timeout);
  
  if( ( timeout.tv_sec ==0)  && ( timeout.tv_usec == 0) ){
   //timeout
   //printf("read timeout \n");
   break;
  }

  if(FD_ISSET(socketfd,&rset)){
   ret =recv(socketfd, read_position, len_remain, 0);
   if(ret <0){
    //error
    //printf("readerr\n");
    break;
   }
   if(ret ==0){
    //printf("read %d..\n",ret);
    //peer close
    //printf("peer close ?\n");
    break;
   }
    //printf("read %d...\n",ret);
   
   read_position+= ret;
   total_lenth+= ret;
   len_remain -=ret;
   if(len_remain<=0) break;
   
  }

  if(FD_ISSET(socketfd,&eset)){
   printf("readerr \n");
   break;

  }

 }
 //printf("time remain %d%d\n",timeout.tv_sec,timeout.tv_usec);
 return total_lenth; 
}


#include
#include
#include
#include
#include
#include

int peer_sock_status = 0;
static void sig_handle(int signo)
{
 switch(signo)
 {

 case SIGTERM:
  printf("receiveSIGTERM!\n");
  exit(0);
  break;
 case SIGKILL:
  printf("receiveSIGKILL!\n");
  exit(0);
  break;   
 case SIGINT:
  printf("\nreceive Ctrl+c!Then,the server will exit after 3 seconds !\n");
  sleep(3);
  exit(0);
  break;
 case SIGALRM:
  printf("receiveSIGALRM!\n");
  break;
 case SIGILL:
  printf("receiveSIGILL!\n");
  break;
 case SIGSEGV:
   printf("receiveSIGSEGV!\n");
  break;
 case SIGPIPE:
   peer_sock_status= 0;
   printf("receiveSIGPIPE!\n");
   break;
 default:
  printf("receive unknownsignal(%d)!\n", signo);
  break;
 }
}

int main (int argc, char *argv[])
{
       int server_sockfd, client_sockfd;
       int server_len, client_len;
       static int i_tmp = 0;
       struct sockaddr_unserver_address;    
       struct sockaddr_un client_address;
       int i, bytes;
       char ch_send, ch_recv;
    chars_buf[MAX_TRANSMIT_DATA_LEN];
    charr_buf[8];
    unsignedint len = 0;
       unlink (SOCK_UNIX_FILE);//delete the file link for the function ofbind  

      
       server_sockfd = socket (AF_UNIX, SOCK_STREAM, 0);

      
       server_address.sun_family = AF_UNIX;

      
       strcpy (server_address.sun_path, SOCK_UNIX_FILE);

      
       server_len = sizeof (server_address);

      
       bind (server_sockfd, (struct sockaddr *)&server_address,server_len);

      
       listen (server_sockfd, 5);//the num of the client is five

       printf ("Server is waiting for client connect...\n");

       client_len = sizeof (client_address);
#if 1
 for(i=1; i<=SIGIO; i++) signal(i,sig_handle);
#endif
accept_again:
    if(i_tmp!= 0)
    {
        printf ("\nServer is waiting for clientconnect...\n");
     }
       client_sockfd = accept (server_sockfd, (struct sockaddr*)&server_address, (socklen_t *)&client_len);
       
       if (client_sockfd == -1) {
               perror ("accept");
               exit (EXIT_FAILURE);
       }
    peer_sock_status=1;
       printf ("The server is waiting for client data...\n");
       strcpy(s_buf,"the tcp send stream to jss_server");

 while(1){

        memset(s_buf,0,MAX_TRANSMIT_DATA_LEN);
        memset(r_buf,0,8);
        sprintf(s_buf,"the%dth packet to jss_server !",i_tmp++);
        printf("writedata :");

               if ((bytes = tcp_write(client_sockfd, s_buf,MAX_TRANSMIT_DATA_LEN,S_TIME_OUT)) <= 0) {
                       perror ("WWrite");
                       goto accept_again;

            }
            

        len+= bytes;
        printf("%d %d\n",bytes,len);
       
        bytes= tcp_read (client_sockfd, r_buf, 8,S_TIME_OUT);
           if ((bytes <= 0) || (strlen(r_buf)<= 0)) {
               perror ("read");
               printf(" %s %d The status of the client is %s\n",__FUNCTION__,__LINE__,"Exited!");
                   goto accept_again;
                   //break;
           }
           usleep(2);
        printf("%s %d The status of the client is %s\n",__FUNCTION__,__LINE__,r_buf);
 }
               close (client_sockfd);
               unlink ("server socket");
        gotoaccept_again;
}

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:989071次
    • 积分:17718
    • 等级:
    • 排名:第539名
    • 原创:741篇
    • 转载:522篇
    • 译文:0篇
    • 评论:105条
    最新评论