udp.c 3.3 KB

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