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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| while(1)
{
maxfd = fdConnection+1;
if(maxfd< STDIN_FILENO+1)
maxfd = STDIN_FILENO+1;
FD_ZERO(&setfd);
FD_SET(fdConnection, &setfd);
FD_SET(STDIN_FILENO, &setfd);
/** repositionnement pour chaque client */
clientListBegin();
for(i = 0; i<clientListGetSize(); i++)
{
int lsocket = clientListGetSocket(i);
FD_SET(lsocket, &setfd);
if(lsocket+1>maxfd)
maxfd = lsocket+1;
}
clientListEnd();
if(select(maxfd, &setfd, 0,0,0) == -1)
{
perror("select");
exit(EXIT_FAILURE);
}
if(FD_ISSET(STDIN_FILENO, &setfd))
{
fprintf(stderr, "Close program : \n");
break;
}
/*on matte ce que les gens racontent*/
clientListBegin();
for(i = 0; i<clientListGetSize(); i++)
{
lsocket = clientListGetSocket(i);
if(FD_ISSET(lsocket, &setfd))
{
//tailleget = read(lsocket, buffer, sizeof(buffer)-1);
tailleget = recv(lsocket, buffer, sizeof(buffer)-1, MSG_PEEK);
if(tailleget == -1)
{
perror("recv");
continue;
}
if(tailleget == 0)
{
fprintf(stderr, "Disconnection\n");
close(lsocket);
clientListPrepareToRemove(i);
continue;
}
char * realEnd;
realEnd = memchr(buffer, '\n', tailleget);
if(realEnd == NULL)
{
fprintf(stderr, "Server receive a too long packet\n");
continue;
}
tailleget = realEnd - buffer + 1;
tailleget = recv(lsocket, buffer, tailleget,0 );
if(tailleget == -1)
{ /*n'arrivera jamais ici*/
perror("recv");
}
else
{
/*fin de connexion ?*/ /*n'arrivera jamais ici*/
if(tailleget == 0)
{
fprintf(stderr, "Disconnection...\n");
close(lsocket);
clientListPrepareToRemove(i);
}
else
{
buffer[tailleget] = '\0';
printf(" * PACKET : : %s\n", buffer);
serverInterpreter(buffer, lsocket);
DEBUG("End serverInterpreter");
}
}
}
}
clientListEnd();
DEBUG("debut demande de connexion");
/* nouvelle demande de connexion ?*/
if(FD_ISSET(fdConnection, &setfd))
{
fprintf(stderr, "Demande de connexion....\n");
tailleaddr = sizeof addrClient;
clientSocket = accept(fdConnection, (struct sockaddr*) &addrClient,
&tailleaddr);
if(clientSocket == -1)
{
perror("accept");
continue;
}
//fprintf(stderr, " * Port : %d", ntohs(addrClient.sin_port));
if(clientListAdd(clientSocket, addrClient.sin_addr.s_addr, addrClient.sin_port) == -1)
{
fprintf(stderr, "Ajout client impossible\n");
}
}
DEBUG("fin demande de connexion");
} |
Partager