1
0
Fork 0
Projekt_SIK/server/server.c

178 lines
5.2 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);
2019-11-09 23:30:02 +01:00
int temp_accept;
2019-11-05 18:54:11 +01:00
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-10 02:27:06 +01:00
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
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-10 02:27:06 +01:00
//printf("-------------\nKolejna Iteracja\n--------------\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);
2019-11-09 23:30:02 +01:00
FD_ZERO(&Socket);
2019-11-09 22:39:59 +01:00
FD_SET(socket_bind, &Socket);
2019-11-09 17:34:44 +01:00
//Dodanie deskryptorów Klientów
2019-11-09 23:30:02 +01:00
//Sprawdzenie Poprawności
//printf("Przed Select\n");
2019-11-10 02:27:06 +01:00
//printf("Rozpoczecie:Select 1\n");
if(select(socket_bind+1, &Socket, NULL, NULL, &timeout) > 0) {
2019-11-09 23:30:02 +01:00
if (FD_ISSET(socket_bind, &Socket)) {
2019-11-09 23:46:08 +01:00
temp_accept = accept(socket_bind, (struct sockaddr*) &client[i].accept_adr, &dl);
2019-11-10 02:27:06 +01:00
//printf("Temp Accept: %i\n", temp_accept
//printf("Sprawdzam Clienta %i: %i\n", i, client[i].socket);
2019-11-09 23:30:02 +01:00
for(i=0;i<=4;i++){
2019-11-10 02:27:06 +01:00
//printf("Miejsce %i: %i\n", i, client[i].socket);
2019-11-09 23:30:02 +01:00
if (client[i].socket == 0){
2019-11-10 02:27:06 +01:00
client[i].socket = temp_accept;
//recv(client[i].socket, msg, 1024, 0);
//printf("Wiadomosc: %s\n", msg);
2019-11-09 23:30:02 +01:00
break;
} else {
printf("Miejsce %i zajęte\n", i);
}
}
2019-11-10 02:27:06 +01:00
2019-11-09 23:30:02 +01:00
}
}
2019-11-10 02:27:06 +01:00
for(i=0;i<=4;i++) {
while(recv(client[i].socket, msg, 1024, 0)>0) {
printf("Wiadomosc: %s\n", msg);
}
}
//printf("Koniec:Select 1\n");
2019-11-09 23:30:02 +01:00
//Set Clients
2019-11-10 02:27:06 +01:00
//printf("Rozpoczęcie: Set Clients\n");
2019-11-09 23:30:02 +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
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-10 02:27:06 +01:00
//printf("Koniec: Set Clients\n");
// printf("Wiadomość:%s\n", msg);
2019-11-09 17:34:44 +01:00
//PRZETWARZANIE WIADOMOŚCI
2019-11-10 02:27:06 +01:00
//printf("Rozpoczecie:Select 2\n");
if(select(max_fd+1, NULL, &Clients, NULL, &timeout) > 0) {
2019-11-09 23:30:02 +01:00
for(i=0;i<=4;i++){
if (FD_ISSET(client[i].socket, &Clients)) {
for(j=0;j<=4;j++) {
2019-11-10 02:27:06 +01:00
if(client[j].socket!=0) {
2019-11-09 23:57:04 +01:00
send(client[j].socket, msg, 1024, 0);
2019-11-09 23:30:02 +01:00
}
}
}
}
}
2019-11-10 02:27:06 +01:00
//printf("Koniec:Select 2\n");
2019-11-09 23:30:02 +01:00
/*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 23:30:02 +01:00
2019-11-09 22:39:59 +01:00
//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 23:30:02 +01:00
}*/
2019-11-05 18:54:11 +01:00
}
2019-11-09 22:39:59 +01:00
}