Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

108 lignes
2.9 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include <unistd.h>
  8. #define SERVER_IP_ADDR "127.0.0.1"
  9. #define SERVER_PORT 2000
  10. #define SECRET_NUMBER 42
  11. struct sockaddr_in server_addr, client_addr;
  12. int socket_desc, client_struct_length = sizeof(client_addr);
  13. char client_message[2000];
  14. // Function to send a message to the client
  15. int send_message(char *message)
  16. {
  17. if (sendto(socket_desc, message, strlen(message), 0,
  18. (struct sockaddr *)&client_addr, client_struct_length) < 0)
  19. {
  20. printf("Failed to send a message to client\n");
  21. return -1;
  22. }
  23. return 0;
  24. }
  25. int main(void)
  26. {
  27. // Create UDP socket
  28. socket_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  29. if (socket_desc < 0)
  30. {
  31. printf("Failed to create a socket\n");
  32. return -1;
  33. }
  34. // Set IP and Port
  35. server_addr.sin_family = AF_INET;
  36. server_addr.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR);
  37. server_addr.sin_port = htons(SERVER_PORT);
  38. // Bind the socket to IP:Port
  39. if (bind(socket_desc, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
  40. {
  41. printf("Failed to bind the socket\n");
  42. return -1;
  43. }
  44. printf("Server is listening on %s:%d\n", SERVER_IP_ADDR, SERVER_PORT);
  45. // Generate a random number between 1 and 42
  46. srand(time(NULL));
  47. int target = rand() % SECRET_NUMBER + 1;
  48. for (int i = 0; i < SECRET_NUMBER; i++)
  49. {
  50. // Receive a message from client
  51. memset(client_message, '\0', sizeof(client_message));
  52. if (recvfrom(socket_desc, client_message, sizeof(client_message), 0,
  53. (struct sockaddr *)&client_addr, &client_struct_length) < 0)
  54. {
  55. printf("Failed to receive a message from client\n");
  56. return -1;
  57. }
  58. // Print the received message
  59. printf("%s:%i: %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), client_message);
  60. // If message is not a number b/w 1 and SECRET_NUMBER send an error and continue
  61. int n = atoi(client_message);
  62. if (n <= 0 || n > SECRET_NUMBER)
  63. {
  64. if (send_message("ERROR\0"))
  65. return -1;
  66. continue;
  67. }
  68. // If a correct number was guessed. Send WIN and terminate.
  69. if (n == target)
  70. {
  71. if (send_message("WIN\0"))
  72. return -1;
  73. close(socket_desc);
  74. return 0;
  75. }
  76. // If a wrong number was guessed, send a hint
  77. if (n < target) {
  78. printf("LESS %d %d\n", n, target);
  79. } else if (n > target){
  80. printf("MORE %d %d\n", n, target);
  81. } else {
  82. printf("EQUAL %d %d\n", n, target);
  83. }
  84. if (send_message(n < target ? "LESS\0" : "MORE\0"))
  85. return -1;
  86. }
  87. if (send_message("LOSE\0"))
  88. return -1;
  89. close(socket_desc);
  90. return 0;
  91. }