#include #include #include #include #include #include #include #include #include #include #include #include #include #include pthread_mutex_t lockDeMiBufferPret =PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cambioDeBufferPret = PTHREAD_COND_INITIALIZER; int bufferpret=0; char buf[1024]; int yes=1; int port_monitor; int port_server; int numClient; int N; void * monitor(void *arg){ struct sockaddr_in name2; int s2, n2, ns2, len2; char buf2[1024]; char num[2]; char receptCltNum[23]; char interruptMsg[]="deconnecte"; //Puerto 2 : monitor s2 = socket(AF_INET, SOCK_STREAM, 0); if (setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) { perror("setsockopt"); exit(1); } name2.sin_family = AF_INET; name2.sin_port = htons(port_monitor); name2.sin_addr.s_addr = htonl(INADDR_ANY); len2 = sizeof(struct sockaddr_in); bind(s2, (struct sockaddr *) &name2, len2); listen(s2, 5); ns2 = accept(s2, (struct sockaddr *) &name2, &len2); printf("Monitor connected...\n"); while(1){ pthread_mutex_lock(&lockDeMiBufferPret); while(bufferpret!=1) pthread_cond_wait(&cambioDeBufferPret,&lockDeMiBufferPret); //Envoie du numero client sprintf(num,"%d",numClient); //num=itoa(numClient); send(ns2,num,sizeof(num),0); //Attente de reponse monitor n2=recv(s2,receptCltNum,sizeof(receptCltNum),MSG_WAITALL); send(ns2,buf,N,0); bufferpret=0; pthread_mutex_unlock(&lockDeMiBufferPret); pthread_cond_signal(&cambioDeBufferPret); //close(ns2); //close(s2); } } int main(int argc, char *argv[]) { if (argc < 3){ printf("Usage : %s \n", argv[0]); exit(1); } int s, n, ns, len; struct sockaddr_in name; fd_set readfds; int size=0; int i , taille1 = FD_SETSIZE; int *ptr; port_monitor=atoi(argv[2]); ptr =calloc(taille1, sizeof(ptr)); //On alloue 'taille1' pointeurs. if(ptr == NULL) exit(1); //notre tableau ptr[2][3] est maintenant utilisable... /* Create the socket. */ s = socket(AF_INET, SOCK_STREAM, 0); if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) { perror("setsockopt"); exit(1); } //Thread pthread_t tid; pthread_create(&tid,NULL,monitor,NULL); /* Create the address of the server. */ name.sin_family = AF_INET; name.sin_port = htons(atoi(argv[1])); name.sin_addr.s_addr = htonl(INADDR_ANY); /* Use the wildcard address.*/ len = sizeof(struct sockaddr_in); /* Bind the socket to the address. */ if( bind(s, (struct sockaddr *) &name, len)) printf("bind error"); /* Listen for connections. */ listen(s, 5); printf("Waiting for connection...\n"); ptr[0]=s; size++; while(1){ //printf("boucle\n"); FD_ZERO(&readfds); int j; int sockmax=0; int itb; //printf("pas 1\n"); for(j=0; j0){ bufferpret=1; pthread_mutex_unlock(&lockDeMiBufferPret); pthread_cond_signal(&cambioDeBufferPret); } else{ close(ptr[i]); ptr[i]=ptr[size-1]; ptr[size-1]=0; size--; pthread_mutex_unlock(&lockDeMiBufferPret); } //pthread_mutex_unlock(&lockDeMiBufferPret); } } } close(ns); close(s); exit(0); }