1
0
Fork 0

Zaktualizuj 'server/server.c'

This commit is contained in:
Adam Toppmayer 2019-11-09 16:34:44 +00:00
parent b3e7938dc4
commit f9d2f53d81
1 changed files with 55 additions and 22 deletions

View File

@ -52,49 +52,55 @@ int main(void) {
struct sockaddr_in adr_accept;
int socket_bind, socket_accept;
char msg[1024];
pid_t child_id;
int socket_client[5];
socket_client client;
client_socket client;
fd_set Clients;
struct sockaddr_in nadawca;
socklen_t dl = sizeof(struct sockaddr_in);
int i;
int j;
for(i = 0; i < 4; i++)
//Inicjalizacja Tablicy Klientów
for(i=0; i<=4; i++)
{
clients[i].socket = 0;
client[i].socket = 0;
}
//INICJALIZACJA SERWERA
//Bind
socket_bind = socket(AF_INET, SOCK_STREAM, 0);
if(socket_bind < 0) {
printf("Error: Socket\n");
return 1;
}
//Adres
adr_bind.sin_family = AF_INET;
adr_bind.sin_port = htons(44444);
adr_bind.sin_addr.s_addr = INADDR_ANY;
if (bind(socket_bind, (struct sockaddr*) &adr_bind, sizeof(adr_bind)) < 0) { //bind
if (bind(socket_bind, (struct sockaddr*) &adr_bind, sizeof(adr_bind)) < 0)
{
printf("Error: Bind\n");
return 1;
}
//Listen
if (listen(socket_bind, 10) < 0) { //listen
printf("Error: Listen\n");
return 1;
}
//printf("Czekam na polaczenie ...\n");
//printf("Inicjalizacja Zakończona\n");
//NASŁUCHIWANIE KLIENTÓW
while (1) {
//Aktualizacja deskryptorów
max_fd = 0;
FD_ZERO(Clients);
FD_SET(socket_bind, Clients);
for(i =0; i<4; i++){
//Dodanie deskryptorów Klientów
for(i =0; i<=4; i++){
if (client[i].socket > 0) {
FD_SET(client[i].socket, Clients);
@ -104,40 +110,67 @@ int main(void) {
}
}
//Sprawdzenie Poprawności
if(select(max_fd+1, &Clients, NULL, NULL, NULL) < 0) {
ERROR
printf("Error: Select\n");
close(socket_bind);
return 0;
}
//Dodanie Klienta
if(FD_ISSET(socket_bind, &Clients)) {
for(i=0;i<4;i++){
for(i=0;i<=4;i++){
if (client[i].socket == 0){
client[i].socket = accept(socket_bind, (struct sockaddr*) &adr_bind, &dl));
client[i].socket = accept(socket_bind, (struct sockaddr*) &client[i].address, &dl));
break;
} else {
printf("Server Pełen");
printf("Server Pełen\n");
}
}
} else {
printf("Error: Socket_bind nie jest w zbiorze deskryptorów");
}
for(i = 0; i < 4; i++)
//PRZETWARZANIE WIADOMOŚCI
for(i = 0; i<=4; i++)
{
if(client[i].socket > 0 && FD_ISSET(clients[i].socket, &Clients))
{
recv(clients[i].socket, &msg, 1024, 0);
if (msg == "EXIT") {
} else if (msg == "DOWNLOAD") {
//fork, który dostanie adres i plik i będzie zajmował się wysyłaniem
} else if (msg == "SEND"){
//Co zrobić z wiadomością?
if (msg == "/EXIT") {
for(j=0;j<=4;j++) {
if(i == j && client[j].socket != 0) {
close(client[i].socket);
client[i].socket = 0;
printf("Ktoś wyszedł\n");
}
}
} else if (msg == "/DOWNLOAD") {
//fork, który dostanie adres i plik i będzie zajmował się ściąganiem
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++) {
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) {
close(client[i].socket);
client[i].socket = 0;
printf("User disconnected\n");
}
}
printf("Koniec Serwera\n");
return 0;
}
}
}