tcp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_IP "127.0.0.1"
  16. #define PORT 9930
  17. #define BUFLEN 512
  18. #define NPACK 10
  19. const char * fname;
  20. enum { SINGLE, PARALLEL } mode = PARALLEL;
  21. int do_fork = 0;
  22. int pipefds[2];
  23. int server(void)
  24. {
  25. int conn, create_socket, new_socket, addrlen, fd;
  26. int bufsize = 1024;
  27. char * buffer = malloc(bufsize);
  28. struct sockaddr_in address;
  29. if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) > 0)
  30. printf("The socket was created\n");
  31. address.sin_family = AF_INET;
  32. inet_pton(AF_INET, SRV_IP, &(address.sin_addr));
  33. address.sin_port = htons(PORT);
  34. if (bind(create_socket, (struct sockaddr *) &address,
  35. sizeof(address)) < 0) {
  36. perror("bind");
  37. close(create_socket);
  38. exit(-1);
  39. }
  40. if (listen(create_socket, 3) < 0) {
  41. perror("listen");
  42. close(create_socket);
  43. exit(-1);
  44. }
  45. if (mode == PARALLEL) {
  46. close(pipefds[0]);
  47. char byte = 0;
  48. write(pipefds[1], &byte, 1);
  49. }
  50. addrlen = sizeof(address);
  51. new_socket = accept(create_socket, (struct sockaddr *) &address,
  52. &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. printf("Request completed\n");
  79. close(new_socket);
  80. if (do_fork)
  81. exit(0);
  82. return 0;
  83. }
  84. int client(void)
  85. {
  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. read(pipefds[0], &byte, 1);
  94. }
  95. if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) >= 0)
  96. printf("The Socket was created\n");
  97. address.sin_family = AF_INET;
  98. inet_pton(AF_INET, SRV_IP, &address.sin_addr);
  99. address.sin_port = htons(PORT);
  100. if (connect(create_socket, (struct sockaddr *) &address,
  101. sizeof(address)) == 0) {
  102. printf("The connection was accepted with the server\n");
  103. } else {
  104. perror("accept");
  105. exit(0);
  106. }
  107. if (do_fork) {
  108. if (fork() > 0) {
  109. close(create_socket);
  110. wait(NULL);
  111. return 0;
  112. }
  113. }
  114. printf("Content:\n");
  115. while((count=recv(create_socket, buffer, bufsize, 0))>0)
  116. write(1, buffer, count);
  117. printf("EOF\n");
  118. buffer[0] = 0;
  119. close(create_socket);
  120. if (do_fork)
  121. exit(0);
  122. return 0;
  123. }
  124. int main(int argc, char ** argv)
  125. {
  126. char fnamebuf[40];
  127. strcpy(fnamebuf, argv[0]);
  128. strcat(fnamebuf, ".c");
  129. fname = fnamebuf;
  130. setvbuf(stdout, NULL, _IONBF, 0);
  131. if (argc > 1) {
  132. if (strcmp(argv[1], "client") == 0) {
  133. mode = SINGLE;
  134. client();
  135. return 0;
  136. }
  137. if (strcmp(argv[1], "server") == 0) {
  138. mode = SINGLE;
  139. server();
  140. return 0;
  141. }
  142. if (strcmp(argv[1], "fork") == 0) {
  143. do_fork = 1;
  144. goto old;
  145. }
  146. }
  147. else {
  148. old:
  149. pipe(pipefds);
  150. int pid = fork();
  151. if (pid == 0)
  152. client();
  153. else {
  154. server();
  155. waitpid(pid, NULL, -1);
  156. }
  157. }
  158. return 0;
  159. }