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

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: C/C++

2015-08-17 17:08:58


点击(此处)折叠或打开

  1. /*解决粘包问题*/
  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. void die(char *s)
  12. {perror(s);
  13. exit(0);
  14. }
  15. struct packet
  16. {
  17.     int len;
  18.     char buf[1024];
  19. };

  20. /*readn*/
  21. ssize_t readn(int fd, void *buf,size_t count)
  22. {
  23.     size_t nleft=count;
  24.     ssize_t nread;
  25.     char *bufp=(char*)buf;
  26.     while(nleft>0)
  27.     {
  28.         if((nread=read(fd,bufp,nleft))<0)
  29.         {
  30.             if(errno==EINTR)
  31.                 continue;
  32.             return -1;
  33.         }
  34.         else if(nread==0)
  35.             return count-nleft;
  36.         bufp+=nread;
  37.         nleft-=nread;
  38.     }
  39.     return count;
  40.     
  41. }
  42. /*writen*/
  43. ssize_t writen(int fd,void* buf,size_t count)
  44. {
  45.     size_t nleft=count;
  46.     ssize_t nwrite;
  47.     char *bufp=(char*)buf;
  48.     while(nleft>0)
  49.     {
  50.         if((nwrite=write(fd,bufp,nleft))<0)
  51.         {
  52.             if(errno==EINTR)
  53.                 continue;
  54.             return -1;
  55.         }
  56.         else if(nwrite==0)
  57.             continue;
  58.         bufp+=nwrite;
  59.         nleft-=nwrite;
  60.     }
  61.     return count;
  62. }
  63. void do_service(int conn)
  64. {
  65.     struct packet recvbuf;
  66.     int n;
  67.     while(1)
  68.     {    memset(&recvbuf,0,sizeof(recvbuf));
  69.         int ret=readn(conn,&recvbuf.len,4);
  70.         if(ret==-1)
  71.                 die("read");
  72.         else if(ret<4)
  73.         {
  74.             printf("client close\n");
  75.             break;
  76.         }
  77.         n=ntohl(recvbuf.len);
  78.         ret=readn(conn,recvbuf.buf,n);
  79.         if(ret==-1)
  80.                 die("read");
  81.         else if(ret<n)
  82.         {
  83.             printf("client close\n");
  84.             break;
  85.         }
  86.             fputs(recvbuf.buf,stdout);
  87.             writen(conn,&recvbuf,4+n);
  88.     }
  89. }
  90. int main(void)
  91. {
  92.     /*socket*/
  93.     int listenfd;
  94.     if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
  95.         die("socket");
  96.     /*bind*/
  97.     struct sockaddr_in servaddr;
  98.     memset(&servaddr,0,sizeof(servaddr));
  99.     servaddr.sin_family=AF_INET;
  100.     servaddr.sin_port=htons(5188);
  101.     servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
  102.     /*servaddr.sin_addr.s_addr=inet_addr("127.0.0.1"");*/
  103.     /* if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) */
  104.     int yes=1;
  105.         if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
  106.             die("reuseaddr");
  107.     if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
  108.         die("bind");
  109.     /*listen*/
  110.     if(listen(listenfd,SOMAXCONN)<0)
  111.         die("listen");
  112.     /*accept*/
  113.     struct sockaddr_in peeraddr;
  114.     socklen_t peerlen=sizeof(peeraddr);
  115.     int conn;
  116.     /*fork 创建多个进程*/
  117.     pid_t pid;
  118.     while(1){
  119.     if((conn=accept(listenfd,(struct sockaddr*)&peeraddr, &peerlen))<0)
  120.         die("accept");
  121.     printf("ip=%s port=%d\n",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port));
  122.     /*echo function*/
  123.     pid=fork();
  124.     if(pid==-1)
  125.         die("fork");
    if(pid==0)
    {
    close(listenfd);
    do_service(conn);
    exit(0);
    }
    else
    close(conn);
    }
    /*close socket*/
    return 0;
    }

点击(此处)折叠或打开

  1. /*echo_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. void die(char *s)
  12. {perror(s);
  13. exit(0);
  14. }
  15. struct packet
  16. {
  17.     int len;
  18.     char buf[1024];
  19. };

  20. ssize_t readn(int fd, void *buf,size_t count)
  21. {
  22.     size_t nleft=count;
  23.     ssize_t nread;
  24.     char *bufp=(char*)buf;
  25.     while(nleft>0)
  26.     {
  27.         if((nread=read(fd,bufp,nleft))<0)
  28.         {
  29.             if(errno==EINTR)
  30.                 continue;
  31.             return -1;
  32.         }
  33.         else if(nread==0)
  34.             return count-nleft;
  35.         bufp+=nread;
  36.         nleft-=nread;
  37.     }
  38.     return count;
  39.     
  40. }
  41. /*writen*/
  42. ssize_t writen(int fd,void* buf,size_t count)
  43. {
  44.     size_t nleft=count;
  45.     ssize_t nwrite;
  46.     char *bufp=(char*)buf;
  47.     while(nleft>0)
  48.     {
  49.         if((nwrite=write(fd,bufp,nleft))<0)
  50.         {
  51.             if(errno==EINTR)
  52.                 continue;
  53.             return -1;
  54.         }
  55.         else if(nwrite==0)
  56.             continue;
  57.         bufp+=nwrite;
  58.         nleft-=nwrite;
  59.     }
  60.     return count;
  61. }
  62. int main(void)
  63. {
  64.     /*socket*/
  65.     int sock;
  66.     if((sock=socket(AF_INET,SOCK_STREAM,0))<0)
  67.         die("socket");
  68.     struct sockaddr_in servaddr;
  69.     memset(&servaddr,0,sizeof(servaddr));
  70.     servaddr.sin_family=AF_INET;
  71.     servaddr.sin_port=htons(5188);
  72.     /* servaddr.sin_addr.s_addr=htonl(INADDR_ANY); */
  73.     servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
  74.     /*connect*/
  75.     if(connect(sock,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
  76.         die("connect");
  77.     struct packet sendbuf;
  78.     struct packet recvbuf;
  79.     memset(&sendbuf,0,sizeof(sendbuf));
  80.     memset(&recvbuf,0,sizeof(recvbuf));
  81.     while(fgets(sendbuf.buf,sizeof(sendbuf.buf),stdin)!=NULL)
  82.     {
  83.         int n=strlen(sendbuf.buf);
  84.         sendbuf.len=htonl(n);
  85.         writen(sock,&sendbuf,4+n);
  86.         int ret=readn(sock,&recvbuf.len,4);
  87.         if(ret==-1)
  88.                 die("read");
  89.         else if(ret<4)
  90.         {
  91.             printf("client close\n");
  92.             break;
  93.         }
  94.             n=ntohl(recvbuf.len);
  95.             ret=readn(sock,recvbuf.buf,n);
  96.             if(ret==-1)
  97.                 die("read");
  98.         else if(ret<n)
  99.         {
  100.             printf("client close\n");
  101.             break;
  102.         }
  103.         fputs(recvbuf.buf,stdout);
  104.         memset(&sendbuf,0,sizeof(sendbuf));
  105.         memset(&recvbuf,0,sizeof(recvbuf));
  106.     }
  107.     close(sock);
  108.     return 0;
  109. }


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