Commit b58103fffad4be486b800d3b30bb6620e97de7e4

Authored by achemin1
1 parent 19fd5674

feat : now detects and handle the disconnects oh clients

Showing 1 changed file with 23 additions and 4 deletions   Show diff stats
  1 +#define _GNU_SOURCE
1 #include <sys/types.h> 2 #include <sys/types.h>
2 #include <sys/socket.h> 3 #include <sys/socket.h>
3 #include <stdlib.h> 4 #include <stdlib.h>
@@ -105,6 +106,16 @@ int serv_gestionClient(int s){ @@ -105,6 +106,16 @@ int serv_gestionClient(int s){
105 } 106 }
106 107
107 108
  109 +void removeSocket(struct pollfd descr[], int i, int length){
  110 + for (int j=i;j<length-1;j++)
  111 + {
  112 + descr[j]=descr[j+1];
  113 +
  114 + }
  115 +
  116 +
  117 +}
  118 +
108 119
109 int boucleServeur2(int ecoute) 120 int boucleServeur2(int ecoute)
110 { 121 {
@@ -115,14 +126,15 @@ int boucleServeur2(int ecoute) @@ -115,14 +126,15 @@ int boucleServeur2(int ecoute)
115 //initialiser POLLIN 126 //initialiser POLLIN
116 for (int i=0;i<1024;i++) 127 for (int i=0;i<1024;i++)
117 { 128 {
118 - descripteurs[i].events=POLLIN; 129 + descripteurs[i].events=POLLIN|POLLRDHUP|POLLERR;
119 } 130 }
120 int LongueurPoll = 1; 131 int LongueurPoll = 1;
121 132
122 while(1){ 133 while(1){
123 134
  135 +
124 //poll 136 //poll
125 - int nb = poll(descripteurs, 1024, -1); 137 + int nb = poll(descripteurs, LongueurPoll, -1);
126 138
127 if(nb<0){ perror("main.poll"); exit(EXIT_FAILURE); } 139 if(nb<0){ perror("main.poll"); exit(EXIT_FAILURE); }
128 140
@@ -141,14 +153,21 @@ int boucleServeur2(int ecoute) @@ -141,14 +153,21 @@ int boucleServeur2(int ecoute)
141 } 153 }
142 154
143 for(int i = 0; i < LongueurPoll-1; i++){ 155 for(int i = 0; i < LongueurPoll-1; i++){
144 - if( (descripteurs[i+1].revents&POLLIN) !=0){ //A lire 156 + if( (descripteurs[i+1].revents&POLLRDHUP) !=0){
  157 + printf("|%i$ Deconnection\n", i+1);
  158 +
  159 + //FONCTION ENLEVER UN SOCKET
  160 + removeSocket(descripteurs,i+1,LongueurPoll );
  161 + LongueurPoll --;
  162 + }
  163 + else if( (descripteurs[i+1].revents&POLLIN) !=0){ //A lire
145 //printf("Gestion Client %i \n", i+1); 164 //printf("Gestion Client %i \n", i+1);
  165 +
146 FILE* f = fdopen(descripteurs[i+1].fd, "a+"); 166 FILE* f = fdopen(descripteurs[i+1].fd, "a+");
147 167
148 char ligne[MAX_LIGNE]; 168 char ligne[MAX_LIGNE];
149 fgets(ligne,MAX_LIGNE,f); 169 fgets(ligne,MAX_LIGNE,f);
150 printf(">%i$ %s\n", i+1, ligne); 170 printf(">%i$ %s\n", i+1, ligne);
151 -sleep(1);  
152 171
153 } 172 }
154 } 173 }