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-01 18:34:46 +01:00
|
|
|
//Połączenia z wieloma klientami
|
|
|
|
void ObsluzPolaczenie(int gn)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
char Msg[1024];
|
|
|
|
char Sciezka[1024];
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
//recv() <- czeka na wiadomość
|
2019-11-02 23:04:47 +01:00
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(gniazdoTCP, &readfds);
|
|
|
|
timeout.tv_sec = 1;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
gniazdo_src = socket();
|
|
|
|
int gniazdo_kli;
|
|
|
|
if (select(gniazdo+1, &readfds, NULL, NULL, &timeout) > 0)
|
|
|
|
{
|
|
|
|
gniazdo_kli = accept(gniazdo_src, (struct sockaddr*) adres, &dladr);
|
|
|
|
}
|
2019-11-01 18:34:46 +01:00
|
|
|
memset(Msg, 0, 1024);
|
|
|
|
recv(Gniazdo_Klient, Msg, 1024, 0);
|
|
|
|
|
|
|
|
//Porównywanie wiadomości
|
2019-11-02 23:04:47 +01:00
|
|
|
If (Msg == "wyślij") {
|
|
|
|
|
2019-11-01 18:34:46 +01:00
|
|
|
} else (Msg = "pobierz") {
|
|
|
|
|
|
|
|
} else (Msg == "exit") {
|
|
|
|
close(Gniazdo_Klient);
|
|
|
|
close(Gniazdo_Server);
|
|
|
|
} else {
|
|
|
|
//wyślij do wszystkich wiadomość
|
|
|
|
//(opconalne)nadpisz plik z historią rozmów - plik tekstowy
|
|
|
|
for(each gniazdo in readfs [select]){
|
|
|
|
send(gniazdo,wiadomość, );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
//Main Server
|
|
|
|
|
2019-10-31 11:17:48 +01:00
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
char bufor[1024]; //wiadomosc
|
|
|
|
int gniazdo, gniazdo2;
|
|
|
|
struct sockaddr_in adr, nadawca;
|
|
|
|
socklen_t dl = sizeof(struct sockaddr_in);
|
|
|
|
|
|
|
|
gniazdo = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
adr.sin_family = AF_INET;
|
|
|
|
adr.sin_port = htons(44444); //port
|
|
|
|
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);
|
|
|
|
recv(gniazdo2, bufor, 1024, 0);
|
|
|
|
printf("Wiadomosc od %s: %s\n", inet_ntoa(nadawca.sin_addr), bufor);
|
|
|
|
close(gniazdo2);
|
|
|
|
}
|
2019-11-01 18:34:46 +01:00
|
|
|
//Połączenie z wieloma Klientami
|
|
|
|
/*
|
2019-11-02 23:04:47 +01:00
|
|
|
struct sockaddr_in adr_serv;
|
|
|
|
int gt, port;
|
|
|
|
|
|
|
|
printf("Numer portu: ");
|
|
|
|
scanf("%d", &port);
|
|
|
|
|
|
|
|
gt = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
adr_serv.sin_family = AF_INET;
|
|
|
|
adr_serv.sin_port = htons(port);
|
|
|
|
adr_serv.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
if (bind(gt,(struct sockaddr*) &adr_serv,
|
|
|
|
sizeof(struct sockaddr_in)) < 0)
|
|
|
|
{
|
|
|
|
printf("Bind nie powiodl sie.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (listen(gt, 10) < 0)
|
|
|
|
{
|
|
|
|
printf("Listen nie powiodl sie.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ObsluzObaProtokoly(gt, &adr_serv);
|
|
|
|
fd_set readfds;
|
|
|
|
struct timeval timeout;
|
|
|
|
unsigned long proba;
|
|
|
|
proba = 0;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(gniazdoTCP, &readfds);
|
|
|
|
timeout.tv_sec = 1;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
if (select(gniazdoTCP+1, &readfds, NULL, NULL, &timeout) > 0)
|
|
|
|
{
|
|
|
|
proba = 0;
|
|
|
|
if (FD_ISSET(gniazdoTCP, &readfds))
|
|
|
|
ObsluzTCP(gniazdoTCP, adres);
|
|
|
|
int nowe_gniazdo;
|
|
|
|
char bufor[1024];
|
|
|
|
socklen_t dladr = sizeof(struct sockaddr_in);
|
|
|
|
nowe_gniazdo =
|
|
|
|
accept(gniazdo, (struct sockaddr*) adres,
|
|
|
|
&dladr);
|
|
|
|
if (nowe_gniazdo < 0)
|
|
|
|
{
|
|
|
|
printf("Bledne polaczenie (accept < 0)\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memset(bufor, 0, 1024);
|
|
|
|
while (recv(nowe_gniazdo, bufor, 1024, 0) <= 0);
|
|
|
|
DrukujNadawce(adres);
|
|
|
|
printf("[TCP]: %s\n", bufor);
|
|
|
|
close(nowe_gniazdo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
proba++;
|
|
|
|
printf("Czekam %lu sekund i nic ...\n", proba);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2019-11-01 18:34:46 +01:00
|
|
|
*/
|
|
|
|
//Polączenie z wieloma klientami
|
|
|
|
|
|
|
|
|
2019-10-31 11:17:48 +01:00
|
|
|
close(gniazdo);
|
|
|
|
return 0;
|
|
|
|
}
|