-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
74 lines (60 loc) · 2 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT 8080
int main() {
int sock;
struct sockaddr_in serv_addr;
char buffer[1024] = {0};
char guess[2];
char choice[2];
do {
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}
int gameFinished = 0;
while (!gameFinished) {
int valread = read(sock, buffer, 1024);
if (valread <= 0) {
printf("Erreur de connexion avec le serveur.\n");
close(sock);
return -1;
}
buffer[valread] = '\0';
// Check if the game is finished
if (strcmp(buffer, "You've guessed the word!\n") == 0) {
printf("%s", buffer);
gameFinished = 1;
break;
} else {
// Print the updated word or response from server
printf("%s\n", buffer);
}
if (!gameFinished) {
printf("Entrez une lettre: ");
scanf(" %c", &guess[0]); // Using %c to read a single character
send(sock, guess, 1, 0); // Sending only one character
}
}
close(sock);
if (gameFinished) {
printf("Voulez-vous jouer une autre partie? (y/n): ");
scanf(" %c", &choice[0]); // Using %c to read a single character
}
} while (choice[0] == 'y' || choice[0] == 'Y');
return 0;
}