tcp.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* copied from http://www.daniweb.com/software-development/c/threads/179814 */
  2. #include <arpa/inet.h>
  3. #include <fcntl.h>
  4. #include <netinet/in.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/socket.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12. #include <unistd.h>
  13. #define SRV_BIND_IP "0.0.0.0"
  14. #define SRV_IP "127.0.0.1"
  15. #define PORT 9930
  16. #define BUFLEN 512
  17. #define NPACK 10
  18. const char* fname;
  19. enum { SINGLE, PARALLEL } mode = PARALLEL;
  20. int do_fork = 0;
  21. int pipefds[2];
  22. int server(void) {
  23. int conn, create_socket, new_socket, fd;
  24. socklen_t addrlen;
  25. int bufsize = 1024;
  26. char* buffer = malloc(bufsize);
  27. struct sockaddr_in address;
  28. if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) > 0)
  29. printf("The socket was created\n");
  30. address.sin_family = AF_INET;
  31. inet_pton(AF_INET, SRV_BIND_IP, &(address.sin_addr));
  32. address.sin_port = htons(PORT);
  33. if (bind(create_socket, (struct sockaddr*)&address, sizeof(address)) < 0) {
  34. perror("bind");
  35. close(create_socket);
  36. exit(-1);
  37. }
  38. if (listen(create_socket, 3) < 0) {
  39. perror("listen");
  40. close(create_socket);
  41. exit(-1);
  42. }
  43. if (mode == PARALLEL) {
  44. close(pipefds[0]);
  45. char byte = 0;
  46. if (write(pipefds[1], &byte, 1) != 1) {
  47. perror("write error");
  48. exit(1);
  49. }
  50. }
  51. addrlen = sizeof(address);
  52. new_socket = accept(create_socket, (struct sockaddr*)&address, &addrlen);
  53. if (new_socket < 0) {
  54. perror("accept");
  55. close(create_socket);
  56. exit(-1);
  57. }
  58. close(create_socket);
  59. if (do_fork) {
  60. if (fork() > 0) {
  61. close(new_socket);
  62. wait(NULL);
  63. return 0;
  64. }
  65. }
  66. char buff[18] = {'\0'};
  67. if (inet_ntop(AF_INET, &address.sin_addr, buff, 18) <= 0)
  68. perror("address conversion");
  69. printf("The client %s is connected...\n", buff);
  70. if ((fd = open(fname, O_RDONLY, 0)) < 0) {
  71. perror("file open failed");
  72. close(new_socket);
  73. close(create_socket);
  74. exit(-1);
  75. }
  76. while ((conn = read(fd, buffer, bufsize)) > 0) {
  77. sendto(new_socket, buffer, conn, 0, 0, 0);
  78. }
  79. printf("Request completed\n");
  80. close(new_socket);
  81. if (do_fork)
  82. exit(0);
  83. return 0;
  84. }
  85. int client(void) {
  86. int count, create_socket;
  87. int bufsize = 1024;
  88. char* buffer = malloc(bufsize);
  89. struct sockaddr_in address;
  90. if (mode == PARALLEL) {
  91. close(pipefds[1]);
  92. char byte = 0;
  93. if (read(pipefds[0], &byte, 1) != 1) {
  94. perror("read error");
  95. exit(1);
  96. }
  97. }
  98. if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) >= 0)
  99. printf("The Socket was created\n");
  100. address.sin_family = AF_INET;
  101. inet_pton(AF_INET, SRV_IP, &address.sin_addr);
  102. address.sin_port = htons(PORT);
  103. if (connect(create_socket, (struct sockaddr*)&address, sizeof(address)) == 0) {
  104. printf("The connection was accepted with the server\n");
  105. } else {
  106. perror("accept");
  107. exit(0);
  108. }
  109. if (do_fork) {
  110. if (fork() > 0) {
  111. close(create_socket);
  112. wait(NULL);
  113. return 0;
  114. }
  115. }
  116. printf("Content:\n");
  117. while ((count = recv(create_socket, buffer, bufsize, 0)) > 0) {
  118. if (write(1, buffer, count) != count) {
  119. perror("write error");
  120. exit(1);
  121. }
  122. }
  123. printf("EOF\n");
  124. buffer[0] = 0;
  125. close(create_socket);
  126. if (do_fork)
  127. exit(0);
  128. return 0;
  129. }
  130. int main(int argc, char** argv) {
  131. char fnamebuf[40];
  132. strcpy(fnamebuf, argv[0]);
  133. strcat(fnamebuf, ".c");
  134. fname = fnamebuf;
  135. setvbuf(stdout, NULL, _IONBF, 0);
  136. if (argc > 1) {
  137. if (strcmp(argv[1], "client") == 0) {
  138. mode = SINGLE;
  139. client();
  140. return 0;
  141. }
  142. if (strcmp(argv[1], "server") == 0) {
  143. mode = SINGLE;
  144. server();
  145. return 0;
  146. }
  147. if (strcmp(argv[1], "fork") == 0) {
  148. do_fork = 1;
  149. goto old;
  150. }
  151. } else {
  152. old:
  153. if (pipe(pipefds) < 0) {
  154. perror("pipe error");
  155. return 1;
  156. }
  157. int pid = fork();
  158. if (pid == 0)
  159. client();
  160. else {
  161. server();
  162. waitpid(pid, NULL, -1);
  163. }
  164. }
  165. return 0;
  166. }