udp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/wait.h>
  8. #include <arpa/inet.h>
  9. #include <netinet/in.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. {
  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. write(pipefds[1], &byte, 1);
  39. }
  40. if (do_fork) {
  41. if (fork() > 0) {
  42. close(s);
  43. wait(NULL);
  44. return 0;
  45. }
  46. }
  47. for (i=0; i<NPACK; i++) {
  48. if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other,
  49. &slen)==-1) {
  50. fprintf(stderr, "recvfrom() failed\n");
  51. exit(1);
  52. }
  53. printf("Received packet from %s:%d\nData: %s\n",
  54. inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
  55. }
  56. close(s);
  57. if (do_fork)
  58. exit(0);
  59. return 0;
  60. }
  61. int client(void)
  62. {
  63. struct sockaddr_in si_other;
  64. int s, i;
  65. socklen_t 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. }