经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
UDP server Code
来源:cnblogs  作者:CristL  时间:2018/9/25 20:33:07  对本文有异议

Code Example:

  1. The following programs demonstrate the use of getaddrinfo(), gai_strerror(), freeaddrinfo(), and getnameinfo(). The programs are an echo server and client for UDP datagrams.

    服务器端程序:
  1. 1 ###Server program
  2. 2
  3. 3 #include <sys/types.h>
  4. 4 #include <stdio.h>
  5. 5 #include <stdlib.h>
  6. 6 #include <unistd.h>
  7. 7 #include <string.h>
  8. 8 #include <sys/socket.h>
  9. 9 #include <netdb.h>
  10. 10
  11. 11 #define BUF_SIZE 500
  12. 12
  13. 13 int
  14. 14 main(int argc, char *argv[])
  15. 15 {
  16. 16 struct addrinfo hints;
  17. 17 struct addrinfo *result, *rp;
  18. 18 int sfd, s;
  19. 19 struct sockaddr_storage peer_addr;
  20. 20 socklen_t peer_addr_len;
  21. 21 ssize_t nread;
  22. 22 char buf[BUF_SIZE];
  23. 23
  24. 24 if (argc != 2) {
  25. 25 fprintf(stderr, "Usage: %s port\n", argv[0]);
  26. 26 exit(EXIT_FAILURE);
  27. 27 }
  28. 28
  29. 29 memset(&hints, 0, sizeof(struct addrinfo));
  30. 30 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
  31. 31 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
  32. 32 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
  33. 33 hints.ai_protocol = 0; /* Any protocol */
  34. 34 hints.ai_canonname = NULL;
  35. 35 hints.ai_addr = NULL;
  36. 36 hints.ai_next = NULL;
  37. 37
  38. 38 s = getaddrinfo(NULL, argv[1], &hints, &result);
  39. 39 if (s != 0) {
  40. 40 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
  41. 41 exit(EXIT_FAILURE);
  42. 42 }
  43. 43
  44. 44 /* getaddrinfo() returns a list of address structures.
  45. 45 Try each address until we successfully bind(2).
  46. 46 If socket(2) (or bind(2)) fails, we (close the socket
  47. 47 and) try the next address. */
  48. 48
  49. 49 for (rp = result; rp != NULL; rp = rp->ai_next) {
  50. 50 sfd = socket(rp->ai_family, rp->ai_socktype,
  51. 51 rp->ai_protocol);
  52. 52 if (sfd == -1)
  53. 53 continue;
  54. 54
  55. 55 if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
  56. 56 break; /* Success */
  57. 57
  58. 58 close(sfd);
  59. 59 }
  60. 60
  61. 61 if (rp == NULL) { /* No address succeeded */
  62. 62 fprintf(stderr, "Could not bind\n");
  63. 63 exit(EXIT_FAILURE);
  64. 64 }
  65. 65
  66. 66 freeaddrinfo(result); /* No longer needed */
  67. 67
  68. 68 /* Read datagrams and echo them back to sender */
  69. 69
  70. 70 for (;;) {
  71. 71 peer_addr_len = sizeof(struct sockaddr_storage);
  72. 72 nread = recvfrom(sfd, buf, BUF_SIZE, 0,
  73. 73 (struct sockaddr *) &peer_addr, &peer_addr_len);
  74. 74 if (nread == -1)
  75. 75 continue; /* Ignore failed request */
  76. 76
  77. 77 char host[NI_MAXHOST], service[NI_MAXSERV];
  78. 78
  79. 79 s = getnameinfo((struct sockaddr *) &peer_addr,
  80. 80 peer_addr_len, host, NI_MAXHOST,
  81. 81 service, NI_MAXSERV, NI_NUMERICSERV);
  82. 82 if (s == 0)
  83. 83 printf("Received %zd bytes from %s:%s\n",
  84. 84 nread, host, service);
  85. 85 else
  86. 86 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
  87. 87
  88. 88 if (sendto(sfd, buf, nread, 0,
  89. 89 (struct sockaddr *) &peer_addr,
  90. 90 peer_addr_len) != nread)
  91. 91 fprintf(stderr, "Error sending response\n");
  92. 92 }
  93. 93 }

