Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1555961
  • 博文数量: 290
  • 博客积分: 3468
  • 博客等级: 中校
  • 技术积分: 3461
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-28 22:21
文章分类

全部博文(290)

文章存档

2016年(13)

2015年(3)

2014年(42)

2013年(67)

2012年(90)

2011年(75)

分类: 嵌入式

2012-07-06 18:11:24

TCP
server.c

点击(此处)折叠或打开

  1. /* Copyright(C) 2012 for free
  2.  * All right reserved
  3.  */
  4. /**
  5.  * @file server.c
  6.  * @synopsis tcp 测试例程
  7.  * @version 1.0
  8.  * @date 2012-07-06
  9.  */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/socket.h>
  15. #include <ctype.h>
  16. #include <netinet/in.h>
  17. #include <arpa/inet.h>

  18. #define MAXLINE 80
  19. #define SERV_PORT 8000

  20. int main(int argc,char *argv[])
  21. {
  22.     struct sockaddr_in servaddr, cliaddr;
  23.     socklen_t cliaddr_len;
  24.     int sockfd, connfd;
  25.     char buf[MAXLINE];
  26.     char str[INET_ADDRSTRLEN];
  27.     int i, n;

  28.     sockfd = socket(AF_INET, SOCK_STREAM, 0);

  29.     bzero(&servaddr, sizeof(servaddr));
  30.     servaddr.sin_family = AF_INET;
  31.     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  32.     servaddr.sin_port = htons(SERV_PORT);
  33.     
  34.     i = 1;
  35.     setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i));
  36.     bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

  37.     listen(sockfd, 20);

  38.     printf("Accepting connections ...\n");
  39.     while (1) {
  40.         memset(buf,0,sizeof(buf));
  41.         cliaddr_len = sizeof(cliaddr);
  42.         connfd = accept(sockfd,
  43.                 (struct sockaddr *)&cliaddr, &cliaddr_len);
  44.     
  45.         n = read(connfd, buf, MAXLINE);
  46.         printf("received from %s at PORT %d : %s\n",
  47.          inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
  48.          ntohs(cliaddr.sin_port),buf);
  49.     
  50.         for (i = 0; i < n; i++)
  51.             buf[i] = toupper(buf[i]);
  52.         write(connfd, buf, n);
  53.         close(connfd);
  54.     }

  55.     close(sockfd);
  56.     return 0;
  57. }
client.c

点击(此处)折叠或打开

  1. /* Copyright(C) 2012 for free
  2. * All right reserved
  3. */
  4. /**
  5. * @file client.c
  6. * @synopsis tcp 测试例程
  7. * @version 1.0
  8. * @date 2012-07-06
  9. */
  10. #include
  11. #include
  12. #include
  13. #include
  14. #include
  15. #include
  16. #include

  17. #define MAXLINE 80
  18. #define SERV_PORT 8000

  19. int main(int argc, char *argv[])
  20. {
  21. struct sockaddr_in servaddr;
  22. char buf[MAXLINE];
  23. int sockfd, n;
  24. char *str;

  25. if (argc != 2) {
  26. fputs("usage: ./client message\n",stderr);
  27. exit(1);
  28. }
  29. str = argv[1];

  30. sockfd = socket(AF_INET, SOCK_STREAM, 0);

  31. bzero(&servaddr, sizeof(servaddr));
  32. servaddr.sin_family = AF_INET;
  33. inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
  34. servaddr.sin_port = htons(SERV_PORT);

  35. connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

  36. write(sockfd, str, strlen(str));

  37. n = read(sockfd, buf, MAXLINE);
  38. printf("Response from server:\n");
  39. write(STDOUT_FILENO, buf, n);
  40. printf("\n");

  41. close(sockfd);
  42. return 0;
  43. }

UDP
server.c

点击(此处)折叠或打开

  1. /* Copyright(C) 2012 for free
  2.  * All right reserved
  3.  */
  4. /**
  5.  * @file server.c
  6.  * @synopsis udp 测试例程
  7.  * @version 1.0
  8.  * @date 2012-07-06
  9.  */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <ctype.h>
  15. #include <netinet/in.h>
  16. #include <arpa/inet.h>

  17. #define MAXLINE 80
  18. #define SERV_PORT 8000

  19. int main(int argc,char *argv[])
  20. {
  21.     struct sockaddr_in servaddr, cliaddr;
  22.     socklen_t cliaddr_len;
  23.     int sockfd;
  24.     char buf[MAXLINE];
  25.     char str[INET_ADDRSTRLEN];
  26.     int i, n;

  27.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);

  28.     bzero(&servaddr, sizeof(servaddr));
  29.     servaddr.sin_family = AF_INET;
  30.     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  31.     servaddr.sin_port = htons(SERV_PORT);
  32.     
  33.     bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

  34.     printf("Accepting connections ...\n");
  35.     while (1) {
  36.         memset(buf,0,sizeof(buf));
  37.         cliaddr_len = sizeof(cliaddr);
  38.         n = recvfrom(sockfd, buf, MAXLINE, 0, (struct sockaddr *)&cliaddr, &cliaddr_len);
  39.         if (n == -1)
  40.             perror("recvfrom error");
  41.         printf("received from %s at PORT %d is:%s\n",
  42.          inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
  43.          ntohs(cliaddr.sin_port),buf);
  44.         for (i = 0; i < n; i++)
  45.             buf[i] = toupper(buf[i]);
  46.         n = sendto(sockfd, buf, n, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
  47.         if (n == -1)
  48.             perror("sendto error");
  49.     }

  50.     close(sockfd);
  51.     return 0;
  52. }
client.c

点击(此处)折叠或打开

  1. /* Copyright(C) 2012 for free
  2.  * All right reserved
  3.  */
  4. /**
  5.  * @file client.c
  6.  * @synopsis upd 测试例程
  7.  * @version 1.0
  8.  * @date 2012-07-06
  9.  */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>

  15. #define MAXLINE 80
  16. #define SERV_PORT 8000

  17. int main(int argc, char *argv[])
  18. {
  19.     struct sockaddr_in servaddr;
  20.     int sockfd, n;
  21.     char buf[MAXLINE];
  22.     
  23.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);

  24.     bzero(&servaddr, sizeof(servaddr));
  25.     servaddr.sin_family = AF_INET;
  26.     inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
  27.     servaddr.sin_port = htons(SERV_PORT);
  28.     
  29.     printf("please input character:\n");
  30.     while (fgets(buf, MAXLINE, stdin) != NULL) {
  31.         n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
  32.         if (n == -1)
  33.             perror("sendto error");

  34.         n = recvfrom(sockfd, buf, MAXLINE, 0, NULL, 0);
  35.         if (n == -1)
  36.             perror("recvfrom error");
  37.     
  38.         write(STDOUT_FILENO, buf, n);
  39.         printf("please input character:\n");
  40.     }

  41.     close(sockfd);
  42.     return 0;
  43. }



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