udp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #define _GNU_SOURCE
  2. #include <arpa/inet.h>
  3. #include <netinet/in.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <unistd.h>
  11. #define SRV_IP "127.0.0.1"
  12. #define PORT 9930
  13. #define BUFLEN 512
  14. #define NPACK 10
  15. enum { SINGLE, PARALLEL } mode = PARALLEL;
  16. int do_fork = 0;
  17. int pipefds[2];
  18. int server(void) {
  19. struct sockaddr_in si_me, si_other;
  20. int s, i;
  21. socklen_t slen = sizeof(si_other);
  22. char buf[BUFLEN];
  23. if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
  24. fprintf(stderr, "socket() failed\n");
  25. exit(1);
  26. }
  27. memset((char*)&si_me, 0, sizeof(si_me));
  28. si_me.sin_family = AF_INET;
  29. si_me.sin_port = htons(PORT);
  30. si_me.sin_addr.s_addr = htonl(INADDR_ANY);
  31. if (bind(s, (struct sockaddr*)&si_me, sizeof(si_me)) == -1) {
  32. fprintf(stderr, "bind() failed\n");
  33. exit(1);
  34. }
  35. if (mode == PARALLEL) {
  36. close(pipefds[0]);
  37. char byte = 0;
  38. if (write(pipefds[1], &byte, 1) != 1) {
  39. perror("write error");
  40. return 1;
  41. }
  42. }
  43. if (do_fork) {
  44. if (fork() > 0) {
  45. close(s);
  46. wait(NULL);
  47. return 0;
  48. }
  49. }
  50. for (i = 0; i < NPACK; i++) {
  51. if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*)&si_other, &slen) == -1) {
  52. fprintf(stderr, "recvfrom() failed\n");
  53. exit(1);
  54. }
  55. printf("Received packet from %s:%d\nData: %s\n", inet_ntoa(si_other.sin_addr),
  56. ntohs(si_other.sin_port), buf);
  57. }
  58. close(s);
  59. if (do_fork)
  60. exit(0);
  61. return 0;
  62. }
  63. int client(void) {
  64. struct sockaddr_in si_other;
  65. int s, i;
  66. socklen_t slen = sizeof(si_other);
  67. char buf[BUFLEN] = "hi";
  68. int res;
  69. if (mode == PARALLEL) {
  70. close(pipefds[1]);
  71. char byte = 0;
  72. if (read(pipefds[0], &byte, 1) != 1) {
  73. perror("read error");
  74. return 1;
  75. }
  76. }
  77. if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
  78. fprintf(stderr, "socket() failed\n");
  79. exit(1);
  80. }
  81. if (do_fork) {
  82. if (fork() > 0) {
  83. close(s);
  84. wait(NULL);
  85. return 0;
  86. }
  87. }
  88. memset((char*)&si_other, 0, sizeof(si_other));
  89. si_other.sin_family = AF_INET;
  90. si_other.sin_port = htons((PORT));
  91. if (inet_aton(SRV_IP, &si_other.sin_addr) == 0) {
  92. fprintf(stderr, "inet_aton() failed\n");
  93. exit(1);
  94. }
  95. for (i = 0; i < 10; i++) {
  96. printf("Sending packet %d\n", i);
  97. sprintf(buf, "This is packet %d", i);
  98. if ((res = sendto(s, buf, BUFLEN, 0, (struct sockaddr*)&si_other, slen)) == -1) {
  99. fprintf(stderr, "sendto() failed\n");
  100. exit(1);
  101. }
  102. }
  103. close(s);
  104. if (do_fork)
  105. exit(0);
  106. return 0;
  107. }
  108. int main(int argc, char** argv) {
  109. if (argc > 1) {
  110. if (strcmp(argv[1], "client") == 0) {
  111. mode = SINGLE;
  112. return client();
  113. } else if (strcmp(argv[1], "server") == 0) {
  114. mode = SINGLE;
  115. return server();
  116. } else if (strcmp(argv[1], "fork") == 0) {
  117. do_fork = 1;
  118. } else {
  119. puts("Invalid option!");
  120. return 1;
  121. }
  122. }
  123. if (pipe(pipefds) < 0) {
  124. perror("pipe error");
  125. return 1;
  126. }
  127. int pid = fork();
  128. if (pid < 0) {
  129. perror("fork error");
  130. return 1;
  131. } else if (pid == 0) {
  132. return client();
  133. } else {
  134. int ret = server();
  135. if (waitpid(pid, NULL, -1) == -1) {
  136. perror("waitpid error");
  137. ret = 1;
  138. }
  139. return ret;
  140. }
  141. }