Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6543329
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类: C/C++

2016-01-19 15:06:00



server.c

点击(此处)折叠或打开

  1. // gcc server.c -o server
  2. // indent -npro -kr -i8 -ts8 -sob -l280 -ss -ncs -cp1 *

  3. /***************************************************************************/
  4. /* */
  5. /* Server program which wait for the client to connect and reads the data */
  6. /* using non-blocking socket. */
  7. /* The reading of non-blocking sockets is done in a loop until data */
  8. /* arrives to the sockfd. */
  9. /* */
  10. /* based on Beej's program - look in the simple TCP server for further doc.*/
  11. /* */
  12. /* */
  13. /***************************************************************************/

  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <sys/socket.h>
  22. #include <sys/wait.h>
  23. #include <fcntl.h>        /* Added for the nonblocking socket */

  24. #define MYPORT 3456        /* the port users will be connecting to */
  25. #define BACKLOG 10        /* how many pending connections queue will hold */

  26. int main()
  27. {
  28.     int sockfd, new_fd;    /* listen on sock_fd, new connection on new_fd */
  29.     struct sockaddr_in my_addr;    /* my address information */
  30.     struct sockaddr_in their_addr;    /* connector's address information */
  31.     int sin_size;
  32.     char string_read[255];
  33.     int n, i;
  34.     int last_fd;        /* Thelast sockfd that is connected */

  35.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  36.         perror("socket");
  37.         exit(1);
  38.     }

  39.     last_fd = sockfd;

  40.     my_addr.sin_family = AF_INET;    /* host byte order */
  41.     my_addr.sin_port = htons(MYPORT);    /* short, network byte order */
  42.     my_addr.sin_addr.s_addr = INADDR_ANY;    /* auto-fill with my IP */
  43.     bzero(&(my_addr.sin_zero), 8);    /* zero the rest of the struct */

  44.     if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
  45.         perror("bind");
  46.         exit(1);
  47.     }

  48.     if (listen(sockfd, BACKLOG) == -1) {
  49.         perror("listen");
  50.         exit(1);
  51.     }

  52.     if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
  53.         perror("accept");
  54.     }
  55.     fcntl(last_fd, F_SETFL, O_NONBLOCK);    /* Change the socket into non-blocking state */
  56.     fcntl(new_fd, F_SETFL, O_NONBLOCK);    /* Change the socket into non-blocking state */

  57.     while (1) {
  58.         for (i = sockfd; i <= last_fd; i++) {
  59.             printf("Round number %d\n", i);
  60.             if (i = sockfd) {
  61.                 sin_size = sizeof(struct sockaddr_in);
  62.                 if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
  63.                     perror("accept");
  64.                 }
  65.                 printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));
  66.                 fcntl(new_fd, F_SETFL, O_NONBLOCK);
  67.                 last_fd = new_fd;
  68.             } else {
  69.                 n = recv(new_fd, string_read, sizeof(string_read), 0);
  70.                 if (n < 1) {
  71.                     perror("recv - non blocking \n");
  72.                     printf("Round %d, and the data read size is: n=%d \n", i, n);
  73.                 } else {
  74.                     string_read[n] = '\0';
  75.                     printf("The string is: %s \n", string_read);
  76.                     if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
  77.                         perror("send");
  78.                 }
  79.             }
  80.         }
  81.     }
  82.     return 0;
  83. }
client.c

点击(此处)折叠或打开

  1. // gcc client.c -o client
  2. // indent -npro -kr -i8 -ts8 -sob -l280 -ss -ncs -cp1 *

  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <netdb.h>
  8. #include <sys/types.h>
  9. #include <netinet/in.h>
  10. #include <sys/socket.h>
  11. #include <unistd.h>

  12. #define PORT 3456        /* the port client will be connecting to */

  13. #define MAXDATASIZE 100        /* max number of bytes we can get at once */

  14. int main(int argc, char *argv[])
  15. {
  16.     int sockfd, numbytes;
  17.     char buf[MAXDATASIZE];
  18.     struct hostent *he;
  19.     struct sockaddr_in their_addr;    /* connector's address information */

  20.     if (argc != 2) {
  21.         fprintf(stderr, "usage: client hostname\n");
  22.         exit(1);
  23.     }

  24.     if ((he = gethostbyname(argv[1])) == NULL) {    /* get the host info */
  25.         herror("gethostbyname");
  26.         exit(1);
  27.     }

  28.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  29.         perror("socket");
  30.         exit(1);
  31.     }

  32.     their_addr.sin_family = AF_INET;    /* host byte order */
  33.     their_addr.sin_port = htons(PORT);    /* short, network byte order */
  34.     their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  35.     bzero(&(their_addr.sin_zero), 8);    /* zero the rest of the struct */

  36.     if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
  37.         perror("connect");
  38.         exit(1);
  39.     }
  40.     /* sleep(5); */
  41.     while (1) {
  42.         if (send(sockfd, "Hello, world!\n", 14, 0) == -1)
  43.             perror("send");
  44.         printf("In loop \n");
  45.         sleep(2);
  46.     }
  47.     close(sockfd);

  48.     return 0;
  49. }



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