send_recv.c 5.9 KB

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