From 2d79d2a265944f05502379994f910ab2937ba89e Mon Sep 17 00:00:00 2001 From: s449273 Date: Mon, 9 Dec 2019 14:28:52 +0100 Subject: [PATCH] introduce wersion v2 single connect --- server/.gitignore | 4 +- server/example.jpg | 0 server/send.c | 2 - server/sendv2.c | 153 +++++++++++++++++++++++++++++++++++++ server/server.c | 4 +- server/serverv2.c | 184 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 341 insertions(+), 6 deletions(-) delete mode 100644 server/example.jpg create mode 100644 server/sendv2.c create mode 100644 server/serverv2.c diff --git a/server/.gitignore b/server/.gitignore index 00f0b29..37c722f 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -1,3 +1,5 @@ server send -a.out \ No newline at end of file +a.out +serverv2 +sendv2 \ No newline at end of file diff --git a/server/example.jpg b/server/example.jpg deleted file mode 100644 index e69de29..0000000 diff --git a/server/send.c b/server/send.c index e64c816..40ed371 100644 --- a/server/send.c +++ b/server/send.c @@ -137,8 +137,6 @@ int main(int argc, char **argv) fclose(file); close(sdsocket); - - goto start; } diff --git a/server/sendv2.c b/server/sendv2.c new file mode 100644 index 0000000..f9f98b9 --- /dev/null +++ b/server/sendv2.c @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + const int port = 7332; + const char ip[512] = "192.168.0.43"; + struct sockaddr_in endpoint; + struct dirent **namelist; + char buff[512]; + char name_file[512]; + int sdsocket, addrlen, n, x, lenbyt; + + endpoint.sin_family = AF_INET; + endpoint.sin_port = htons(port); + endpoint.sin_addr.s_addr = inet_addr(ip); + addrlen = sizeof(struct sockaddr_in); + ssize_t count = 0; + + while (1) + { + start: + printf("Enter the server query\n"); + bzero(buff, 512); + scanf("%s", buff); + + if (strncmp("CLIENTOFF", buff, 9) == 0) + { + close(sdsocket); + return 0; + } + else if (strncmp("HELP", buff, 4) == 0) + { + printf("COMMAND LIST:\n CLIENTOFF\n EXIT\n GET_ALL_FILES\n FTP_namefile\n SEND_namefile\n"); + goto start; + } + + if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + printf("socket() failed\n"); + return 1; + } + if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0) + { + printf("connect() failed\n"); + return 0; + } + else + printf("CONNECTED\n"); + + send(sdsocket, buff, 512, 0); + + if (strncmp("FTP", buff, 3) == 0) + { + ssize_t total = 0; + strncpy(name_file, buff + 4, 509); + printf("Name of file :%s\n", name_file); + + FILE *file = fopen(name_file, "wb"); + if (file == NULL) + { + printf("fopen() failed"); + } + + char length[512]; + recv(sdsocket, length, 512, 0); + printf("Rozmiar pliku to: %s\n", length); + lenbyt = atoi(length); + printf("Rozmiar pliku w intach to: %d\n", lenbyt); + char addr[INET_ADDRSTRLEN]; + printf("Downloading file: %s od %s\n", name_file, inet_ntop(AF_INET, &endpoint.sin_addr.s_addr, addr, INET_ADDRSTRLEN)); + + ssize_t n; + char buff[4096] = {0}; + while (total < lenbyt) + { + n = recv(sdsocket, buff, 4096, 0); + total += n; + if (n == -1) + { + printf("recv() failed"); + } + + if (fwrite(buff, sizeof(char), n, file) != n) + { + printf("fwrite() failed"); + } + memset(buff, 0, 4096); + } + + printf("Downloading succes, numer of bytes = %ld\n", total); + + fclose(file); + + goto start; + } + if (strncmp("SEND", buff, 4) == 0) + { + ssize_t count = 0; + bzero(name_file, 512); + strncpy(name_file, buff + 5, 507); + printf("File to send :%s\n", name_file); + + FILE *file = fopen(name_file, "rb"); + if (file == NULL) + { + printf("fopen() failed"); + } + + fseek(file, 0L, SEEK_END); + + long int res = ftell(file); + printf("Rozmiar pliku to: %ld\n", res); + char length[512]; + sprintf(length, "%ld", res); + send(sdsocket, length, 512, 0); + + rewind(file); + + char package[4096] = {0}; + while ((x = fread(package, sizeof(char), 4096, file)) > 0) + { + count += x; + + if (send(sdsocket, package, x, 0) == -1) + { + printf("send() failed"); + } + memset(package, 0, 4096); + } + + printf("Send success, number of bytes: %ld\n", count); + + fclose(file); + + goto start; + } + + bzero(buff, 512); + recv(sdsocket, buff, 512, 0); + printf("%s\n", buff); + } + + return 0; +} diff --git a/server/server.c b/server/server.c index f8873f3..cac2629 100644 --- a/server/server.c +++ b/server/server.c @@ -115,9 +115,7 @@ int main(int argc, char **argv) &addrlen)) >= 0) { - - - while ((n = recv(sdconnection2, buff, 4096, 0)) > 0) + while ((n = recv(sdconnection2, buff, 4096, 0)) > 0) { total += n; if (n == -1) diff --git a/server/serverv2.c b/server/serverv2.c new file mode 100644 index 0000000..d364a63 --- /dev/null +++ b/server/serverv2.c @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + + const int port = 7332; + struct sockaddr_in myaddr, endpoint; + struct dirent **namelist; + char buff[512]; + char name_file[512]; + char names_files[512]; + int sdsocket, sdconnection, addrlen, n, x, lenbyt; + + sdsocket = socket(AF_INET, SOCK_STREAM, 0); + addrlen = sizeof(struct sockaddr_in); + + myaddr.sin_family = AF_INET; + myaddr.sin_port = htons(port); + myaddr.sin_addr.s_addr = htonl(INADDR_ANY); + + if (bind(sdsocket, (struct sockaddr *)&myaddr, addrlen) < 0) + { + printf("bind() failed\n"); + return 1; + } + + if (listen(sdsocket, 10) < 0) + { + printf("listen() failed\n"); + return 1; + } + + while (1) + { + start: + while ((sdconnection = + accept(sdsocket, + (struct sockaddr *)&endpoint, + &addrlen)) >= 0) + { + + printf("CONNECT FROM : %s\n ", inet_ntoa(endpoint.sin_addr)); + + bzero(buff, 512); + recv(sdconnection, buff, 512, 0); + + if (strncmp("GET_ALL_FILES", buff, 13) == 0) + { + + printf("Sending list of names files\n"); + bzero(names_files, 512); + bzero(name_file, 512); + + 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); + + send(sdconnection, names_files, 512, 0); + goto start; + } + else if (strncmp("SEND", buff, 4) == 0) + { + ssize_t total = 0; + strncpy(name_file, buff + 5, 508); + printf("Name of file :%s\n", name_file); + + printf("I'm receving a file: %s\n", buff); + + FILE *file = fopen(name_file, "wb"); + if (file == NULL) + { + printf("fopen() failed"); + } + + char length[512]; + recv(sdconnection, length, 512, 0); + printf("Rozmiar pliku to: %s\n", length); + lenbyt = atoi(length); + printf("Rozmiar pliku w intach to: %d\n", lenbyt); + + char addr[INET_ADDRSTRLEN]; + printf("Downloading file: %s od %s\n", name_file, inet_ntop(AF_INET, &endpoint.sin_addr.s_addr, addr, INET_ADDRSTRLEN)); + + ssize_t n; + + char buff[4096] = {0}; + + while (total < lenbyt) + { + n = recv(sdconnection, buff, 4096, 0); + total += n; + if (n == -1) + { + printf("recv() failed"); + } + + if (fwrite(buff, sizeof(char), n, file) != n) + { + printf("fwrite() failed"); + } + memset(buff, 0, 4096); + } + printf("Downloading succes, numer of bytes = %ld\n", total); + + fclose(file); + } + else if (strncmp("EXIT", buff, 4) == 0) + { + printf("Server shut down..\n"); + + close(sdsocket); + return 0; + } + else if (strncmp("FTP", buff, 3) == 0) + { + ssize_t count = 0; + bzero(name_file, 512); + strncpy(name_file, buff + 4, 509); + printf("File to send :%s\n", name_file); + + FILE *file = fopen(name_file, "rb"); + if (file == NULL) + { + printf("fopen() failed"); + } + fseek(file, 0L, SEEK_END); + + long int res = ftell(file); + printf("Rozmiar pliku to: %ld\n", res); + char length[512]; + sprintf(length, "%ld", res); + send(sdconnection, length, 512, 0); + + rewind(file); + + char package[4096] = {0}; + while ((x = fread(package, sizeof(char), 4096, file)) > 0) + { + count += x; + + if (send(sdconnection, package, x, 0) == -1) + { + printf("send() failed"); + } + memset(package, 0, 4096); + } + + printf("Send success, number of bytes: %ld\n", count); + + fclose(file); + } + else + { + printf("Wrong query: %s\n", buff); + + send(sdsocket, "Wrong query", 512, 0); + goto start; + } + } + } + close(sdsocket); + return 0; +} \ No newline at end of file