diff --git a/server/server.c b/server/server.c index e864d7f..96759a9 100644 --- a/server/server.c +++ b/server/server.c @@ -23,6 +23,7 @@ int main(void) { fd_set Clients; fd_set Socket; socklen_t dl = sizeof(struct sockaddr_in); + int temp_accept; int i; int j; char ff; @@ -62,21 +63,39 @@ int main(void) { //NASŁUCHIWANIE KLIENTÓW while (1) { - /*scanf("%c", &ff); - printf("\n"); - if(ff == 'e') { - return 0; - } - ff = 'a';*/ - printf("While\n"); //Aktualizacja deskryptorów max_fd = 0; FD_ZERO(&Clients); - FD_ZERO(&Socket); + FD_ZERO(&Socket); FD_SET(socket_bind, &Socket); //Dodanie deskryptorów Klientów - for(i =0; i<=4; i++){ + + //Sprawdzenie Poprawności + //printf("Przed Select\n"); + if(select(socket_bind+1, &Socket, NULL, NULL, NULL) < 0) { + if (FD_ISSET(socket_bind, &Socket)) { + temp_accept = accept(socket_bind, (struct sockaddr*) &client[i].accept_adr, &dl) + for(i=0;i<=4;i++){ + if (client[i].socket == temp_accept){ + recv(client[i].socket, &msg, 1024, 0); + break; + } + } + if(i>4) { + for(i=0;i<=4;i++){ + if (client[i].socket == 0){ + client[i].socket = accept(socket_bind, (struct sockaddr*) &client[i].accept_adr, &dl); + break; + } else { + printf("Miejsce %i zajęte\n", i); + } + } + } + } + } + //Set Clients + for(i =0; i<=4; i++){ if (client[i].socket > 0) { FD_SET(client[i].socket, &Clients); @@ -85,41 +104,27 @@ int main(void) { } } } - printf("FD_SET\n"); - //Sprawdzenie Poprawności - //printf("Przed Select\n"); - //if(select(max_fd+1, &Clients, NULL, NULL, NULL) < 0) { - // printf("Error: Select\n"); - // close(socket_bind); - // return 0; - //} - //printf("Po Select\n"); - //Dodanie Klienta - //printf("Isset Przed\n"); - if(FD_ISSET(socket_bind, &Socket)) { - printf("Isset Po\n"); - for(i=0;i<=4;i++){ - if (client[i].socket == 0){ - //printf("Przed\n"); - client[i].socket = accept(socket_bind, (struct sockaddr*) &client[i].accept_adr, &dl); - //printf("Po\n"); - break; - } else { - printf("Server Pełen\n"); - } - } - } else { - printf("Error: Socket_bind nie jest w zbiorze deskryptorów"); - } - printf("Accept\n"); + printf("Wiadomość:%s\n", msg); //PRZETWARZANIE WIADOMOŚCI - for(i = 0; i<=4; i++) + if(select(max_fd+1, NULL, &Clients, NULL, NULL) < 0) { + + for(i=0;i<=4;i++){ + if (FD_ISSET(client[i].socket, &Clients)) { + for(j=0;j<=4;j++) { + if(i!=j && client[j].socket!=0) { + send(client[j].socket, &msg, 1024, 0); + } + } + } + } + + } + + /*for(i = 0; i<=4; i++) { if(client[i].socket != 0 && FD_ISSET(client[i].socket, &Clients)) { - recv(client[i].socket, &msg, 1024, 0); - printf("%c\n", msg[0]); - printf("Widomość ^^^\n"); + //Co zrobić z wiadomością? if (msg == "/EXIT") { for(j=0;j<=4;j++) { @@ -153,6 +158,6 @@ int main(void) { } } } - } + }*/ } }