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
| int main(int args, char *argv[]){
if (args!=2) {
printf("Usage : serveur port\n");
exit(0);
}
int serv, client[NB_MAX_CLIENTS], nbcli = 0, maxFd, i, cli, val;
socklen_t taille=sizeof(struct sockaddr_in);
struct sockaddr_in cli_addr;
int sock=socketServeur(atoi(argv[1]));
if (sock==-1) exit(0);
fd_set fd;
pthread_t t, t1, t2;
while(1){
FD_ZERO(&fd);
FD_SET(sock, &fd);
for (int i=0; i<nbcli; i++)
FD_SET(client[i], &fd);
maxFd = sock;
for(int i = 0; i < nbcli; i++){
if (client[i]>maxFd)
maxFd=client[i];
}
select(maxFd+1, &fd, NULL, NULL, NULL);
if(FD_ISSET(sock, &fd)){
cli = accept(sock, (struct sockaddr *)&cli_addr, &taille);
client[nbcli++] = cli;
struct hostent *info;
info=gethostbyaddr((char *)&(cli_addr.sin_addr.s_addr), 4, AF_INET);
printf("Nouvelle connexion avec %s...\n", info->h_name);
}
for(int i = 0; i < nbcli; i++){
if(FD_ISSET(client[i], &fd)){
cout << "Ici" <<endl;
pthread_create(&t, NULL, gere_etudiant, (void *)&client[i]);
pthread_create(&t2, NULL, gere_salle, (void *)&client[i]);
pthread_join(t, NULL);
pthread_join(t2, NULL);
}
}
}
} |
Partager