send_recv.c 5.0 KB

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