diff --git a/server/.gitignore b/server/.gitignore index 13cd1fa..00f0b29 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -1 +1,3 @@ -server \ No newline at end of file +server +send +a.out \ No newline at end of file diff --git a/server/example.jpg b/server/example.jpg new file mode 100644 index 0000000..e69de29 diff --git a/server/send.c b/server/send.c new file mode 100644 index 0000000..3eed123 --- /dev/null +++ b/server/send.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + char odbior[10]; + struct sockaddr_in endpoint; + int sdsocket, addrlen, sdconnection; + const int port = 7332; + const char ip[512] = "192.168.0.43"; + char buff[512]; + char eee[1]; + struct dirent **namelist; + int n; + char wiadomosc[512]; + char mess[512]; + + endpoint.sin_family = AF_INET; + endpoint.sin_port = htons(port); + endpoint.sin_addr.s_addr = inet_addr(ip); + addrlen = sizeof(struct sockaddr_in); + + while (1) + { + printf("wpisz 11 aby nawiazac polaczenie\nwpisz 22 aby moc napisac polecenie\n"); + scanf("%s", mess); + + if (strncmp("11", mess, 2) == 0) + { + printf("Realizowany jest connect\n"); + if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + printf("socket() nie powiodl sie (failed)\n"); + return 1; + } + + if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0) + { + printf("connect() nie powiodl sie (failed)\n"); + return 0; + } + bzero(odbior, 512); + recv(sdsocket, odbior, 10, 0); + printf("Wiadomosc tresc: %s\n", odbior); + } + else if (strncmp("22", mess, 2) == 0) + { + printf("Wpisz zapytanie do serwera\n"); + if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + printf("socket() nie powiodl sie (failed)\n"); + return 1; + } + + if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0) + { + printf("connect() nie powiodl sie (failed)\n"); + return 0; + } + + scanf("%s", buff); + send(sdsocket, buff, 512, 0); + + close(sdsocket); + + ////////////////////////////////// + + if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + printf("socket() nie powiodl sie (failed)\n"); + return 1; + } + + if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0) + { + printf("connect() nie powiodl sie (failed)\n"); + return 0; + } + bzero(odbior, 512); + recv(sdsocket, odbior, 512, 0); + printf("%s\n", odbior); + }else{ + if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + printf("socket() nie powiodl sie (failed)\n"); + return 1; + } + + if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0) + { + printf("connect() nie powiodl sie (failed)\n"); + return 0; + } + + scanf("%s", buff); + send(sdsocket, buff, 512, 0); + + close(sdsocket); + } + } + + return 0; +} diff --git a/server/server.c b/server/server.c index b46047c..7e9d16e 100644 --- a/server/server.c +++ b/server/server.c @@ -7,12 +7,17 @@ #include #include #include +#include int main(int argc, char **argv) { - struct sockaddr_in myaddr, endpoint; - int sdsocket, sdconnection, addrlen, received; const int port = 7332; + struct sockaddr_in myaddr, endpoint; + int sdsocket, sdconnection, addrlen, sdconnection2, n; + char odbior[512]; + struct dirent **namelist; + char name_file[512]; + char names_files[512]; sdsocket = socket(AF_INET, SOCK_STREAM, 0); addrlen = sizeof(struct sockaddr_in); @@ -38,11 +43,75 @@ int main(int argc, char **argv) (struct sockaddr *)&endpoint, &addrlen)) >= 0) { - + printf("Connected\n"); send(sdconnection, "CONNECT", 7, 0); + close(sdconnection); + break; } + while (1) + { + + while ((sdconnection = + accept(sdsocket, + (struct sockaddr *)&endpoint, + &addrlen)) >= 0) + { + bzero(odbior, 512); + recv(sdconnection, odbior, 512, 0); + + close(sdconnection); + + if (strncmp("GET_ALL_FILES", odbior, 13) == 0) + { + printf("Sending list of names files\n"); + n = scandir(".", &namelist, NULL, alphasort); + if (n == -1) + { + perror("scandir"); + exit(EXIT_FAILURE); + } + + while (n--) + { + sprintf(name_file, "%s\n", namelist[n]->d_name); + + strcat(names_files, name_file); + + free(namelist[n]); + } + free(namelist); + + while ((sdconnection2 = + accept(sdsocket, + (struct sockaddr *)&endpoint, + &addrlen)) >= 0) + { + + send(sdconnection2, names_files, 512, 0); + + close(sdconnection2); + break; + } + } + else + { + printf("Wrong query\n"); + while ((sdconnection2 = + accept(sdsocket, + (struct sockaddr *)&endpoint, + &addrlen)) >= 0) + { + + send(sdconnection2, "Wrong query", 512, 0); + + close(sdconnection2); + break; + } + } + } + } close(sdsocket); return 0; }