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

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: C/C++

2015-08-28 14:00:14


点击(此处)折叠或打开

  1. /*select server*/
  2. #include<stdio.h>
  3. #include<sys/types.h>
  4. #include<sys/select.h>
  5. #include<string.h>
  6. #include<sys/socket.h>
  7. #include<unistd.h>
  8. #include<stdlib.h>
  9. #include<errno.h>
  10. #include<netinet/in.h>
  11. #include<arpa/inet.h>
  12. #define MAXLINE 1024
  13. #include<signal.h>
  14. void die(char *s)
  15. {perror(s);
  16. exit(0);
  17. }
  18. /*readn*/
  19. ssize_t readn(int fd, void *buf,size_t count)
  20. {
  21.     size_t nleft=count;
  22.     ssize_t nread;
  23.     char *bufp=(char*)buf;
  24.     while(nleft>0)
  25.     {
  26.         if((nread=read(fd,bufp,nleft))<0)
  27.         {
  28.             if(errno==EINTR)
  29.                 continue;
  30.             return -1;
  31.             }
  32.         else if(nread==0)
  33.         return count-nleft;
  34.         bufp+=nread;
  35.         nleft-=nread;
  36.         }
  37.     return count;
  38. }
  39. /*writen*/
  40. ssize_t writen(int fd,void* buf,size_t count)
  41. {
  42.     size_t nleft=count;
  43.     ssize_t nwrite;
  44.     char *bufp=(char*)buf;
  45.     while(nleft>0)
  46.     {
  47.         if((nwrite=write(fd,bufp,nleft))<0)
  48.         {
  49.             if(errno==EINTR)
  50.                 continue;
  51.             return -1;
  52.         }
  53.         else if(nwrite==0)
  54.             continue;
  55.         bufp+=nwrite;
  56.         nleft-=nwrite;
  57.     }
  58.     return count;
  59. }
  60. /*recv_peek function*/
  61. ssize_t recv_peek(int sockfd,void*buf,size_t len)
  62. {
  63.     while(1)
  64.     {
  65.         int ret=recv(sockfd,buf,len,MSG_PEEK);
  66.         if(ret==-1&errno==EINTR)
  67.             continue;
  68.         return ret;
  69.     }
  70. }
  71.     /*readline function*/
  72. ssize_t readline(int sockfd,void *buf,size_t len)
  73. {
  74.     int ret;
  75.     int nread;
  76.     char *bufp=buf;
  77.     int nleft=MAXLINE;
  78.     while(1)
  79.     {
  80.         ret=recv_peek(sockfd,bufp,nleft);
  81.         if(ret<0)
  82.             return ret;
  83.         else if(ret==0)
  84.             return ret;
  85.         nread=ret;
  86.         int i;
  87.         for(i=0;i<nread;i++)
  88.         {
  89.         if(bufp[i]=='\n')
  90.             {
  91.                 ret=readn(sockfd,bufp,i+1);
  92.                 if(ret!=i+1)
  93.                     exit(1);
  94.                 return ret;
  95.             }
  96.         }
  97.         if(nread>nleft)
  98.             exit(1);
  99.         nleft-=nread;
  100.         ret=readn(sockfd,bufp,nread);
  101.         if(ret!=nread)
  102.             exit(1);
  103.         bufp+=nread;
  104.     }
  105.     return -1;
  106. }
  107. /*避免僵尸进程*/
  108. void handle_sigchld(int sig)
  109. {
  110.     while(waitpid(-1,NULL,WNOHANG)>0)
  111.         ;
  112. }
  113. /* void echo(int conn)
  114. {
  115.     char recvbuf[1024];
  116.     while(1)
  117.     {    memset(recvbuf,0,sizeof(recvbuf));
  118.         int ret=readline(conn,recvbuf,1024);
  119.         if(ret==-1)
  120.             die("readline");
  121.         if(ret==0)
  122.         {
  123.             printf("client close\n");
  124.             break;
  125.         }
  126.             fputs(recvbuf,stdout);
  127.             writen(conn,recvbuf,strlen(recvbuf));
  128.     }
  129. } */
  130. int main(void)
  131. {    /*signal处理僵尸进程*/
  132.     signal(SIGCHLD,handle_sigchld);
  133.     /*socket*/
  134.     int listenfd;
  135.     if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
  136.         die("socket");
  137.     /*bind*/
  138.     struct sockaddr_in servaddr;
  139.     memset(&servaddr,0,sizeof(servaddr));
  140.     servaddr.sin_family=AF_INET;
  141.     servaddr.sin_port=htons(5188);
  142.     servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
  143.     /*servaddr.sin_addr.s_addr=inet_addr("127.0.0.1"");*/
  144.     /* if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) */
  145.         int yes=1;
  146.         if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
  147.             die("reuseaddr");
  148.     if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
  149.             die("bind");
  150.         /*listen*/
  151.     if(listen(listenfd,SOMAXCONN)<0)
  152.         die("listen");
  153.     /*accept*/
  154.     struct sockaddr_in peeraddr;
  155.     socklen_t peerlen;
  156.     int conn;

  157. int client[FD_SETSIZE];
  158. int i;
  159. for(i=0;i<FD_SETSIZE;i++)
  160.     client[i]=-1;
  161. /*select*/
  162. int maxi=0;
  163. int nready;//检测到的描述符
  164. int maxfd=listenfd;
  165. fd_set rset;
  166. fd_set allset;
  167. FD_ZERO(&rset);
  168. FD_ZERO(&allset);
  169. FD_SET(listenfd,&allset);
  170. while(1){
  171.     rset=allset;
  172.     nready=select(maxfd+1,&rset,NULL,NULL,NULL);
  173.     if(nready==-1)
  174.     {
  175.         if(errno==EINTR)
  176.             continue;
  177.         else die("select");
  178.     }
  179.     if(nready==0)
  180.         continue;
  181.     /*z自己写入数据*/
  182.     if(FD_ISSET(listenfd,&rset))
  183.     {    peerlen=sizeof(peeraddr);
  184.         conn=accept(listenfd,(struct sockaddr*)&peeraddr,&peerlen);
  185.         if(conn==-1)
  186.             die("accept");
  187.         for(i=0;i<FD_SETSIZE;i++)
  188.         {
  189.             if(client[i]<0)
  190.             {client[i]=conn;
  191.                 if(i>maxi)
  192.                 maxi=i;
  193.                 break;
  194.             }
  195.         }
  196.         if(i==FD_SETSIZE)
  197.         {
  198.             fprintf(stdout,"too many client");
  199.             exit(1);
  200.         }
  201.         printf("ip=%s port=%d\n",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port));
  202.     FD_SET(conn,&allset);
  203.         if(conn>maxfd)
  204.             maxfd=conn;
  205.         if(--nready<=0)
  206.             continue;
  207.     }
  208.     /*数据来自对方的发送*/
  209.     for(i=0;i<=maxi;i++)
  210.     {
  211.         conn=client[i];
  212.         if(conn==-1)
  213.             continue;
  214.         if(FD_ISSET(conn,&rset))
  215.         {
  216.             /*读的集合来自客户端,进行回射服务*/
  217.             char recvbuf[1024]={0};
  218.         int ret=readline(conn,recvbuf,1024);
  219.         if(ret==-1)
  220.             die("readline");
  221.         if(ret==0)
  222.         {
  223.             printf("client close\n");
    /*如果错误,从集合当中清除*/
    FD_CLR(conn,&allset);
    client[i]=-1;
    }
    fputs(recvbuf,stdout);
    writen(conn,recvbuf,strlen(recvbuf));
    if(--nready<=0)
    break;
    }
    }




    }
    /*close socket*/ 
    return 0;
    }

