send_recv.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <arpa/inet.h>
  6. #include <assert.h>
  7. #include <netinet/in.h>
  8. #include <pthread.h>
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <sys/socket.h>
  15. #include <unistd.h>
  16. #ifdef __wasi__
  17. #include <wasi_socket_ext.h>
  18. #endif
  19. static pthread_mutex_t lock = { 0 };
  20. static pthread_cond_t cond = { 0 };
  21. static bool server_create_failed = false;
  22. static bool server_is_ready = false;
  23. void *
  24. run_as_server(void *arg)
  25. {
  26. int sock = -1, on = 1;
  27. struct sockaddr_in addr = { 0 };
  28. int addrlen = 0;
  29. int new_sock = -1;
  30. char *buf[] = {
  31. "The stars shine down", "It brings us light", "Light comes down",
  32. "To make us paths", "It watches us", "And mourns for us",
  33. };
  34. struct iovec iov[] = {
  35. { .iov_base = buf[0], .iov_len = strlen(buf[0]) + 1 },
  36. { .iov_base = buf[1], .iov_len = strlen(buf[1]) + 1 },
  37. { .iov_base = buf[2], .iov_len = strlen(buf[2]) + 1 },
  38. { .iov_base = buf[3], .iov_len = strlen(buf[3]) + 1 },
  39. { .iov_base = buf[4], .iov_len = strlen(buf[4]) + 1 },
  40. { .iov_base = buf[5], .iov_len = strlen(buf[5]) + 1 },
  41. };
  42. struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 6 };
  43. ssize_t send_len = 0;
  44. pthread_mutex_lock(&lock);
  45. sock = socket(AF_INET, SOCK_STREAM, 0);
  46. if (sock < 0) {
  47. server_create_failed = true;
  48. pthread_cond_signal(&cond);
  49. pthread_mutex_unlock(&lock);
  50. perror("Create a socket failed");
  51. return NULL;
  52. }
  53. #ifndef __wasi__
  54. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) {
  55. server_create_failed = true;
  56. pthread_cond_signal(&cond);
  57. pthread_mutex_unlock(&lock);
  58. perror("Setsockopt failed");
  59. goto fail1;
  60. }
  61. #endif
  62. /* 0.0.0.0:1234 */
  63. addr.sin_family = AF_INET;
  64. addr.sin_port = htons(1234);
  65. addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  66. addrlen = sizeof(addr);
  67. if (bind(sock, (struct sockaddr *)&addr, addrlen) < 0) {
  68. server_create_failed = true;
  69. pthread_cond_signal(&cond);
  70. pthread_mutex_unlock(&lock);
  71. perror("Bind failed");
  72. goto fail1;
  73. }
  74. if (listen(sock, 0) < 0) {
  75. server_create_failed = true;
  76. pthread_cond_signal(&cond);
  77. pthread_mutex_unlock(&lock);
  78. perror("Listen failed");
  79. goto fail1;
  80. }
  81. server_is_ready = true;
  82. pthread_cond_signal(&cond);
  83. pthread_mutex_unlock(&lock);
  84. printf("Server is online ... \n");
  85. new_sock = accept(sock, (struct sockaddr *)&addr, (socklen_t *)&addrlen);
  86. if (new_sock < 0) {
  87. perror("Accept failed");
  88. goto fail1;
  89. }
  90. printf("Start sending. \n");
  91. send_len = sendmsg(new_sock, &msg, 0);
  92. if (send_len < 0) {
  93. perror("Sendmsg failed");
  94. goto fail2;
  95. }
  96. printf("Send %ld bytes successfully!\n", send_len);
  97. fail2:
  98. close(new_sock);
  99. fail1:
  100. shutdown(sock, SHUT_RD);
  101. close(sock);
  102. return NULL;
  103. }
  104. void *
  105. run_as_client(void *arg)
  106. {
  107. int sock = -1;
  108. struct sockaddr_in addr = { 0 };
  109. /* buf of server is 106 bytes */
  110. char buf[110] = { 0 };
  111. struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
  112. struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1 };
  113. ssize_t recv_len = 0;
  114. pthread_mutex_lock(&lock);
  115. while (!server_create_failed && !server_is_ready) {
  116. pthread_cond_wait(&cond, &lock);
  117. }
  118. pthread_mutex_unlock(&lock);
  119. if (server_create_failed) {
  120. return NULL;
  121. }
  122. printf("Client is running...\n");
  123. sock = socket(AF_INET, SOCK_STREAM, 0);
  124. if (sock < 0) {
  125. perror("Create a socket failed");
  126. return NULL;
  127. }
  128. /* 127.0.0.1:1234 */
  129. addr.sin_family = AF_INET;
  130. addr.sin_port = htons(1234);
  131. addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  132. if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  133. perror("Connect failed");
  134. goto fail;
  135. }
  136. printf("Start receiving. \n");
  137. recv_len = recvmsg(sock, &msg, 0);
  138. if (recv_len < 0) {
  139. perror("Recvmsg failed");
  140. goto fail;
  141. }
  142. printf("Receive %ld bytes successlly!\n", recv_len);
  143. assert(recv_len == 106);
  144. printf("Data:\n");
  145. char *s = msg.msg_iov->iov_base;
  146. while (strlen(s) > 0) {
  147. printf(" %s\n", s);
  148. s += strlen(s) + 1;
  149. }
  150. fail:
  151. shutdown(sock, SHUT_RD);
  152. close(sock);
  153. return NULL;
  154. }
  155. int
  156. main(int argc, char *argv[])
  157. {
  158. pthread_t cs[2] = { 0 };
  159. uint8_t i = 0;
  160. int ret = EXIT_SUCCESS;
  161. if (pthread_mutex_init(&lock, NULL)) {
  162. perror("Initialize mutex failed");
  163. ret = EXIT_FAILURE;
  164. goto RETURN;
  165. }
  166. if (pthread_cond_init(&cond, NULL)) {
  167. perror("Initialize condition failed");
  168. ret = EXIT_FAILURE;
  169. goto DESTROY_MUTEX;
  170. }
  171. if (pthread_create(&cs[0], NULL, run_as_server, NULL)) {
  172. perror("Create a server thread failed");
  173. ret = EXIT_FAILURE;
  174. goto DESTROY_COND;
  175. }
  176. if (pthread_create(&cs[1], NULL, run_as_client, NULL)) {
  177. perror("Create a client thread failed");
  178. ret = EXIT_FAILURE;
  179. goto DESTROY_COND;
  180. }
  181. for (i = 0; i < 2; i++) {
  182. pthread_join(cs[i], NULL);
  183. }
  184. DESTROY_COND:
  185. pthread_cond_destroy(&cond);
  186. DESTROY_MUTEX:
  187. pthread_mutex_destroy(&lock);
  188. RETURN:
  189. return ret;
  190. }