tcp_msg_peek.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #define _GNU_SOURCE
  2. #include <arpa/inet.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <netinet/in.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/socket.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <unistd.h>
  14. #define SRV_IP "127.0.0.1"
  15. #define PORT 11111
  16. #define BUFLEN 512
  17. enum { SINGLE, PARALLEL } mode = PARALLEL;
  18. int pipefds[2];
  19. void server(void) {
  20. int listening_socket, client_socket;
  21. struct sockaddr_in address;
  22. socklen_t addrlen;
  23. if ((listening_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  24. perror("socket");
  25. exit(1);
  26. }
  27. int enable = 1;
  28. if (setsockopt(listening_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0) {
  29. perror("setsockopt");
  30. exit(1);
  31. }
  32. memset(&address, 0, sizeof(address));
  33. address.sin_family = AF_INET;
  34. address.sin_port = htons(PORT);
  35. address.sin_addr.s_addr = htonl(INADDR_ANY);
  36. if (bind(listening_socket, (struct sockaddr*)&address, sizeof(address)) < 0) {
  37. perror("bind");
  38. exit(1);
  39. }
  40. if (listen(listening_socket, 3) < 0) {
  41. perror("listen");
  42. exit(1);
  43. }
  44. if (mode == PARALLEL) {
  45. if (close(pipefds[0]) < 0) {
  46. perror("close of pipe");
  47. exit(1);
  48. }
  49. char byte = 0;
  50. ssize_t written = 0;
  51. while (written == 0) {
  52. if ((written = write(pipefds[1], &byte, sizeof(byte))) < 0) {
  53. if (errno == EINTR || errno == EAGAIN)
  54. continue;
  55. perror("write on pipe");
  56. exit(1);
  57. }
  58. }
  59. }
  60. addrlen = sizeof(address);
  61. client_socket = accept(listening_socket, (struct sockaddr*)&address, &addrlen);
  62. if (client_socket < 0) {
  63. perror("accept");
  64. exit(1);
  65. }
  66. if (close(listening_socket) < 0) {
  67. perror("close of listening socket");
  68. exit(1);
  69. }
  70. puts("[server] client is connected...");
  71. char buffer[] = "Hello from server!\n";
  72. ssize_t written = 0;
  73. while (written < sizeof(buffer)) {
  74. ssize_t n;
  75. if ((n = sendto(client_socket, buffer + written, sizeof(buffer) - written, 0, 0, 0)) < 0) {
  76. if (errno == EINTR || errno == EAGAIN)
  77. continue;
  78. perror("sendto to client");
  79. exit(1);
  80. }
  81. written += n;
  82. }
  83. if (close(client_socket) < 0) {
  84. perror("close of client socket");
  85. exit(1);
  86. }
  87. puts("[server] done");
  88. }
  89. static ssize_t client_recv(int server_socket, char* buf, size_t len, int flags) {
  90. ssize_t read = 0;
  91. while (1) {
  92. ssize_t n;
  93. if ((n = recv(server_socket, buf + read, len - read, flags)) < 0) {
  94. if (errno == EINTR || errno == EAGAIN)
  95. continue;
  96. perror("client recv");
  97. exit(1);
  98. }
  99. read += n;
  100. if (!n || flags & MSG_PEEK) {
  101. /* recv with MSG_PEEK flag should be done only once */
  102. break;
  103. }
  104. }
  105. return read;
  106. }
  107. void client(void) {
  108. int server_socket;
  109. struct sockaddr_in address;
  110. char buffer[BUFLEN];
  111. ssize_t count;
  112. if (mode == PARALLEL) {
  113. if (close(pipefds[1]) < 0) {
  114. perror("close of pipe");
  115. exit(1);
  116. }
  117. char byte = 0;
  118. ssize_t received = 0;
  119. while (received == 0) {
  120. if ((received = read(pipefds[0], &byte, sizeof(byte))) < 0) {
  121. if (errno == EINTR || errno == EAGAIN)
  122. continue;
  123. perror("read on pipe");
  124. exit(1);
  125. }
  126. }
  127. }
  128. if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  129. perror("socket");
  130. exit(1);
  131. }
  132. memset(&address, 0, sizeof(address));
  133. address.sin_family = AF_INET;
  134. address.sin_port = htons((PORT));
  135. if (inet_aton(SRV_IP, &address.sin_addr) == 0) {
  136. perror("inet_aton");
  137. exit(1);
  138. }
  139. if (connect(server_socket, (struct sockaddr*)&address, sizeof(address)) < 0) {
  140. perror("connect");
  141. exit(1);
  142. }
  143. printf("[client] receiving with MSG_PEEK: ");
  144. count = client_recv(server_socket, buffer, sizeof(buffer), MSG_PEEK);
  145. fwrite(buffer, count, 1, stdout);
  146. printf("[client] receiving without MSG_PEEK: ");
  147. count = client_recv(server_socket, buffer, sizeof(buffer), 0);
  148. fwrite(buffer, count, 1, stdout);
  149. printf("[client] checking how many bytes are left unread: ");
  150. count = client_recv(server_socket, buffer, sizeof(buffer), 0);
  151. printf("%zu\n", count);
  152. if (close(server_socket) < 0) {
  153. perror("close of server socket");
  154. exit(1);
  155. }
  156. puts("[client] done");
  157. }
  158. int main(int argc, char** argv) {
  159. if (argc > 1) {
  160. if (strcmp(argv[1], "client") == 0) {
  161. mode = SINGLE;
  162. client();
  163. return 0;
  164. }
  165. if (strcmp(argv[1], "server") == 0) {
  166. mode = SINGLE;
  167. server();
  168. return 0;
  169. }
  170. } else {
  171. if (pipe(pipefds) < 0) {
  172. perror("pipe error");
  173. return 1;
  174. }
  175. int pid = fork();
  176. if (pid == 0) {
  177. client();
  178. } else {
  179. server();
  180. }
  181. }
  182. return 0;
  183. }