udp.c 3.5 KB

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