tcp.c 4.3 KB

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