send_recv.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <netinet/in.h>
  7. #include <pthread.h>
  8. #include <stdbool.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <sys/socket.h>
  14. #include <unistd.h>
  15. #ifdef __wasi__
  16. #include <wasi_socket_ext.h>
  17. #endif
  18. static pthread_mutex_t lock = { 0 };
  19. static pthread_cond_t cond = { 0 };
  20. static bool server_is_ready = false;
  21. void *
  22. run_as_server(void *arg)
  23. {
  24. int sock = -1, on = 1;
  25. struct sockaddr_in addr = { 0 };
  26. int addrlen = 0;
  27. int new_sock = -1;
  28. char *buf[] = {
  29. "The stars shine down", "It brings us light", "Light comes down",
  30. "To make us paths", "It watches us", "And mourns for us",
  31. };
  32. struct iovec iov[] = {
  33. { .iov_base = buf[0], .iov_len = strlen(buf[0]) + 1 },
  34. { .iov_base = buf[1], .iov_len = strlen(buf[1]) + 1 },
  35. { .iov_base = buf[2], .iov_len = strlen(buf[2]) + 1 },
  36. { .iov_base = buf[3], .iov_len = strlen(buf[3]) + 1 },
  37. { .iov_base = buf[4], .iov_len = strlen(buf[4]) + 1 },
  38. { .iov_base = buf[5], .iov_len = strlen(buf[5]) + 1 },
  39. };
  40. struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 6 };
  41. ssize_t send_len = 0;
  42. pthread_mutex_lock(&lock);
  43. sock = socket(AF_INET, SOCK_STREAM, 0);
  44. if (sock < 0) {
  45. perror("Create a socket failed");
  46. goto RETURN;
  47. }
  48. #ifndef __wasi__
  49. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) {
  50. perror("Setsockopt failed");
  51. goto RETURN;
  52. }
  53. #endif
  54. /* 0.0.0.0:1234 */
  55. addr.sin_family = AF_INET;
  56. addr.sin_port = htons(1234);
  57. addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  58. addrlen = sizeof(addr);
  59. if (bind(sock, (struct sockaddr *)&addr, addrlen) < 0) {
  60. perror("Bind failed");
  61. goto UNLOCK_SHUTDOWN;
  62. }
  63. if (listen(sock, 0) < 0) {
  64. perror("Listen failed");
  65. goto UNLOCK_SHUTDOWN;
  66. }
  67. server_is_ready = true;
  68. pthread_cond_signal(&cond);
  69. pthread_mutex_unlock(&lock);
  70. printf("Server is online ... \n");
  71. new_sock = accept(sock, (struct sockaddr *)&addr, (socklen_t *)&addrlen);
  72. if (new_sock < 0) {
  73. perror("Accept failed");
  74. goto SHUTDOWN;
  75. }
  76. printf("Start sending. \n");
  77. send_len = sendmsg(new_sock, &msg, 0);
  78. if (send_len < 0) {
  79. perror("Sendmsg failed");
  80. goto SHUTDOWN;
  81. }
  82. printf("Send %ld bytes successfully!\n", send_len);
  83. SHUTDOWN:
  84. shutdown(sock, SHUT_RD);
  85. return NULL;
  86. UNLOCK_SHUTDOWN:
  87. shutdown(sock, SHUT_RD);
  88. RETURN:
  89. pthread_mutex_unlock(&lock);
  90. return NULL;
  91. }
  92. void *
  93. run_as_client(void *arg)
  94. {
  95. int sock = -1;
  96. struct sockaddr_in addr = { 0 };
  97. char buf[256] = { 0 };
  98. struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
  99. struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1 };
  100. ssize_t recv_len = 0;
  101. pthread_mutex_lock(&lock);
  102. while (false == server_is_ready) {
  103. pthread_cond_wait(&cond, &lock);
  104. }
  105. pthread_mutex_unlock(&lock);
  106. printf("Client is running...\n");
  107. sock = socket(AF_INET, SOCK_STREAM, 0);
  108. if (sock < 0) {
  109. perror("Create a socket failed");
  110. goto RETURN;
  111. }
  112. /* 127.0.0.1:1234 */
  113. addr.sin_family = AF_INET;
  114. addr.sin_port = htons(1234);
  115. addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  116. if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  117. perror("Connect failed");
  118. goto UNLOCK_SHUTDOWN;
  119. }
  120. printf("Start receiving. \n");
  121. recv_len = recvmsg(sock, &msg, 0);
  122. if (recv_len < 0) {
  123. perror("Recvmsg failed");
  124. goto SHUTDOWN;
  125. }
  126. printf("Receive %ld bytes successlly!\n", recv_len);
  127. printf("Data:\n");
  128. uint8_t i = 0;
  129. char *s = msg.msg_iov->iov_base;
  130. for (i = 0; i < 6; i++) {
  131. printf(" %s\n", s);
  132. s += strlen(s) + 1;
  133. }
  134. SHUTDOWN:
  135. shutdown(sock, SHUT_RD);
  136. return NULL;
  137. UNLOCK_SHUTDOWN:
  138. shutdown(sock, SHUT_RD);
  139. RETURN:
  140. pthread_mutex_unlock(&lock);
  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. }