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
|
/*-------------------------------------------------------------------------*/
main(int argc, char *argv[]){
/* Tranformation en demon et creation d'une session */
if (fork()!=0){ exit(0); }
setsid();
printf("serveur de pid %d lance\n", (int)getpid() );
action.sa_handler = finFils;
sigaction(SIGCHLD, &action, NULL);
/* creation et attachement de la socket d'ecoute */
ecoute=creerSocket(SOCK_STREAM, port, &adr);
if (ecoute == -1){ fprintf(stderr, "creation de la socket d'ecoute impossible\n"); exit(2); }
/* declaration d'ouverture de service */
if (listen(ecoute,150) == -1) { perror("listen"); exit(2); }
while(TRUE) { //boucle d'attente de connexion
if (itrt > 1000000000){itrt=0;}
waitpid(-1,NULL,WNOHANG); // recupere un fils zombie
waitpid(-1,NULL,WNOHANG); // recupere un fils zombie
connexion = accept(ecoute, (struct sockaddr *)&adr, &lg_adr);
if (connexion == -1 && errno == EINTR) {continue;} //probalement interrompu par SIGCHLD, sinon ...
if (connexion == -1 && errno != EINTR) {perror("accept"); exit(2);}
NBREQa++; if (NBREQa > 999999) {NBREQa=0; NBREQb++;}// avant fork
if (fork() == 0) { // un processus pour la nouvelle connexion
demmarertimer();
dup2(connexion, qcon);
close(connexion); close(ecoute);
int lolo=servirconnexion();
close (qcon);
exit(2);
}
close(connexion);
itrt=itrt+1;
}
}
/*-----------------------------------------------------------------------*/ |
Partager