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
|
La fonction thread qui me permets de créer mes groupes
void * groupCreationThreadFun(void* data){
Data * pData = (Data*)data;
char buffer[BUF_SIZE];
int nbOfAnswers = 0; //compte le nombre de clients qui ont déjà répondu à la requete
int max=0; // pour la selection de socket
SOCKET sock = init_connection(PORT+1); // création de la socket de connexion du serveur
fd_set readset;
FD_ZERO(&readset);
for (int i=0 ; i< pData->nbOfGuests ; i++){ //j'établis une connexion avec chacun des clients concernés
SOCKET csock;
SOCKADDR_IN csin = { 0 };
socklen_t csinsize = sizeof(csin);
/* création de la demande au client */
char * question = malloc(200);
strcpy(question,"joingroup voulez-vous rejoindre le groupe "); //le mot-clé "joingroup me permet de détecter qu'il s'agit d'une requete pour rejoindre un groupe côté client
strcat(question,pData->group.name);
strcat(question," ?");
/* envoi de la demande au client */
write_client(pData->guests[i]->sock,question);
/* attente de la connexion du client */
csock = accept(sock, (SOCKADDR *)&csin, &csinsize);
if(csock == SOCKET_ERROR)
perror("accept()");
/* on mémorise la socket pour la suite */
pData->guests[i]->tempSock = csock;
/* On ajoute la socket du client à readset,
mais j'ai l'impression que ça foire un peu... */
FD_SET(pData->guests[i]->tempSock,&readset);
max = csock > max ? csock : max; // calcul du nouveau max, pour le select
}
while(nbOfAnswers < pData->nbOfGuests){ // On attend que tous les clients aient répondu à la requete
/* ci ça bloque au deuxième passage dans la boucle, car readset ne contient plus rien...
En tout cas quand je débug avec gdb, print readset renvoit {__fds_bits = {0 <repeated 16 times>}
J'ai l'impression que c'est FD_SET qui écrase la première socket au lieu d'en ajouter une nouvelle.*/
printf("ca bloque au select...\n)");
if(select(max , &readset, NULL, NULL, NULL) == -1){
perror("select()");
exit(errno);
}
for(int i = 0; i < pData->nbOfGuests; i++){
/* on test quel client a répondu */
if(FD_ISSET(pData->guests[i]->tempSock, &readset)){
read_client(pData->guests[i]->tempSock, buffer);
if( strcmp(buffer,"yes") == 0){
write_client(pData->guests[i]->tempSock,"You have been added to the group\n");
strcpy(buffer,pData->guests[i]->name);
strcat(buffer," accepted the invitation to your group");
write_client(pData->creator.sock,buffer);
}
if( strcmp(buffer,"no") == 0 ){
strcpy(buffer,pData->guests[i]->name);
strcat(buffer," refused the invitation to your group");
write_client(pData->creator.sock,buffer);
}
/* Je déconnecte le client qui vient de répondre */
FD_CLR(pData->guests[i]->tempSock,&readset);
closesocket(pData->guests[i]->tempSock);nbOfAnswers++;
printf("On ferme la socket du client %s\n",pData->guests[i]->name);
}
}
}
closesocket(sock);
return NULL;
} |
Partager