server_tcp_mthread.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <pthread.h>
  6. #include <arpa/inet.h>
  7. #define PORT 8081
  8. #define BUFFER_SIZE 1024
  9. // structure to store client data
  10. typedef struct {
  11. int client_socket;
  12. struct sockaddr_in client_addr;
  13. } client_data_t;
  14. //function to handle thread
  15. void *handle_client(void *arg)
  16. {
  17. client_data_t *data = (client_data_t *)arg;
  18. int client_socket = data->client_socket;
  19. struct sockaddr_in client_addr = data->client_addr;
  20. char buffer[BUFFER_SIZE]={0};
  21. int n;
  22. printf("client connected\n");
  23. while(1)
  24. {
  25. n = recv(client_socket, buffer, sizeof(buffer), 0);
  26. if (n <= 0)
  27. {
  28. printf("Client disconnected\n");
  29. break;
  30. }
  31. printf("Received from client: %s\n", buffer);
  32. send(client_socket, buffer, strlen(buffer), 0);
  33. }
  34. close(client_socket);
  35. free(data); // Free the memory allocated for client data
  36. pthread_exit(NULL);
  37. }
  38. void main()
  39. {
  40. int server_socket, client_socket;
  41. struct sockaddr_in server_addr={0}, client_addr;
  42. socklen_t client_len = sizeof(client_addr);
  43. pthread_t thread_id;
  44. // create server socket
  45. if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  46. {
  47. perror("Socket creation failed");
  48. exit(EXIT_FAILURE);
  49. }
  50. // server address setup
  51. server_addr.sin_family = AF_INET;
  52. server_addr.sin_addr.s_addr = INADDR_ANY; // Listen on all interfaces
  53. server_addr.sin_port = htons(PORT);
  54. //bind the server socket to port_no and ip
  55. if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
  56. {
  57. perror("Bind failed");
  58. close(server_socket);
  59. exit(EXIT_FAILURE);
  60. }
  61. //listen for connection
  62. if (listen(server_socket, 5) < 0)
  63. {
  64. perror("Listen failed");
  65. close(server_socket);
  66. exit(EXIT_FAILURE);
  67. }
  68. printf("server listening\n");
  69. while(1)
  70. {
  71. // accept incomming client
  72. client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_len);
  73. if (client_socket < 0)
  74. {
  75. perror("Client accept failed");
  76. continue; // Continue to accept new clients
  77. }
  78. //allocate memory for client structure
  79. client_data_t *data = malloc(sizeof(client_data_t));
  80. data->client_socket = client_socket;
  81. data->client_addr = client_addr;
  82. //create new thread to handle every new client
  83. if (pthread_create(&thread_id, NULL, handle_client, data) != 0)
  84. {
  85. perror("Thread creation failed");
  86. close(client_socket);
  87. free(data);
  88. } else
  89. {
  90. //Detach the thread to allow it to clean up automatically after finishing
  91. pthread_detach(thread_id);
  92. }
  93. }
  94. close(server_socket);
  95. }