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

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: C/C++

2015-08-17 14:25:36


点击(此处)折叠或打开

  1. /*点对点server.c*/
  2.  #include<stdio.h>
  3.  #include<sys/types.h>
  4.  #include<string.h>
  5.  #include<sys/socket.h>
  6.  #include<unistd.h>
  7.  #include<stdlib.h>
  8.  #include<errno.h>
  9.  #include<netinet/in.h>
  10.  #include<arpa/inet.h>
  11.  #include<signal.h>/**/
  12.  void die(char *s)
  13.  {perror(s);
  14.  exit(0);
  15.  }
  16.  void handler(int sig)
  17.  {
  18.      printf("recv a sig=%d\n",sig);
  19.      exit(0);
  20.  }
  21. int main(void)
  22.  {    /*socket*/
  23.     int    listenfd;
  24.     if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
  25.         die("socket");
  26.         /*bind*/
  27.     struct sockaddr_in servaddr;
  28.     memset(&servaddr,0,sizeof(servaddr));
  29.     servaddr.sin_family=AF_INET;
  30.     servaddr.sin_port=htons(5188);
  31.     servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
  32.     /*servaddr.sin_addr.s_addr=inet_addr("127.0.0.1"");*/
  33.     /* if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) */
  34.     int yes=1;
  35.     if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
  36.         die("reuaddr");
  37.  
  38.     if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
  39.         die("bind");
  40.     /*listen*/
  41.     if(listen(listenfd,SOMAXCONN)<0)
  42.         die("listen");
  43.     /*accept*/
  44.     struct sockaddr_in peeraddr;
  45.     socklen_t peerlen=sizeof(peeraddr);
  46.     int conn;
  47.     if((conn=accept(listenfd,(struct sockaddr*)&peeraddr,&peerlen))<0)
  48.         die("accept");
  49.     printf("ip=%s port=%d\n",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port));
  50.     /*chat function*/
  51.     pid_t pid;
  52.     pid=fork();
  53.     if(pid==-1)
  54.         die("fork");
  55.     if(pid==0)
  56.     {    /*send process*/
  57.         signal(SIGUSR1,handler);
  58.         char sendbuf[1024]={0};
  59.         while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL)
  60.         {
  61.             write(conn,sendbuf,strlen(sendbuf));
  62.             memset(sendbuf,0,sizeof(sendbuf));
  63.         }
  64.         printf("child close\n");
  65.         exit(0);
  66.     }
  67.     else{
  68.         /*recv process*/
  69.         char recvbuf[1024];
  70.         while(1)
  71.         {    memset(recvbuf,0,sizeof(recvbuf));
  72.         int ret=read(conn,recvbuf,sizeof(recvbuf));
  73.         if(ret==-1)
  74.             die("read");
  75.         else if(ret==0){
  76.         printf("peer close\n");
  77.         break;
  78.         }
  79.         fputs(recvbuf,stdout);    
  80.     }
  81.         printf("parent close\n");
    kill(pid,SIGUSR1);
    exit(0);
    }

    /*close socket*/
    close(conn);
    close(listenfd);
    return 0;
    }

点击(此处)折叠或打开

  1. /*点对点client.c*/
  2. #include<stdio.h>
  3. #include<sys/types.h>
  4. #include<string.h>
  5. #include<sys/socket.h>
  6. #include<unistd.h>
  7. #include<stdlib.h>
  8. #include<errno.h>
  9. #include<netinet/in.h>
  10. #include<arpa/inet.h>
  11. #include<signal.h>
  12. void die(char *s)
  13. {perror(s);
  14. exit(0);
  15. }
  16. void handler(int sig)
  17. {
  18.     printf("recv a sig=%d\n",sig);
  19.     exit(0);
  20. }
  21. int main(void)
  22. {
  23.     /*socket*/
  24.     int sock;
  25.     if((sock=socket(AF_INET,SOCK_STREAM,0))<0)
  26.         die("socket");
  27.     struct sockaddr_in servaddr;
  28.     memset(&servaddr,0,sizeof(servaddr));
  29.     servaddr.sin_family=AF_INET;
  30.     servaddr.sin_port=htons(5188);
  31.     /* servaddr.sin_addr.s_addr=htonl(INADDR_ANY); */
  32.     servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
  33.     /*connect*/
  34.     if(connect(sock,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
  35.         die("connect");
  36.     pid_t pid;
  37.     pid=fork();
  38.     if(pid==-1)
  39.         die("fork");
  40.     /*recv process*/
  41.     if(pid==0)
  42.     {
  43.         char recvbuf[1024];
  44.         while(1)
  45.         {
  46.             memset(recvbuf,0,sizeof(recvbuf));
  47.             int ret=read(sock,recvbuf,sizeof(recvbuf));
  48.             if(ret==-1)
  49.                 die("read");
  50.             else if(ret==0)
  51.             {printf("peer close\n");
  52.             break;}
  53.             fputs(recvbuf,stdout);
  54.         }
  55.         close(sock);
  56.         kill(getppid(),SIGUSR1);
  57.     }
  58.     /*send process*/
  59.     else {
  60.         signal(SIGUSR1,handler);
  61.         char sendbuf[1024]={0};
  62.         while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL)
  63.         {
  64.         write(sock,sendbuf,strlen(sendbuf));
  65.         memset(sendbuf,0,sizeof(sendbuf));
  66.         }
  67.         }
  68.     
  69.     
  70.     return 0;
  71. }


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