1
0
Projekt_SIK/server/server.c

159 lines
4.4 KiB
C
Raw Normal View History

2019-10-31 11:17:48 +01:00
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
2019-11-09 21:30:49 +01:00
#include <sys/stat.h>
#include <stdlib.h>
2019-11-01 18:34:46 +01:00
2019-11-05 18:54:11 +01:00
struct client_socket
{
int socket;
2019-11-09 22:39:59 +01:00
struct sockaddr_in accept_adr;
//char name[20] = "";
2019-11-05 18:54:11 +01:00
};
2019-11-01 18:34:46 +01:00
2019-10-31 11:17:48 +01:00
int main(void) {
2019-11-09 17:46:05 +01:00
struct sockaddr_in bind_adr;
int socket_bind;
2019-11-05 18:54:11 +01:00
char msg[1024];
2019-11-09 22:39:59 +01:00
struct client_socket client[7];
2019-11-05 18:54:11 +01:00
fd_set Clients;
2019-11-09 22:39:59 +01:00
fd_set Socket;
2019-11-05 18:54:11 +01:00
socklen_t dl = sizeof(struct sockaddr_in);
int i;
int j;
2019-11-09 17:46:05 +01:00
char ff;
2019-11-09 22:39:59 +01:00
int max_fd;
2019-11-09 17:34:44 +01:00
//Inicjalizacja Tablicy Klientów
for(i=0; i<=4; i++)
2019-11-05 18:54:11 +01:00
{
2019-11-09 17:34:44 +01:00
client[i].socket = 0;
2019-11-05 18:54:11 +01:00
}
2019-11-09 22:39:59 +01:00
printf("Tablica Klientow\n");
2019-11-09 17:34:44 +01:00
//INICJALIZACJA SERWERA
//Bind
2019-11-05 18:54:11 +01:00
socket_bind = socket(AF_INET, SOCK_STREAM, 0);
if(socket_bind < 0) {
printf("Error: Socket\n");
return 1;
}
2019-11-09 22:39:59 +01:00
printf("Socket\n");
2019-11-09 17:34:44 +01:00
//Adres
2019-11-09 22:39:59 +01:00
bind_adr.sin_family = AF_INET;
bind_adr.sin_port = htons(44444);
bind_adr.sin_addr.s_addr = INADDR_ANY;
2019-11-09 17:46:05 +01:00
if (bind(socket_bind, (struct sockaddr*) &bind_adr, sizeof(bind_adr)) < 0)
2019-11-09 17:34:44 +01:00
{
2019-11-05 18:54:11 +01:00
printf("Error: Bind\n");
return 1;
}
2019-11-09 22:39:59 +01:00
printf("Bind\n");
2019-11-09 17:34:44 +01:00
//Listen
2019-11-05 18:54:11 +01:00
if (listen(socket_bind, 10) < 0) { //listen
printf("Error: Listen\n");
return 1;
}
2019-11-09 22:39:59 +01:00
printf("Listen\n");
2019-11-09 17:34:44 +01:00
//printf("Inicjalizacja Zakończona\n");
//NASŁUCHIWANIE KLIENTÓW
2019-11-05 18:54:11 +01:00
while (1) {
2019-11-09 22:39:59 +01:00
/*scanf("%c", &ff);
2019-11-09 17:46:05 +01:00
printf("\n");
if(ff == 'e') {
return 0;
}
2019-11-09 22:39:59 +01:00
ff = 'a';*/
printf("While\n");
2019-11-09 17:34:44 +01:00
//Aktualizacja deskryptorów
2019-11-05 18:54:11 +01:00
max_fd = 0;
2019-11-09 22:39:59 +01:00
FD_ZERO(&Clients);
FD_ZERO(&Socket);
FD_SET(socket_bind, &Socket);
2019-11-09 17:34:44 +01:00
//Dodanie deskryptorów Klientów
for(i =0; i<=4; i++){
2019-11-05 18:54:11 +01:00
if (client[i].socket > 0) {
2019-11-09 22:39:59 +01:00
FD_SET(client[i].socket, &Clients);
2019-11-05 18:54:11 +01:00
if(client[i].socket > max_fd) {
max_fd = client[i].socket;
}
}
}
2019-11-09 22:39:59 +01:00
printf("FD_SET\n");
2019-11-09 17:34:44 +01:00
//Sprawdzenie Poprawności
2019-11-09 22:39:59 +01:00
//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");
2019-11-09 17:34:44 +01:00
//Dodanie Klienta
2019-11-09 22:39:59 +01:00
//printf("Isset Przed\n");
if(FD_ISSET(socket_bind, &Socket)) {
printf("Isset Po\n");
2019-11-09 17:34:44 +01:00
for(i=0;i<=4;i++){
2019-11-05 18:54:11 +01:00
if (client[i].socket == 0){
2019-11-09 22:39:59 +01:00
//printf("Przed\n");
client[i].socket = accept(socket_bind, (struct sockaddr*) &client[i].accept_adr, &dl);
//printf("Po\n");
break;
2019-11-05 18:54:11 +01:00
} else {
2019-11-09 22:39:59 +01:00
printf("Server Pełen\n");
}
2019-11-05 18:54:11 +01:00
}
2019-11-09 17:34:44 +01:00
} else {
printf("Error: Socket_bind nie jest w zbiorze deskryptorów");
2019-11-05 18:54:11 +01:00
}
2019-11-09 22:39:59 +01:00
printf("Accept\n");
2019-11-09 17:34:44 +01:00
//PRZETWARZANIE WIADOMOŚCI
for(i = 0; i<=4; i++)
2019-11-05 18:54:11 +01:00
{
2019-11-09 22:39:59 +01:00
if(client[i].socket != 0 && FD_ISSET(client[i].socket, &Clients))
2019-11-05 18:54:11 +01:00
{
2019-11-09 17:46:05 +01:00
recv(client[i].socket, &msg, 1024, 0);
2019-11-09 22:39:59 +01:00
printf("%c\n", msg[0]);
printf("Widomość ^^^\n");
//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 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;
} else {
for(j=0;j<=4;j++) {
if(i!=j && client[j].socket!=0) {
send(client[j].socket, &msg, 1024, 0);
}
}
}
2019-11-05 18:54:11 +01:00
}
}
}
2019-11-09 22:39:59 +01:00
}