tcp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. write(pipefds[1], &byte, 1);
  47. }
  48. addrlen = sizeof(address);
  49. new_socket = accept(create_socket, (struct sockaddr*)&address, &addrlen);
  50. if (new_socket < 0) {
  51. perror("accept");
  52. close(create_socket);
  53. exit(-1);
  54. }
  55. close(create_socket);
  56. if (do_fork) {
  57. if (fork() > 0) {
  58. close(new_socket);
  59. wait(NULL);
  60. return 0;
  61. }
  62. }
  63. char buff[18] = {'\0'};
  64. if (inet_ntop(AF_INET, &address.sin_addr, buff, 18) <= 0)
  65. perror("address conversion");
  66. printf("The client %s is connected...\n", buff);
  67. if ((fd = open(fname, O_RDONLY, 0)) < 0) {
  68. perror("file open failed");
  69. close(new_socket);
  70. close(create_socket);
  71. exit(-1);
  72. }
  73. while ((conn = read(fd, buffer, bufsize)) > 0) {
  74. sendto(new_socket, buffer, conn, 0, 0, 0);
  75. }
  76. printf("Request completed\n");
  77. close(new_socket);
  78. if (do_fork)
  79. exit(0);
  80. return 0;
  81. }
  82. int client(void) {
  83. int count, create_socket;
  84. int bufsize = 1024;
  85. char* buffer = malloc(bufsize);
  86. struct sockaddr_in address;
  87. if (mode == PARALLEL) {
  88. close(pipefds[1]);
  89. char byte = 0;
  90. read(pipefds[0], &byte, 1);
  91. }
  92. if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) >= 0)
  93. printf("The Socket was created\n");
  94. address.sin_family = AF_INET;
  95. inet_pton(AF_INET, SRV_IP, &address.sin_addr);
  96. address.sin_port = htons(PORT);
  97. if (connect(create_socket, (struct sockaddr*)&address, sizeof(address)) == 0) {
  98. printf("The connection was accepted with the server\n");
  99. } else {
  100. perror("accept");
  101. exit(0);
  102. }
  103. if (do_fork) {
  104. if (fork() > 0) {
  105. close(create_socket);
  106. wait(NULL);
  107. return 0;
  108. }
  109. }
  110. printf("Content:\n");
  111. while ((count = recv(create_socket, buffer, bufsize, 0)) > 0) {
  112. write(1, buffer, count);
  113. }
  114. printf("EOF\n");
  115. buffer[0] = 0;
  116. close(create_socket);
  117. if (do_fork)
  118. exit(0);
  119. return 0;
  120. }
  121. int main(int argc, char** argv) {
  122. char fnamebuf[40];
  123. strcpy(fnamebuf, argv[0]);
  124. strcat(fnamebuf, ".c");
  125. fname = fnamebuf;
  126. setvbuf(stdout, NULL, _IONBF, 0);
  127. if (argc > 1) {
  128. if (strcmp(argv[1], "client") == 0) {
  129. mode = SINGLE;
  130. client();
  131. return 0;
  132. }
  133. if (strcmp(argv[1], "server") == 0) {
  134. mode = SINGLE;
  135. server();
  136. return 0;
  137. }
  138. if (strcmp(argv[1], "fork") == 0) {
  139. do_fork = 1;
  140. goto old;
  141. }
  142. } else {
  143. old:
  144. pipe(pipefds);
  145. int pid = fork();
  146. if (pid == 0)
  147. client();
  148. else {
  149. server();
  150. waitpid(pid, NULL, -1);
  151. }
  152. }
  153. return 0;
  154. }