commit 76bb7c79e27f1f31445c85ad5898b022198b69d9 Author: Michał Jankowski Date: Sat Dec 14 10:01:22 2019 +0100 Please enter the commit message for your changes. Lines starting with '#' will be ignored, and an empty message aborts the commit. Committer: Michał Jankowski On branch master Initial commit Changes to be committed: new file: filestorage-client/.gitignore new file: filestorage-client/CMakeLists.txt new file: filestorage-client/Makefile new file: filestorage-client/README.md new file: filestorage-client/main.c new file: filestorage-client/panobrazek.jpg new file: filestorage-server/.gitignore new file: filestorage-server/README.md new file: filestorage-server/src/Connection.java new file: filestorage-server/src/Server.java new file: filestorage-server/storage/basquiat.jpg diff --git a/filestorage-client/.gitignore b/filestorage-client/.gitignore new file mode 100644 index 0000000..3d79ab5 --- /dev/null +++ b/filestorage-client/.gitignore @@ -0,0 +1,3 @@ +.idea +cmake-build-debug +client \ No newline at end of file diff --git a/filestorage-client/CMakeLists.txt b/filestorage-client/CMakeLists.txt new file mode 100644 index 0000000..d98ddb2 --- /dev/null +++ b/filestorage-client/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.13) +project(untitled C) + +set(CMAKE_C_STANDARD 99) + +add_executable(untitled main.c) \ No newline at end of file diff --git a/filestorage-client/Makefile b/filestorage-client/Makefile new file mode 100644 index 0000000..bff3beb --- /dev/null +++ b/filestorage-client/Makefile @@ -0,0 +1,2 @@ +install: + gcc -o client main.c \ No newline at end of file diff --git a/filestorage-client/README.md b/filestorage-client/README.md new file mode 100644 index 0000000..2aa9f41 --- /dev/null +++ b/filestorage-client/README.md @@ -0,0 +1,15 @@ +# FileStorage-Client + +Instalacja rewelacja klienta + +## Installation + +```bash +make install +``` + +## Usage + +```bash +./client +``` diff --git a/filestorage-client/main.c b/filestorage-client/main.c new file mode 100644 index 0000000..40a588c --- /dev/null +++ b/filestorage-client/main.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFFER_SIZE 1024 + +int main(int argc, char *argv[]) { + + char ipAddress[512]; + char buffer[BUFFER_SIZE]; + char fileName[255]; + uint32_t buffer_size; + struct sockaddr_in adr; + FILE *file; + unsigned int port; + int gniazdo; + long fileSize; + ssize_t bytesRead; + size_t nread; + + if (argc >= 2) { + strcpy(ipAddress, argv[1]); + port = atoi(argv[2]); + } else { + printf("Przyklad: ./client "); + exit(1); + } + + gniazdo = socket(PF_INET, SOCK_STREAM, 0); + adr.sin_family = AF_INET; + adr.sin_port = htons(port); + adr.sin_addr.s_addr = inet_addr(ipAddress); + + if (connect(gniazdo, (struct sockaddr *) &adr, + sizeof(adr)) < 0) { + printf("You did smth wrong, nie moge sie polaczyc!\n"); + return 1; + } + + printf("Czesc! Ponizej masz liste dostpenych komend:\n" + "/ls - lista plikow\n" + "/rm - usun plik\n" + "/send - wyslij plik na dana sciezke\n" + "/exit - wyjdz\n" ); + + fflush(stdout); + + while (1) { + printf("Wpisz komende: "); + memset(buffer, 0, BUFFER_SIZE*sizeof(char)); + + //wczytywanie + fgets(buffer, 1024, stdin); + + //wysylka na serwer + buffer_size = htonl(BUFFER_SIZE); + write(gniazdo, &buffer_size, sizeof(buffer_size)); + send(gniazdo, buffer, BUFFER_SIZE, 0); + + if (strncmp(buffer, "/exit", 5) == 0) { + break; + } + + if (strncmp(buffer, "/send", 5) == 0) { + strncpy(fileName, buffer+6, 255); + strtok(fileName, "\n"); + printf("Wysylam! '%s'\n", fileName); + file = fopen(fileName, "r"); + if (file) { + // rozmiar pliku + fseek(file, 0 , SEEK_END); + fileSize = ftell(file); + fseek(file, 0 , SEEK_SET);// sluzy do pobierania poczatku pliku + printf("It has %ld bytes.\n", fileSize); + + buffer_size = 0; + buffer_size = htonl((uint32_t)fileSize); + + write(gniazdo, &buffer_size, sizeof(buffer_size)); + + memset(buffer, 0, BUFFER_SIZE*sizeof(char)); + int progress = 0; + while ((nread = fread(buffer, 1, sizeof buffer, file)) > 0) { + progress += nread; + printf("\rSending %d/%ld",progress, fileSize); + send(gniazdo, buffer, nread, 0); + } + printf("\n"); + if (ferror(file)) { + printf("Error with file!"); + close(gniazdo); + exit(1); + } + fclose(file); + } + } + + // odczytaj odpowiedz + printf("Odpowiedz: \n"); + memset(buffer, 0, BUFFER_SIZE*sizeof(char)); + buffer_size = 0; + recv(gniazdo, &buffer_size, 4, 0); + buffer_size = ntohl(buffer_size); + + while (buffer_size > 0) { + bytesRead = recv(gniazdo, buffer, BUFFER_SIZE, 0); + if (-1 == bytesRead) { + printf("Blad podczas wczytywania z gniazda"); + break; + } + printf("%s", buffer); + buffer_size -= bytesRead; + } + printf("\n"); + } + close(gniazdo); + return 0; +} diff --git a/filestorage-client/panobrazek.jpg b/filestorage-client/panobrazek.jpg new file mode 100644 index 0000000..40108d2 Binary files /dev/null and b/filestorage-client/panobrazek.jpg differ diff --git a/filestorage-server/.gitignore b/filestorage-server/.gitignore new file mode 100644 index 0000000..bf1985c --- /dev/null +++ b/filestorage-server/.gitignore @@ -0,0 +1,3 @@ +.idea +FileStorageServer.iml +*/**/*.class \ No newline at end of file diff --git a/filestorage-server/README.md b/filestorage-server/README.md new file mode 100644 index 0000000..32eaefb --- /dev/null +++ b/filestorage-server/README.md @@ -0,0 +1,9 @@ +# FileStorage-Server + +Szybki guide jak odpalić Serwer + +``` +cd src +javac Server.java +java Server 8001 +``` diff --git a/filestorage-server/src/Connection.java b/filestorage-server/src/Connection.java new file mode 100644 index 0000000..29c6ca9 --- /dev/null +++ b/filestorage-server/src/Connection.java @@ -0,0 +1,121 @@ +import java.io.*; +import java.nio.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.*; +import java.net.*; + +public class Connection implements Runnable { + + private DataInputStream dis; + private DataOutputStream dos; + private Socket socket; + + // konstruktor + public Connection(Socket socket) throws IOException { + this.dis = new DataInputStream(socket.getInputStream()); + this.dos = new DataOutputStream(socket.getOutputStream()); + this.socket = socket; + } + + @Override + public void run() { + StringBuilder tc; + + while (true) { + try { + String outputMessage = "OK."; + String filename; + String received = ""; + int bytesRead = 0; + boolean end = false; + byte[] messageByte = new byte[1024]; + + // wczytywanie wejscia + int bytesToRead = this.dis.readInt(); + while (!end) { + bytesRead = this.dis.read(messageByte); + received += new String(messageByte, 0, bytesRead); + + if (received.length() == bytesToRead) { + end = true; + } + } + + // log + System.out.println(socket + " Input: " + received); + + if (received.startsWith("/exit")) { + this.socket.close(); + Server.connections.removeElement(this); + break; + } + + if (received.startsWith("/send")) { + filename = Server.storageDir + "/" + received.substring(6).trim(); + Path file = Paths.get(filename); + bytesToRead = this.dis.readInt(); + System.out.println(this.socket + " wysyla plik "+filename+". Rozmiar: "+bytesToRead+" bajtow. "); + + boolean first = true; + while (bytesToRead > 0) { + bytesRead = this.dis.read(messageByte); + if (first) { + first = false; + Files.write(file,Arrays.copyOf(messageByte, bytesRead)); + } else { + Files.write(file, Arrays.copyOf(messageByte, bytesRead), StandardOpenOption.APPEND); + } + bytesToRead -= bytesRead; + } + System.out.println(this.socket + " Zapisano plik."); + outputMessage = "Zapisano plik!."; + } + + if (received.startsWith("/ls")) { + File folder = new File(Server.storageDir); + File[] listOfFiles = folder.listFiles(); + tc = new StringBuilder(); + tc.append(String.format("Znaleziono %s plikow\n", listOfFiles.length)); + for (File file : listOfFiles) { + if (file.isFile()) { + tc.append(file.getName()).append("\n"); + } + } + outputMessage = tc.toString(); + } + + if (received.startsWith("/rm")) { + filename = received.substring(4).trim(); + File file = new File(Server.storageDir + "/" + filename); + + if (file.delete()) { + outputMessage = String.format("Usunieto plik %s ", filename); + } else { + outputMessage = String.format("Blad przy usunieciu pliku %s", filename); + } + } + + this.dos.write(ByteBuffer.allocate(4).putInt(outputMessage.length()).array()); + this.dos.write(outputMessage.getBytes()); + + } catch (IOException e) { + e.printStackTrace(); + + break; + } + + } + + try { + // closing resources + this.dis.close(); + this.dos.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/filestorage-server/src/Server.java b/filestorage-server/src/Server.java new file mode 100644 index 0000000..f3c6382 --- /dev/null +++ b/filestorage-server/src/Server.java @@ -0,0 +1,44 @@ + +import java.io.*; +import java.util.*; +import java.net.*; + +// Server class +public class Server { + + final public static String storageDir = System.getProperty("user.dir") + "/../storage"; + + public static Vector connections = new Vector<>(); + + public static void main(String[] args) throws IOException { + + int port; + + if (args.length > 0) { + port = Integer.parseInt(args[0]); + } else { + System.out.println("Uzywam: java Server "); + return; + } + + ServerSocket serverSocket = new ServerSocket(port); + System.out.println("Start serwera na porcie "+port); + + Socket socket; + + while (true) { + // Akceptowanie nadchodzacego zapytania + socket = serverSocket.accept(); + + System.out.println("Nowe polaczenie: " + socket); + + Connection connection = new Connection(socket); + + Thread thread = new Thread(connection); + + connections.add(connection); + + thread.start(); + } + } +} diff --git a/filestorage-server/storage/basquiat.jpg b/filestorage-server/storage/basquiat.jpg new file mode 100644 index 0000000..40108d2 Binary files /dev/null and b/filestorage-server/storage/basquiat.jpg differ