send_recv.c 5.1 KB

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