tcp.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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, fd;
  27. socklen_t addrlen;
  28. int bufsize = 1024;
  29. char * buffer = malloc(bufsize);
  30. struct sockaddr_in address;
  31. if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) > 0)
  32. printf("The socket was created\n");
  33. address.sin_family = AF_INET;
  34. inet_pton(AF_INET, SRV_BIND_IP, &(address.sin_addr));
  35. address.sin_port = htons(PORT);
  36. if (bind(create_socket, (struct sockaddr *) &address,
  37. sizeof(address)) < 0) {
  38. perror("bind");
  39. close(create_socket);
  40. exit(-1);
  41. }
  42. if (listen(create_socket, 3) < 0) {
  43. perror("listen");
  44. close(create_socket);
  45. exit(-1);
  46. }
  47. if (mode == PARALLEL) {
  48. close(pipefds[0]);
  49. char byte = 0;
  50. write(pipefds[1], &byte, 1);
  51. }
  52. addrlen = sizeof(address);
  53. new_socket = accept(create_socket, (struct sockaddr *) &address,
  54. &addrlen);
  55. if (new_socket < 0) {
  56. perror("accept");
  57. close(create_socket);
  58. exit(-1);
  59. }
  60. close(create_socket);
  61. if (do_fork) {
  62. if (fork() > 0) {
  63. close(new_socket);
  64. wait(NULL);
  65. return 0;
  66. }
  67. }
  68. char buff[18] = {'\0'};
  69. if (inet_ntop(AF_INET, &address.sin_addr, buff, 18) <= 0)
  70. perror("address conversion");
  71. printf("The client %s is connected...\n", buff);
  72. if ((fd = open(fname, O_RDONLY, 0)) < 0) {
  73. perror("file open failed");
  74. close(new_socket);
  75. close(create_socket);
  76. exit(-1);
  77. }
  78. while ((conn = read(fd, buffer, bufsize)) > 0)
  79. sendto(new_socket, buffer, conn, 0, 0, 0);
  80. printf("Request completed\n");
  81. close(new_socket);
  82. if (do_fork)
  83. exit(0);
  84. return 0;
  85. }
  86. int client(void)
  87. {
  88. int count, create_socket;
  89. int bufsize = 1024;
  90. char * buffer = malloc(bufsize);
  91. struct sockaddr_in address;
  92. if (mode == PARALLEL) {
  93. close(pipefds[1]);
  94. char byte = 0;
  95. read(pipefds[0], &byte, 1);
  96. }
  97. if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) >= 0)
  98. printf("The Socket was created\n");
  99. address.sin_family = AF_INET;
  100. inet_pton(AF_INET, SRV_IP, &address.sin_addr);
  101. address.sin_port = htons(PORT);
  102. if (connect(create_socket, (struct sockaddr *) &address,
  103. 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. write(1, buffer, count);
  119. printf("EOF\n");
  120. buffer[0] = 0;
  121. close(create_socket);
  122. if (do_fork)
  123. exit(0);
  124. return 0;
  125. }
  126. int main(int argc, char ** argv)
  127. {
  128. char fnamebuf[40];
  129. strcpy(fnamebuf, argv[0]);
  130. strcat(fnamebuf, ".c");
  131. fname = fnamebuf;
  132. setvbuf(stdout, NULL, _IONBF, 0);
  133. if (argc > 1) {
  134. if (strcmp(argv[1], "client") == 0) {
  135. mode = SINGLE;
  136. client();
  137. return 0;
  138. }
  139. if (strcmp(argv[1], "server") == 0) {
  140. mode = SINGLE;
  141. server();
  142. return 0;
  143. }
  144. if (strcmp(argv[1], "fork") == 0) {
  145. do_fork = 1;
  146. goto old;
  147. }
  148. }
  149. else {
  150. old:
  151. pipe(pipefds);
  152. int pid = fork();
  153. if (pid == 0)
  154. client();
  155. else {
  156. server();
  157. waitpid(pid, NULL, -1);
  158. }
  159. }
  160. return 0;
  161. }