点击(此处)折叠或打开

  1. /*echo_client.c*/
  2. /*select改进的client*/
  3. #include<stdio.h>
  4. #include<sys/types.h>
  5. #include<string.h>
  6. #include<sys/socket.h>
  7. #include<unistd.h>
  8. #include<stdlib.h>
  9. #include<errno.h>
  10. #include<netinet/in.h>
  11. #include<arpa/inet.h>
  12. #include <netdb.h>
  13. #include<signal.h>
  14. #define MAXLINE 1024
  15. void die(char *s)
  16. {perror(s);
  17. exit(0);
  18. }
  19. ssize_t readn(int fd, void *buf,size_t count)
  20. {
  21.     size_t nleft=count;
  22.     ssize_t nread;
  23.     char *bufp=(char*)buf;
  24.     while(nleft>0)
  25.     {
  26.         if((nread=read(fd,bufp,nleft))<0)
  27.         {
  28.             if(errno==EINTR)
  29.                 continue;
  30.             return -1;
  31.         }
  32.         else if(nread==0)
  33.             return count-nleft;
  34.         bufp+=nread;
  35.         nleft-=nread;
  36.     }
  37.     return count;
  38.     
  39. }
  40. /*writen*/
  41. ssize_t writen(int fd,void* buf,size_t count)
  42. {
  43.     size_t nleft=count;
  44.     ssize_t nwrite;
  45.     char *bufp=(char*)buf;
  46.     while(nleft>0)
  47.     {
  48.         if((nwrite=write(fd,bufp,nleft))<0)
  49.         {
  50.             if(errno==EINTR)
  51.                 continue;
  52.             return -1;
  53.         }
  54.         else if(nwrite==0)
  55.             continue;
  56.         bufp+=nwrite;
  57.         nleft-=nwrite;
  58.     }
  59.     return count;
  60. }
  61. ssize_t recv_peek(int sockfd,void*buf,size_t len)
  62. {
  63.     while(1)
  64.     {
  65.         int ret=recv(sockfd,buf,len,MSG_PEEK);
  66.         if(ret==-1&errno==EINTR)
  67.             continue;
  68.         return ret;
  69.         
  70.     }
  71. }
  72. /*readline function*/
  73. ssize_t readline(int sockfd,void *buf,size_t len)
  74. {
  75.     int ret;
  76.     int nread;
  77.     char *bufp=buf;
  78.     int nleft=MAXLINE;
  79.     while(1)
  80.     {
  81.         ret=recv_peek(sockfd,bufp,nleft);
  82.         if(ret<0)
  83.             return ret;
  84.         else if(ret==0)
  85.             return ret;
  86.         nread=ret;
  87.         int i;
  88.         for(i=0;i<nread;i++)
  89.         {
  90.             if(bufp[i]=='\n')
  91.             {
  92.                 ret=readn(sockfd,bufp,i+1);
  93.                 if(ret!=i+1)
  94.                     exit(1);
  95.                 return ret;
  96.             }
  97.         }
  98.         if(nread>nleft)
  99.             exit(1);
  100.         nleft-=nread;
  101.         ret=readn(sockfd,bufp,nread);
  102.         if(ret!=nread)
  103.             exit(1);
  104.         bufp+=nread;
  105.     }
  106.     return -1;
  107. }
  108. void handle_sigchld(int sig)
  109. {
  110.     printf("recv a sig=%d\n",sig);
  111. }
  112. void echo(int sock)
  113. {
  114.     /* char sendbuf[1024]={0};
  115.     char recvbuf[1024]={0};
  116.     while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL)
  117.     {
  118.         writen(sock,sendbuf,strlen(sendbuf));
  119.         int ret=readline(sock,recvbuf,sizeof(recvbuf));
  120.         if(ret==-1)
  121.                 die("read");
  122.         else if(ret==0)
  123.         {
  124.             printf("client close\n");
  125.             break;
  126.         }
  127.         fputs(recvbuf,stdout);
  128.         memset(sendbuf,0,sizeof(sendbuf));
  129.         memset(recvbuf,0,sizeof(recvbuf));
  130.     }
  131.     close(sock); */
  132.     /*select*/
  133.     char recvbuf[1024]={0};
  134.     char sendbuf[1024]={0};
  135.     fd_set rset;
  136.     FD_ZERO(&rset);
  137.     int nready;
  138.     int maxfd;
  139.     int fd_stdin=fileno(stdin);
  140.     if(fd_stdin>sock)
  141.             maxfd=fd_stdin;
  142.         else
  143.             maxfd=sock;
  144.     while(1)
  145.     {    FD_SET(fd_stdin,&rset);
  146.         FD_SET(sock,&rset);
  147.         nready=select(maxfd+1,&rset,NULL,NULL,NULL);
  148.         if(nready==-1)
  149.             die("select");
  150.         if(nready==0)
  151.             continue;
  152.         /*sock产生可读事件*/
  153.         if(FD_ISSET(sock,&rset))
  154.         {
  155.             int ret=readline(sock,recvbuf,1024);
  156.         if(ret==-1)
  157.                 die("readline");
  158.         if(ret==0)
  159.         {
  160.             printf("server close\n");
  161.             break;
  162.         }
  163.             fputs(recvbuf,stdout);
  164.             memset(recvbuf,0,sizeof(recvbuf));
  165.         }
  166.         /*stdin*/
  167.         if(FD_ISSET(fd_stdin,&rset))
  168.         {
  169.             if(fgets(sendbuf,sizeof(sendbuf),stdin)==NULL)
  170.             break;
  171.             writen(sock,sendbuf,strlen(sendbuf));
  172.             memset(sendbuf,0,sizeof(sendbuf));
  173.         }

  174.     }
  175.     close(sock);
  176. }
  177. int main(void)
  178. {    signal(SIGPIPE,SIG_IGN);
  179.     int sock;
  180.     int i;
  181.     
  182.     /*socket*/
  183.     if((sock=socket(AF_INET,SOCK_STREAM,0))<0)
  184.         die("socket");
  185.     struct sockaddr_in servaddr;
  186.     memset(&servaddr,0,sizeof(servaddr));
  187.     servaddr.sin_family=AF_INET;
  188.     servaddr.sin_port=htons(5188);
  189.     /* servaddr.sin_addr.s_addr=htonl(INADDR_ANY); */
  190.     servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
  191.     /*sockect*/
  192.     if(connect(sock,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
  193.         die("connect");
  194.     /*getsockname function*/
  195.     /*return local addr port*/
  196.     struct sockaddr_in localaddr;
  197.     socklen_t addrlen=sizeof(localaddr);
  198.     if(getsockname(sock,(struct sockaddr*)&localaddr,&addrlen)<0)
  199.         die("getsockname");
  200.     printf("ip=%s port=%d\n",inet_ntoa(localaddr.sin_addr),ntohs(localaddr.sin_port));

  201.     

  202.     echo(sock);
  203.     return 0;
  204. }


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