udp.c 3.5 KB

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