Chinaunix首页 | 论坛 | 博客
  • 博客访问: 329448
  • 博文数量: 49
  • 博客积分: 653
  • 博客等级: 上士
  • 技术积分: 646
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-01 22:43
文章分类

全部博文(49)

文章存档

2018年(1)

2017年(4)

2015年(1)

2014年(6)

2013年(8)

2012年(24)

2011年(5)

分类: C/C++

2012-01-17 17:04:32

client.cc

  1. /*
  2.  * client.cc
  3.  *
  4.  * Created on: 2012-1-17
  5.  * Author: simondu
  6.  */

  7. #include "head.h"

  8. #define MAXLINE 80
  9. #define SERV_PORT 8000

  10. int main(int argc, char *argv[])
  11. {
  12.     struct sockaddr_in servaddr;
  13.     char buf[MAXLINE];
  14.     int sockfd, n;
  15.     char *str;

  16.     printf("starting... \n");
  17.     if (argc != 2) {
  18.         fputs("usage: ./client message\n", stderr);
  19.         exit(1);
  20.     }
  21.     str = argv[1];
  22.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  23.     bzero(&servaddr, sizeof(servaddr));
  24.     servaddr.sin_family = AF_INET;
  25.     inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
  26.     servaddr.sin_port = htons(SERV_PORT);
  27.     connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

  28.     while (fgets(buf, MAXLINE, stdin) != NULL) {
  29.             write(sockfd, buf, strlen(buf));
  30.             n = read(sockfd, buf, MAXLINE);
  31.             if (n == 0)
  32.                 printf("the other side has been closed.\n");
  33.             else
  34.                 //Write(STDOUT_FILENO, buf, n);
  35.                 printf("%s\n",buf);
  36.         }

  37.     close(sockfd);
  38.     return 0;
  39. }



server.cc
  1. /*
  2.  * server.cc
  3.  *
  4.  * Created on: 2012-1-17
  5.  * Author: simondu
  6.  */
  7. #include "head.h"

  8. #define MAXLINE 80
  9. #define SERV_PORT 8000
  10. int main(void )
  11. {
  12.     int i, maxi, maxfd, listenfd, connfd, sockfd;
  13.     int nready, client[FD_SETSIZE];
  14.     ssize_t n;
  15.     fd_set rset, allset;
  16.     char buf[MAXLINE];
  17.     char str[INET_ADDRSTRLEN];
  18.     socklen_t cliaddr_len;
  19.     struct sockaddr_in    cliaddr, servaddr;

  20.     listenfd = socket(AF_INET, SOCK_STREAM, 0);

  21.     bzero(&servaddr, sizeof(servaddr));
  22.     servaddr.sin_family = AF_INET;
  23.     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  24.     servaddr.sin_port = htons(SERV_PORT);

  25.     bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

  26.     listen(listenfd, 20);

  27.     maxfd = listenfd;        /* initialize */
  28.     maxi = -1;            /* index into client[] array */
  29.     for (i = 0; i < FD_SETSIZE; i++)
  30.         client[i] = -1;    /* -1 indicates available entry */
  31.     FD_ZERO(&allset);
  32.     FD_SET(listenfd, &allset);
  33.     printf("Starting ... \n");
  34.     for ( ; ; ) {
  35.         rset = allset;    /* structure assignment */
  36.         nready = select(maxfd+1, &rset, NULL, NULL, NULL);
  37.         if (nready < 0)
  38.         {
  39.             printf("select error");
  40.             exit(1);
  41.         }

  42.         if (FD_ISSET(listenfd, &rset)) { /* new client connection */
  43.             cliaddr_len = sizeof(cliaddr);
  44.             connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len);

  45.             printf("received from %s at PORT %d\n",
  46.              inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
  47.              ntohs(cliaddr.sin_port));

  48.             for (i = 0; i < FD_SETSIZE; i++)
  49.                 if (client[i] < 0) {
  50.                     client[i] = connfd; /* save descriptor */
  51.                     break;
  52.                 }

  53.             if (i == FD_SETSIZE) {
  54.                 fputs("too many clients\n", stderr);
  55.                 exit(1);
  56.             }

  57.             FD_SET(connfd, &allset);    /* add new descriptor to set */
  58.             if (connfd > maxfd)
  59.                 maxfd = connfd; /* for select */
  60.             if (i > maxi)
  61.                 maxi = i;    /* max index in client[] array */

  62.             if (--nready == 0)
  63.                 continue;    /* no more readable descriptors */
  64.         }

  65.         for (i = 0; i <= maxi; i++) {    /* check all clients for data */
  66.             if ( (sockfd = client[i]) < 0)
  67.             {
  68.                 printf("sockfd = client[i] = %d\n",sockfd);
  69.                 continue;
  70.             }
  71.             if (FD_ISSET(sockfd, &rset)) {
  72.                 if ( (n = read(sockfd, buf, MAXLINE)) == 0) {
  73.                     /* connection closed by client */
  74.                     close(sockfd);
  75.                     FD_CLR(sockfd, &allset);
  76.                     client[i] = -1;
  77.                 } else {
  78.                     int j;
  79.                     for (j = 0; j < n; j++)
  80.                         buf[j] = toupper(buf[j]);
  81.                     int rettest = write(sockfd, buf, n);
  82.                     printf("%d\n",rettest);
  83.                 }
  84.                 if (--nready == 0)
  85.                     break;    /* no more readable descriptors */
  86.             }
  87.         }
  88.     }

  89. }




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