tcp_server.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. printf("[Server] Create socket\n");
  46. socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  47. if (socket_fd < 0) {
  48. perror("Create socket failed");
  49. goto fail;
  50. }
  51. /* 0.0.0.0:1234 */
  52. addr.sin_family = AF_INET;
  53. addr.sin_port = htons(1234);
  54. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  55. printf("[Server] Bind socket\n");
  56. addrlen = sizeof(addr);
  57. if (bind(socket_fd, (struct sockaddr *)&addr, addrlen) < 0) {
  58. perror("Bind failed");
  59. goto fail;
  60. }
  61. printf("[Server] Listening on socket\n");
  62. if (listen(socket_fd, 3) < 0) {
  63. perror("Listen failed");
  64. goto fail;
  65. }
  66. printf("[Server] Wait for clients to connect ..\n");
  67. while (connections < WORKER_NUM) {
  68. client_sock_fds[connections] =
  69. accept(socket_fd, (struct sockaddr *)&addr, (socklen_t *)&addrlen);
  70. if (client_sock_fds[connections] < 0) {
  71. perror("Accept failed");
  72. break;
  73. }
  74. printf("[Server] Client connected\n");
  75. if (pthread_create(&workers[connections], NULL, run,
  76. &client_sock_fds[connections])) {
  77. perror("Create a worker thread failed");
  78. shutdown(client_sock_fds[connections], SHUT_RDWR);
  79. break;
  80. }
  81. connections++;
  82. }
  83. if (connections == WORKER_NUM) {
  84. printf("[Server] Achieve maximum amount of connections\n");
  85. }
  86. for (int i = 0; i < WORKER_NUM; i++) {
  87. pthread_join(workers[i], NULL);
  88. }
  89. printf("[Server] Shuting down ..\n");
  90. shutdown(socket_fd, SHUT_RDWR);
  91. sleep(3);
  92. printf("[Server] BYE \n");
  93. return EXIT_SUCCESS;
  94. fail:
  95. printf("[Server] Shuting down ..\n");
  96. if (socket_fd >= 0)
  97. close(socket_fd);
  98. sleep(3);
  99. return EXIT_FAILURE;
  100. }