forked from s444341/Projekt_SIK
164 lines
4.7 KiB
C
164 lines
4.7 KiB
C
#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>
|
|
#include <sys/stat.h>
|
|
#include <stdlib.h>
|
|
|
|
struct client_socket
|
|
{
|
|
int socket;
|
|
struct sockaddr_in accept_adr;
|
|
//char name[20] = "";
|
|
};
|
|
|
|
int main(void) {
|
|
struct sockaddr_in bind_adr;
|
|
int socket_bind;
|
|
char msg[1024];
|
|
struct client_socket client[7];
|
|
fd_set Clients;
|
|
fd_set Socket;
|
|
socklen_t dl = sizeof(struct sockaddr_in);
|
|
int temp_accept;
|
|
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);
|
|
if(socket_bind < 0) {
|
|
printf("Error: Socket\n");
|
|
return 1;
|
|
}
|
|
printf("Socket\n");
|
|
//Adres
|
|
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) {
|
|
//Aktualizacja deskryptorów
|
|
max_fd = 0;
|
|
FD_ZERO(&Clients);
|
|
FD_ZERO(&Socket);
|
|
FD_SET(socket_bind, &Socket);
|
|
|
|
//Dodanie deskryptorów Klientów
|
|
|
|
//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);
|
|
if(client[i].socket > max_fd) {
|
|
max_fd = client[i].socket;
|
|
}
|
|
}
|
|
}
|
|
printf("Wiadomość:%s\n", msg);
|
|
//PRZETWARZANIE WIADOMOŚCI
|
|
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))
|
|
{
|
|
|
|
//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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}*/
|
|
}
|
|
}
|