Chinaunix首页 | 论坛 | 博客
  • 博客访问: 275726
  • 博文数量: 113
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1044
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-15 16:09
文章分类

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: LINUX

2015-04-05 16:30:15

实现点对点聊天功能。其中,signal函数实现对僵尸进程的处理。
server.c程序:

点击(此处)折叠或打开

  1. #include <sys/socket.h>
  2. #include <stdio.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <fcntl.h>
  9. #include <sys/shm.h>
  10. #include<signal.h>
  11. #define MYPORT 8887
  12. #define QUEUE 20
  13. #define BUFFER_SIZE 1024
  14. void handler(int sig)   //信号的处理,避免出现僵尸进程。
  15. {
  16.     printf("recv a sig=%d\n",sig);
  17.     exit(0);
  18. }

  19. int main()
  20. {
  21.     ///定义sockfd
  22.     int server_sockfd = socket(AF_INET,SOCK_STREAM, 0);

  23.     ///定义sockaddr_in
  24.     struct sockaddr_in server_sockaddr;
  25.     server_sockaddr.sin_family = AF_INET;
  26.     server_sockaddr.sin_port = htons(MYPORT);
  27.     server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

  28.     ///bind,成功返回0,出错返回-1
  29.     if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1)
  30.     {
  31.         perror("bind");
  32.         exit(1);
  33.     }

  34.     ///listen,成功返回0,出错返回-1
  35.     if(listen(server_sockfd,QUEUE) == -1)
  36.     {
  37.         perror("listen");
  38.         exit(1);
  39.     }

  40.     ///客户端套接字
  41.     
  42.     struct sockaddr_in client_addr;
  43.     socklen_t length = sizeof(client_addr);

  44.     ///成功返回非负描述字,出错返回-1
  45.     int conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length);
  46.     if(conn<0)
  47.     {
  48.         perror("connect");
  49.         exit(1);
  50.     }
  51.     pid_t pid;  //fork实现多进程
  52.     pid =fork();
  53.     if(pid ==-1)
  54.        {
  55.         perror("fork");
  56.         exit(1);
  57.        }
  58.     if(pid ==0) //子进程
  59.     {
  60.         signal(SIGUSR1,handler);
  61.         char sendbuf[BUFFER_SIZE] ={0};
  62.         while (fgets(sendbuf,sizeof(sendbuf),stdin) !=NULL)
  63.         {
  64.              write(conn,sendbuf,strlen(sendbuf));
  65.              memset(sendbuf,0,sizeof(sendbuf));
  66.         }
  67.         printf("child cose\n");
  68.         exit(0);
  69.     }
  70.      else  //父进程
  71.      {
  72.          char buffer[BUFFER_SIZE];    
  73.     
  74.     
  75.            while(1)
  76.           {
  77.             memset(buffer,0,sizeof(buffer));
  78.             int len = recv(conn, buffer, sizeof(buffer),0);
  79.             if(len==-1) {
  80.                 perror("fork");
  81.                 exit(1);
  82.                 }
  83.             else if(len ==0){
  84.                 printf("client close\n");
  85.          break;    
  86.              }
  87.          fputs(buffer, stdout);
  88.             
  89.           }
  90.          printf("parent close\n");
  91.          kill(pid,SIGUSR1);
  92.          exit(0);
  93.      }
  94.     close(conn);
  95.     close(server_sockfd);
  96.     return 0;
  97. }
client.c代码:

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <stdio.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <fcntl.h>
  10. #include <sys/shm.h>
  11. #include<signal.h>
  12. #define MYPORT 8887
  13. #define BUFFER_SIZE 1024
  14. void handler(int sig) //信号处理功能
  15. {
  16.     printf("recv a sig=%d",sig);
  17.     exit(0);
  18. }
  19. int main()
  20. {
  21.     ///定义sockfd
  22.     int sock_cli = socket(AF_INET,SOCK_STREAM, 0);

  23.     ///定义sockaddr_in
  24.     struct sockaddr_in servaddr;
  25.     memset(&servaddr, 0, sizeof(servaddr));
  26.     servaddr.sin_family = AF_INET;
  27.     servaddr.sin_port = htons(MYPORT); ///服务器端口
  28.     servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); ///服务器ip

  29.     ///连接服务器,成功返回0,错误返回-1
  30.     if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
  31.     {
  32.         perror("connect");
  33.         exit(1);
  34.     }
  35.      pid_t pid;
  36.      pid =fork();
  37.      if(pid ==-1)
  38.         {
  39.         perror("fork");
  40.         exit(1);
  41.          }
  42.      if(pid ==0) //子进程
  43.         { 
  44.          char recvbuf[BUFFER_SIZE];
  45.          while(1)
  46.          {
  47.              memset(recvbuf,0,sizeof(recvbuf));
  48.              int len =read(sock_cli,recvbuf,sizeof(recvbuf));
  49.              if (len ==-1){
  50.                 perror("read");
  51.                 exit(1);
  52.                 }
  53.                 else if(len ==0)
  54.                 {
  55.                     printf("client close\n");    
  56.                     break;
  57.                 }
  58.                 fputs(recvbuf,stdout);    
  59.          }
  60.          close(socket);
  61.          kill(getppid(),SIGUSR1);
  62.         
  63.      }
  64.      else  //父进程
  65.      {     
  66.             signal(SIGUSR1,handler);
  67.          char sendbuf[BUFFER_SIZE] ={0};
  68.             while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
  69.             {
  70.             send(sock_cli, sendbuf, strlen(sendbuf),0); ///发送
  71.             memset(sendbuf, 0, sizeof(sendbuf));
  72.            
  73.             }    
  74.         close(socket);
  75.         }
  76.   
  77.     
  78.     
  79.     return 0;
  80. }


阅读(1315) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~