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

原创 2013年12月02日 14:54:05

#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;
}

 

 

相关文章推荐

带客户端和服务器端的即时通信

  • 2010年12月09日 08:34
  • 178KB
  • 下载

JavaSE第一百一十讲:使用线程实现服务器端与客户端的双向通信

在上一讲中我们讲到了服务器端与客户端的双向通信的例子,显然这种双向通信不是非常好,因为服务端与客户端建立连接之后,服务端一定要等待客户端的输入然后自己再返回一个输出,这在实际的开发过程显然是没有任何意...

TCP通信 包括客户端和服务器端

  • 2015年10月29日 13:49
  • 40KB
  • 下载

客户端到服务器端的通信过程及原理

学习任何东西,我们只要搞清楚其原理,就会触类旁通。现在结和我所学,我想总结一下客户端到服务器端的通信过程。只有明白了原理,我们才会明白当我们程序开发过程中错误的问题会出现在那,才会更好的解决问题。 ...

Android客户端与服务器端通信Demo

  • 2016年02月19日 13:14
  • 1.61MB
  • 下载

C++服务器端+Android客户端的网络通信

今天晚上11点半,在学校断网后的十分钟,我终于实现了C++服务器端与Android客户端的通信。本来很简单的一件事,却因为Android Socket线程阻塞的问题弄了我两天。 好了不多说,直接...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:嵌入式 服务器端与客户端通信当客户端异常关闭的解决服务器跟着关闭一服务器
举报原因:
原因补充:

(最多只允许输入30个字)