客户端程序:

  1. 1 ###Client program
  2. 2
  3. 3 #include <sys/types.h>
  4. 4 #include <sys/socket.h>
  5. 5 #include <netdb.h>
  6. 6 #include <stdio.h>
  7. 7 #include <stdlib.h>
  8. 8 #include <unistd.h>
  9. 9 #include <string.h>
  10. 10
  11. 11 #define BUF_SIZE 500
  12. 12
  13. 13 int
  14. 14 main(int argc, char *argv[])
  15. 15 {
  16. 16 struct addrinfo hints;
  17. 17 struct addrinfo *result, *rp;
  18. 18 int sfd, s, j;
  19. 19 size_t len;
  20. 20 ssize_t nread;
  21. 21 char buf[BUF_SIZE];
  22. 22
  23. 23 if (argc < 3) {
  24. 24 fprintf(stderr, "Usage: %s host port msg...\n", argv[0]);
  25. 25 exit(EXIT_FAILURE);
  26. 26 }
  27. 27
  28. 28 /* Obtain address(es) matching host/port */
  29. 29
  30. 30 memset(&hints, 0, sizeof(struct addrinfo));
  31. 31 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
  32. 32 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
  33. 33 hints.ai_flags = 0;
  34. 34 hints.ai_protocol = 0; /* Any protocol */
  35. 35
  36. 36 s = getaddrinfo(argv[1], argv[2], &hints, &result);
  37. 37 if (s != 0) {
  38. 38 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
  39. 39 exit(EXIT_FAILURE);
  40. 40 }
  41. 41
  42. 42 /* getaddrinfo() returns a list of address structures.
  43. 43 Try each address until we successfully connect(2).
  44. 44 If socket(2) (or connect(2)) fails, we (close the socket
  45. 45 and) try the next address. */
  46. 46
  47. 47 for (rp = result; rp != NULL; rp = rp->ai_next) {
  48. 48 sfd = socket(rp->ai_family, rp->ai_socktype,
  49. 49 rp->ai_protocol);
  50. 50 if (sfd == -1)
  51. 51 continue;
  52. 52
  53. 53 if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
  54. 54 break; /* Success */
  55. 55
  56. 56 close(sfd);
  57. 57 }
  58. 58
  59. 59 if (rp == NULL) { /* No address succeeded */
  60. 60 fprintf(stderr, "Could not connect\n");
  61. 61 exit(EXIT_FAILURE);
  62. 62 }
  63. 63
  64. 64 freeaddrinfo(result); /* No longer needed */
  65. 65
  66. 66 /* Send remaining command-line arguments as separate
  67. 67 datagrams, and read responses from server */
  68. 68
  69. 69 for (j = 3; j < argc; j++) {
  70. 70 len = strlen(argv[j]) + 1;
  71. 71 /* +1 for terminating null byte */
  72. 72
  73. 73 if (len + 1 > BUF_SIZE) {
  74. 74 fprintf(stderr,
  75. 75 "Ignoring long message in argument %d\n", j);
  76. 76 continue;
  77. 77 }
  78. 78
  79. 79 if (write(sfd, argv[j], len) != len) {
  80. 80 fprintf(stderr, "partial/failed write\n");
  81. 81 exit(EXIT_FAILURE);
  82. 82 }
  83. 83
  84. 84 nread = read(sfd, buf, BUF_SIZE);
  85. 85 if (nread == -1) {
  86. 86 perror("read");
  87. 87 exit(EXIT_FAILURE);
  88. 88 }
  89. 89
  90. 90 printf("Received %zd bytes: %s\n", nread, buf);
  91. 91 }
  92. 92
  93. 93 exit(EXIT_SUCCESS);
  94. 94 }

 Come from url: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号