tcp_server.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #define WORKER_NUM 5
  19. void *
  20. run(void *arg)
  21. {
  22. const char *message = "Say Hi from the Server\n";
  23. int new_socket = *(int *)arg;
  24. int i;
  25. printf("[Server] Communicate with the new connection #%u @ %p ..\n",
  26. new_socket, (void *)(uintptr_t)pthread_self());
  27. for (i = 0; i < 5; i++) {
  28. if (send(new_socket, message, strlen(message), 0) < 0) {
  29. perror("Send failed");
  30. break;
  31. }
  32. }
  33. printf("[Server] Shuting down the new connection #%u ..\n", new_socket);
  34. shutdown(new_socket, SHUT_RDWR);
  35. return NULL;
  36. }
  37. int
  38. main(int argc, char *argv[])
  39. {
  40. int socket_fd = -1, addrlen = 0;
  41. struct sockaddr_in addr = { 0 };
  42. unsigned connections = 0;
  43. pthread_t workers[WORKER_NUM] = { 0 };
  44. int client_sock_fds[WORKER_NUM] = { 0 };
  45. char ip_string[16];
  46. printf("[Server] Create socket\n");
  47. socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  48. if (socket_fd < 0) {
  49. perror("Create socket failed");
  50. goto fail;
  51. }
  52. /* 0.0.0.0:1234 */
  53. addr.sin_family = AF_INET;
  54. addr.sin_port = htons(1234);
  55. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  56. printf("[Server] Bind socket\n");
  57. addrlen = sizeof(addr);
  58. if (bind(socket_fd, (struct sockaddr *)&addr, addrlen) < 0) {
  59. perror("Bind failed");
  60. goto fail;
  61. }
  62. printf("[Server] Listening on socket\n");
  63. if (listen(socket_fd, 3) < 0) {
  64. perror("Listen failed");
  65. goto fail;
  66. }
  67. printf("[Server] Wait for clients to connect ..\n");
  68. while (connections < WORKER_NUM) {
  69. client_sock_fds[connections] =
  70. accept(socket_fd, (struct sockaddr *)&addr, (socklen_t *)&addrlen);
  71. if (client_sock_fds[connections] < 0) {
  72. perror("Accept failed");
  73. break;
  74. }
  75. inet_ntop(AF_INET, &addr.sin_addr, ip_string,
  76. sizeof(ip_string) / sizeof(ip_string[0]));
  77. printf("[Server] Client connected (%s:%d)\n", ip_string,
  78. ntohs(addr.sin_port));
  79. if (pthread_create(&workers[connections], NULL, run,
  80. &client_sock_fds[connections])) {
  81. perror("Create a worker thread failed");
  82. shutdown(client_sock_fds[connections], SHUT_RDWR);
  83. break;
  84. }
  85. connections++;
  86. }
  87. if (connections == WORKER_NUM) {
  88. printf("[Server] Achieve maximum amount of connections\n");
  89. }
  90. for (int i = 0; i < WORKER_NUM; i++) {
  91. pthread_join(workers[i], NULL);
  92. }
  93. printf("[Server] Shuting down ..\n");
  94. shutdown(socket_fd, SHUT_RDWR);
  95. sleep(3);
  96. printf("[Server] BYE \n");
  97. return EXIT_SUCCESS;
  98. fail:
  99. printf("[Server] Shuting down ..\n");
  100. if (socket_fd >= 0)
  101. close(socket_fd);
  102. sleep(3);
  103. return EXIT_FAILURE;
  104. }