send_recv.c 5.5 KB

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