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-10 13:45:49 +01:00
|
|
|
struct clients{
|
|
|
|
char nick[30];
|
|
|
|
char ip[30];
|
|
|
|
unsigned short port;
|
|
|
|
};
|
|
|
|
|
|
|
|
int finduser(struct clients cli[], char name[]){ //zwraca na ktorym miejscu jest user, -1 jesli go nie ma
|
|
|
|
for (int i = 0; i < 10; i++){
|
|
|
|
if (cli[i].nick[0] != '\0'){ //jesli cos juz zapisalismy
|
|
|
|
int ok = 1;
|
|
|
|
int j = 0;
|
|
|
|
while (cli[i].nick[j] != '\0'){ //dopoki nie ma konca
|
|
|
|
if (cli[i].nick[j] != name[j]){ //jesli sa rozne to przerywamy
|
|
|
|
ok = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
if (ok){ //znalezlismy
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-31 11:17:48 +01:00
|
|
|
int main(void) {
|
|
|
|
char bufor[1024]; //wiadomosc
|
2019-11-10 13:45:49 +01:00
|
|
|
int gniazdo, gniazdo2, gniazdo3; //gniazda
|
|
|
|
struct sockaddr_in adr, nadawca, odbiorca; //nasz adres i adres do ktorego przesylamy wiadomosc
|
|
|
|
struct clients cli[10]; //maksymalnie 10 klientow
|
|
|
|
for(int i = 0; i < 10; i++){ //czyscimy zmienne w klientach
|
|
|
|
cli[i].port = 0;
|
|
|
|
for(int j = 0; j < 30; j++){
|
|
|
|
cli[i].nick[j] = '\0';
|
|
|
|
cli[i].ip[j] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 11:17:48 +01:00
|
|
|
socklen_t dl = sizeof(struct sockaddr_in);
|
|
|
|
|
2019-11-10 13:45:49 +01:00
|
|
|
//nasze dane
|
2019-10-31 11:17:48 +01:00
|
|
|
gniazdo = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
adr.sin_family = AF_INET;
|
2019-11-10 13:45:49 +01:00
|
|
|
adr.sin_port = htons(44445); //port
|
2019-10-31 11:17:48 +01:00
|
|
|
adr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
printf("Slucham na %s:%d\n", inet_ntoa(adr.sin_addr), ntohs(adr.sin_port));
|
|
|
|
|
|
|
|
if (bind(gniazdo, (struct sockaddr*) &adr, sizeof(adr)) < 0) { //bind
|
|
|
|
printf("Bind nie powiodl sie.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (listen(gniazdo, 10) < 0) { //listen
|
|
|
|
printf("Listen nie powiodl sie.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("Czekam na polaczenie ...\n");
|
|
|
|
while ((gniazdo2 = accept(gniazdo, (struct sockaddr*) &nadawca, &dl)) > 0) { //odbieranie
|
|
|
|
memset(bufor, 0, 1024);
|
2019-11-10 13:45:49 +01:00
|
|
|
recv(gniazdo2, bufor, 1024, 0); //odbieramy
|
|
|
|
|
|
|
|
//pobieramy dane uzytkownika
|
|
|
|
char name[30];
|
|
|
|
char ip[30];
|
|
|
|
unsigned short port = 0;
|
|
|
|
for (int i = 0; i < 30; i++){ //czyscimy
|
|
|
|
name[i] = '\0';
|
|
|
|
ip[i] = '\0';
|
|
|
|
}
|
|
|
|
int i = 0;
|
|
|
|
while (bufor[i] != '>'){ //> to jest nasz znak specjalny ktory konczy nazwe uzytkownika
|
|
|
|
name[i] = bufor[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
char *temp = inet_ntoa(nadawca.sin_addr);
|
|
|
|
size_t len = strlen(temp);
|
|
|
|
for (int i = 0; i < len; i++){
|
|
|
|
ip[i] = temp[i];
|
|
|
|
}
|
|
|
|
port = nadawca.sin_port;
|
|
|
|
printf("Wiadomosc od %s(%s:%i): %s\n", name, ip, port, bufor);
|
|
|
|
|
|
|
|
//dodajemy go do listy
|
|
|
|
i = 0;
|
|
|
|
if (finduser(cli, name) == -1){ //jesli go jeszcze nie ma to doadajemy
|
|
|
|
while(1){
|
|
|
|
if (i >= 10){
|
|
|
|
printf("NIE MA DLA CIEBIE MIEJSCA\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(cli[i].nick[0] == '\0'){ //mamy wolne miejsce
|
|
|
|
cli[i].port = port;
|
|
|
|
for(int j = 0; j < 30; j++){
|
|
|
|
cli[i].nick[j] = name[j];
|
|
|
|
cli[i].ip[j] = ip[j];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Znajdujemy do kogo przeslac wiadomosc
|
|
|
|
char nametosend[30];
|
|
|
|
for (int j = 0; j < 30; j++){ //czyscimy
|
|
|
|
nametosend[j] = '\0';
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
while(bufor[i] != '<'){
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
int j = 0;
|
|
|
|
i++;
|
|
|
|
while(bufor[i] != '\0'){
|
|
|
|
nametosend[j] = bufor[i];
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
printf("Wiadomosc do %s \n", nametosend);
|
|
|
|
|
|
|
|
//wydobywamy informacje z listy i wysylamy
|
|
|
|
int index = finduser(cli, nametosend);
|
|
|
|
|
|
|
|
gniazdo3 = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
odbiorca.sin_family = AF_INET;
|
|
|
|
odbiorca.sin_port = htons(cli[index].port);
|
|
|
|
odbiorca.sin_addr.s_addr = inet_addr(cli[index].ip);
|
|
|
|
if (connect(gniazdo3, (struct sockaddr*) &odbiorca, sizeof(odbiorca)) < 0) {
|
|
|
|
printf("Nawiazanie polaczenia nie powiodlo sie.\n");
|
|
|
|
}
|
|
|
|
//GNIAZDO 2 TO WYSYLANIE WIAD TAM SKAD DOSTALISMY GNIAZDO 3 JAK BEDZIEMY MIELI 3 KOMPUTERY
|
|
|
|
//JAK USTAWIMY GNIAZDO 2 TO WYRZUCI BLAD ALE PRZESLE
|
|
|
|
send(gniazdo2, bufor, strlen(bufor), 0);
|
|
|
|
|
|
|
|
close(gniazdo3);
|
2019-10-31 11:17:48 +01:00
|
|
|
close(gniazdo2);
|
|
|
|
}
|
|
|
|
close(gniazdo);
|
|
|
|
return 0;
|
|
|
|
}
|