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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <semaphore.h>
#define false 0
#define true 1
int J[2] = {-1, -1};
int nCoin = 15;
sem_t semaphore;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int cree_socket_tcp_ip()
{
int sock;
struct sockaddr_in adresse;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "Erreur socket\n");
return -1;
}
memset(&adresse, 0, sizeof(struct sockaddr_in));
adresse.sin_family = AF_INET;
adresse.sin_port = htons(8081);
inet_aton("127.0.0.1", &adresse.sin_addr);
if (bind(sock, (struct sockaddr*) &adresse, sizeof(struct sockaddr_in)) < 0)
{
close(sock);
fprintf(stderr, "Erreur bind\n");
return -1;
}
return sock;
}
void initCoin(char tab[])
{
tab[0] = '|';
tab[1] = ' ';
tab[2] = ' ';
tab[3] = ' ';
tab[4] = ' ';
for (int i = 5; i < 5 + nCoin;i++)
{
tab[i] = 'O';
}
tab[nCoin + 5] = ' ';
tab[nCoin + 6] = ' ';
tab[nCoin + 7] = ' ';
tab[nCoin + 8] = ' ';
tab[nCoin + 9] = '|';
tab[nCoin + 10] = '\n';
tab[nCoin + 11] = '\0';
}
void takeCoin(int sock)
{
int coin = 0;
char tCoin[100] = "";
char tab[100];
int valid = false;
printf("In fonction takeCoin.\n");
while (valid == false)
{
if (J[0] == sock)
{
printf("Joueur 1 doit choisir");
sprintf(tab,"Joueur 1 entrez un nombre (1-3) : ");
send(sock,tab,strlen(tab),0);
}
else
{
printf("Joueur 2 doit choisir");
sprintf(tab,"Joueur 2 entrez un nombre (1-3) : ");
send(sock,tab,strlen(tab),0);
}
fflush(stdout);
recv(sock,tCoin,100,0);
printf("tCoin : %s\n",tCoin);
coin = atoi(tCoin);
if ((coin > 0) && (coin <= 3))
valid = true;
else
{
sprintf(tab,"Le nombre doit être entre 1 et 3 bord inclus.\n");
write(sock,tab,46*sizeof(char));
}
}
}
void *func_j1(void *arg)
{
int sock = *((int *)arg);
int nCoinT;
char tab[100];
char tCoin[3];
printf("sock : %d\n", sock);
fflush(stdout);
sprintf(tab,"Jeux des pièces.\n");
send(sock,tab,strlen(tab),0);
sprintf(tab,"Vous etes le joueur 1.\n");
send(sock,tab,strlen(tab),0);
sprintf(tab,"Attende du joueur 2.\n");
send(sock,tab,strlen(tab),0);
sem_wait(&semaphore);
sprintf(tab,"joueur 2 Trouver.\n");
send(sock,tab,strlen(tab),0);
fflush(stdin);
sprintf(tab,"La partie commence.\n");
send(sock,tab,strlen(tab),0);
initCoin(tab);
send(sock,tab,strlen(tab),0);
pthread_mutex_lock(&mutex);
nCoinT = nCoin;
pthread_mutex_unlock(&mutex);
while (nCoinT >= 2)
{
takeCoin(sock);
}
close(sock);
pthread_exit(NULL);
}
void *func_j2(void *arg)
{
int sock = *((int *)arg);
int nCoinT;
int coin;
char tab[100];
printf("sock : %d\n", sock);
sprintf(tab,"Jeux des pièces.\n");
write(sock,tab,strlen(tab));
sem_post(&semaphore);
sprintf(tab,"Vous etes le joueur 2.\n");
write(sock,tab,strlen(tab));
sprintf(tab,"La partie commencer.\n");
write(sock,tab,strlen(tab));
initCoin(tab);
write(sock,tab,strlen(tab));
pthread_mutex_lock(&mutex);
nCoinT = nCoin;
pthread_mutex_unlock(&mutex);
while (nCoinT >= 2)
{
sprintf(tab,"Le joueur 1 est en train de jouer.\n");
write(sock,tab,strlen(tab));
sem_wait(&semaphore);
//takeCoin(sock, coin);
}
close(sock);
pthread_exit(NULL);
}
int affiche_adresse_socket(int sock)
{
struct sockaddr_in adresse;
socklen_t longueur;
longueur = sizeof(struct sockaddr_in);
if (getsockname(sock, (struct sockaddr*)&adresse, &longueur) < 0)
{
fprintf(stderr, "Erreur getsockname\n");
return -1;
}
printf("IP = %s, Port = %u\n", inet_ntoa(adresse.sin_addr), ntohs(adresse.sin_port));
return 0;
}
int main(void)
{
int sock_contact;
int sock_connectee;
int j = 0;
pthread_t th1;
pthread_t th2;
struct sockaddr_in adresse;
socklen_t longueur;
sock_contact = cree_socket_tcp_ip();
if (sock_contact < 0)
return -1;
listen(sock_contact, 5);
printf("Mon adresse (sock contact) -> ");
affiche_adresse_socket(sock_contact);
sem_init (&semaphore, 0, 0);
while (1)
{
printf("Attende d'un client.\n");
longueur = sizeof(struct sockaddr_in);
sock_connectee = accept(sock_contact, (struct sockaddr*)&adresse, &longueur);
printf("Client trouver.\n");
J[j] = sock_connectee;
if (j == 0)
pthread_create(&th1, NULL, func_j1, &sock_connectee);
else
pthread_create(&th2, NULL, func_j2, &sock_connectee);
j++;
}
return 0;
} |
Partager