diff --git a/server/send.c b/server/send.c index 7ae6862..c454b08 100644 --- a/server/send.c +++ b/server/send.c @@ -9,19 +9,23 @@ #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"; + const char ip[512] = "192.168.1.30"; char buff[512]; char eee[1]; struct dirent **namelist; int n; char wiadomosc[512]; char mess[512]; + char name_file[512]; endpoint.sin_family = AF_INET; endpoint.sin_port = htons(port); @@ -73,6 +77,48 @@ int main(int argc, char **argv) return 0; } + if (strncmp("FTP", buff, 3) == 0) + { + ssize_t total = 0; + strncpy(name_file, buff + 4, 509); + printf("dlugość nazwy:%ld\n", strlen(name_file)); + printf("Plik do wyslania :%s\n", name_file); + + FILE *file = fopen(name_file, "wb"); + if (file == NULL) + { + printf("Nie udalo sie otowrzyc pliku"); + + } + + char addr[INET_ADDRSTRLEN]; + printf("Pobieranie pliku: %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 ((n = recv(sdsocket, buff, 4096, 0)) > 0) + { + total += n; + if (n == -1) + { + printf("recv Error"); + } + + if (fwrite(buff, sizeof(char), n, file) != n) + { + printf("fwrite Error"); + + } + memset(buff, 0, 4096); + } + + printf("Przesylanie udane liczba bajtow = %ld\n", total); + + fclose(file); + + goto start; + } + bzero(odbior, 512); recv(sdsocket, odbior, 512, 0); printf("%s\n", odbior); diff --git a/server/server.c b/server/server.c index 63fa6ce..6b6b426 100644 --- a/server/server.c +++ b/server/server.c @@ -9,6 +9,8 @@ #include #include +ssize_t count = 0; + int main(int argc, char **argv) { const int port = 7332; @@ -38,18 +40,6 @@ int main(int argc, char **argv) return 1; } - /*while ((sdconnection = - accept(sdsocket, - (struct sockaddr *)&endpoint, - &addrlen)) >= 0) - { - printf("Connected\n"); - send(sdconnection, "CONNECT", 7, 0); - - close(sdconnection); - break; - }*/ - while (1) { @@ -60,18 +50,14 @@ int main(int argc, char **argv) { printf("CONNECT FROM : %s\n ", inet_ntoa(endpoint.sin_addr)); - - - bzero(odbior, 512); rec = recv(sdconnection, odbior, 512, 0); - close(sdconnection); if (strncmp("GET_ALL_FILES", odbior, 13) == 0) { - + printf("Sending list of names files\n"); bzero(names_files, 512); bzero(name_file, 512); @@ -100,8 +86,7 @@ int main(int argc, char **argv) { send(sdconnection2, names_files, 512, 0); - - + close(sdconnection2); break; } @@ -109,37 +94,51 @@ int main(int argc, char **argv) else if (strncmp("EXIT", odbior, 4) == 0) { printf("Server shut down..\n"); - - - close(sdsocket); - - return 0; - - + close(sdsocket); + + return 0; } else if (strncmp("FTP", odbior, 3) == 0) { - - bzero(name_file, 512); - - strncpy (name_file, odbior+4, 509); + bzero(name_file, 512); + + strncpy(name_file, odbior + 4, 509); + printf("dlugość nazwy:%ld\n", strlen(name_file)); printf("Plik do wyslania :%s\n", name_file); - + + FILE *file = fopen(name_file, "rb"); + if (file == NULL) + { + printf("Open file failed"); + } + while ((sdconnection2 = accept(sdsocket, (struct sockaddr *)&endpoint, &addrlen)) >= 0) { - send(sdconnection2, name_file, 512, 0); - - + int x; + char package[4096] = {0}; + while ((x = fread(package, sizeof(char), 4096, file)) > 0) + { + count += x; + + if (send(sdconnection2, package, x, 0) == -1) + { + printf("Send failed"); + } + memset(package, 0, 4096); + } + + printf("Send Success, NumBytes = %ld\n", count); + fclose(file); + close(sdconnection2); break; } - } else {