forked from s444341/Projekt_SIK
commit
This commit is contained in:
parent
2a468af441
commit
120a454d9d
@ -12,25 +12,27 @@ struct client_socket
|
||||
{
|
||||
int socket;
|
||||
struct sockaddr_in accept_adr;
|
||||
char name[20] = "";
|
||||
//char name[20] = "";
|
||||
};
|
||||
|
||||
int main(void) {
|
||||
struct sockaddr_in bind_adr;
|
||||
int socket_bind;
|
||||
char msg[1024];
|
||||
client_socket client[7];
|
||||
struct client_socket client[7];
|
||||
fd_set Clients;
|
||||
fd_set Socket;
|
||||
socklen_t dl = sizeof(struct sockaddr_in);
|
||||
int i;
|
||||
int j;
|
||||
char ff;
|
||||
int max_fd;
|
||||
//Inicjalizacja Tablicy Klientów
|
||||
for(i=0; i<=4; i++)
|
||||
{
|
||||
client[i].socket = 0;
|
||||
}
|
||||
|
||||
printf("Tablica Klientow\n");
|
||||
//INICJALIZACJA SERWERA
|
||||
//Bind
|
||||
socket_bind = socket(AF_INET, SOCK_STREAM, 0);
|
||||
@ -38,61 +40,69 @@ int main(void) {
|
||||
printf("Error: Socket\n");
|
||||
return 1;
|
||||
}
|
||||
printf("Socket\n");
|
||||
//Adres
|
||||
adr_bind.sin_family = AF_INET;
|
||||
adr_bind.sin_port = htons(44444);
|
||||
adr_bind.sin_addr.s_addr = INADDR_ANY;
|
||||
bind_adr.sin_family = AF_INET;
|
||||
bind_adr.sin_port = htons(44444);
|
||||
bind_adr.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
if (bind(socket_bind, (struct sockaddr*) &bind_adr, sizeof(bind_adr)) < 0)
|
||||
{
|
||||
printf("Error: Bind\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Bind\n");
|
||||
//Listen
|
||||
if (listen(socket_bind, 10) < 0) { //listen
|
||||
printf("Error: Listen\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Listen\n");
|
||||
//printf("Inicjalizacja Zakończona\n");
|
||||
|
||||
//NASŁUCHIWANIE KLIENTÓW
|
||||
while (1) {
|
||||
scanf("%c", &ff);
|
||||
/*scanf("%c", &ff);
|
||||
printf("\n");
|
||||
if(ff == 'e') {
|
||||
return 0;
|
||||
}
|
||||
ff = 'a';
|
||||
ff = 'a';*/
|
||||
printf("While\n");
|
||||
//Aktualizacja deskryptorów
|
||||
max_fd = 0;
|
||||
FD_ZERO(Clients);
|
||||
FD_SET(socket_bind, Clients);
|
||||
FD_ZERO(&Clients);
|
||||
FD_ZERO(&Socket);
|
||||
FD_SET(socket_bind, &Socket);
|
||||
|
||||
//Dodanie deskryptorów Klientów
|
||||
for(i =0; i<=4; i++){
|
||||
|
||||
if (client[i].socket > 0) {
|
||||
FD_SET(client[i].socket, Clients);
|
||||
FD_SET(client[i].socket, &Clients);
|
||||
if(client[i].socket > max_fd) {
|
||||
max_fd = client[i].socket;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("FD_SET\n");
|
||||
//Sprawdzenie Poprawności
|
||||
if(select(max_fd+1, &Clients, NULL, NULL, NULL) < 0) {
|
||||
printf("Error: Select\n");
|
||||
close(socket_bind);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//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
|
||||
if(FD_ISSET(socket_bind, &Clients)) {
|
||||
//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){
|
||||
client[i].socket = accept(socket_bind, (struct sockaddr*) &client[i].address, &dl));
|
||||
//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");
|
||||
@ -101,14 +111,15 @@ int main(void) {
|
||||
} else {
|
||||
printf("Error: Socket_bind nie jest w zbiorze deskryptorów");
|
||||
}
|
||||
|
||||
printf("Accept\n");
|
||||
//PRZETWARZANIE WIADOMOŚCI
|
||||
for(i = 0; i<=4; i++)
|
||||
{
|
||||
if(client[i].socket > 0 && FD_ISSET(client[i].socket, &Clients))
|
||||
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++) {
|
||||
@ -120,17 +131,10 @@ int main(void) {
|
||||
}
|
||||
} else if (msg == "/DOWNLOAD") {
|
||||
//fork, który dostanie adres i plik i będzie zajmował się ściąganiem
|
||||
if(fork()==0) {
|
||||
//if(fork()==0) {
|
||||
//send()
|
||||
}
|
||||
} else if (msg == "/SEND"){
|
||||
//fork, który dostanie adres i plik i będzie zajmował się wysyłaniem
|
||||
} else {
|
||||
for(j=0;j<=4;j++) {
|
||||
if(i != j && client[j].socket != 0) {
|
||||
send(client[i].socket, &msg, 1024, 0);
|
||||
}
|
||||
}
|
||||
} else if (msg == "/EXIT_M" || msg == "/EXIT_M\n"){
|
||||
for(j=0;j<=4;j++) {
|
||||
if(client[j].socket != 0) {
|
||||
@ -141,11 +145,14 @@ int main(void) {
|
||||
}
|
||||
printf("Koniec Serwera\n");
|
||||
return 0;
|
||||
} else {
|
||||
for(j=0;j<=4;j++) {
|
||||
if(i!=j && client[j].socket!=0) {
|
||||
send(client[j].socket, &msg, 1024, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user