From c7bf3e89747032a8a0011fe3f4de95d4698c619e Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 10 Nov 2023 16:04:19 -0500 Subject: [PATCH 1/3] cum --- .gitignore | 29 +++++++++ .idea/.gitignore | 8 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/uiDesigner.xml | 124 +++++++++++++++++++++++++++++++++++++ .idea/vcs.xml | 6 ++ Assignment 2 Java.iml | 11 ++++ src/client/Client.java | 11 ++++ src/server/Connection.java | 38 ++++++++++++ src/server/Server.java | 35 +++++++++++ src/shared/FileHeader.java | 58 +++++++++++++++++ 11 files changed, 334 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 Assignment 2 Java.iml create mode 100644 src/client/Client.java create mode 100644 src/server/Connection.java create mode 100644 src/server/Server.java create mode 100644 src/shared/FileHeader.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..aad31e3 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..842dc8b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assignment 2 Java.iml b/Assignment 2 Java.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Assignment 2 Java.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/client/Client.java b/src/client/Client.java new file mode 100644 index 0000000..63e32b0 --- /dev/null +++ b/src/client/Client.java @@ -0,0 +1,11 @@ +package client; + +public class Client { + + public static void main(String[] args) { + + + + } + +} diff --git a/src/server/Connection.java b/src/server/Connection.java new file mode 100644 index 0000000..08cc3e0 --- /dev/null +++ b/src/server/Connection.java @@ -0,0 +1,38 @@ +package server; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.Socket; + +public class Connection extends Thread { + + private final Socket clientSocket; + private final Server server; + private BufferedWriter out; + private BufferedReader in; + + public Connection(Server server, Socket clientSocket) { + this.server = server; + this.clientSocket = clientSocket; + try { + out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())); + in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + } catch (Exception ignored) { + } + } + + @Override + public void run() { + while (server.isRunning()) { + + } + try { + out.close(); + in.close(); + clientSocket.close(); + } catch (Exception ignored) {} + } + +} diff --git a/src/server/Server.java b/src/server/Server.java new file mode 100644 index 0000000..38e5752 --- /dev/null +++ b/src/server/Server.java @@ -0,0 +1,35 @@ +package server; + +import java.io.IOException; +import java.net.ServerSocket; + +public class Server { + + public static final int SERVER_PORT = 42069; + + private ServerSocket serverSocket; + private volatile boolean running = true; + + public Server() { + try { + serverSocket = new ServerSocket(SERVER_PORT); + + while (running) { + new Connection(this, serverSocket.accept()).start(); + } + + serverSocket.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public boolean isRunning(){ + return isRunning(); + } + + public static void main(String[] args) { + new Server(); + } + +} \ No newline at end of file diff --git a/src/shared/FileHeader.java b/src/shared/FileHeader.java new file mode 100644 index 0000000..25e794a --- /dev/null +++ b/src/shared/FileHeader.java @@ -0,0 +1,58 @@ +package shared; + +import java.io.*; + +public class FileHeader { + + private static final int READER_SIZE = 8192; + + public enum COMMAND { + WRITE((byte) 1); + private final byte type; + + private COMMAND(byte type) { + this.type = type; + } + } + + private final String relative_path; + private final String full_path; + + public FileHeader(String path) { + String workingDirectory = System.getProperty("user.dir"); + this.full_path = path; + this.relative_path = path.replace(workingDirectory, ""); + System.out.println(relative_path); + this.size = 0; + } + + void write(DataOutputStream writer) { + try { + DataInputStream reader = new DataInputStream(new BufferedInputStream(new FileInputStream(full_path))); + + writer.write(COMMAND.WRITE.type); + writer.writeUTF(relative_path); + + while (reader.available() > 0) { + int read = Integer.min(reader.available(), READER_SIZE); + byte[] bytes = new byte[read]; + + int amount = reader.read(bytes); + writer.writeInt(amount); + writer.write(bytes, 0, amount); + } + } catch (Exception ignored) { + } + } + + void receive(DataInputStream reader) { + try { + String relative = reader.readUTF(); + + + DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(new FileOutputStream())); + } catch (Exception ignored){ + } + } + +} From a6e42a8934085511076981071e7e90ed3d3779c4 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 10 Nov 2023 16:13:18 -0500 Subject: [PATCH 2/3] reading --- src/shared/FileHeader.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/shared/FileHeader.java b/src/shared/FileHeader.java index 25e794a..e7e639c 100644 --- a/src/shared/FileHeader.java +++ b/src/shared/FileHeader.java @@ -41,6 +41,7 @@ public class FileHeader { writer.writeInt(amount); writer.write(bytes, 0, amount); } + writer.writeInt(0); } catch (Exception ignored) { } } @@ -48,9 +49,13 @@ public class FileHeader { void receive(DataInputStream reader) { try { String relative = reader.readUTF(); - - - DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(new FileOutputStream())); + DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(relative))); + int size = 0; + while ((size = reader.readInt()) > 0){ + byte[] data = new byte[size]; + int amount = reader.read(data, 0, size); + writer.write(data, 0, amount); + } } catch (Exception ignored){ } } From 3ba1c8a9f0ade8fff87ad79e20b540c74412bf5a Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Sat, 11 Nov 2023 22:52:33 -0500 Subject: [PATCH 3/3] files --- ihaveafile.txt | 1 + out-ihaveafile.txt | 0 src/client/Client.java | 50 +++++++++++++++++++++++++++++++++++++- src/server/Connection.java | 26 ++++++++++++++------ src/server/Server.java | 2 +- src/shared/FileHeader.java | 29 ++++++++++++++++------ 6 files changed, 90 insertions(+), 18 deletions(-) create mode 100644 ihaveafile.txt create mode 100644 out-ihaveafile.txt diff --git a/ihaveafile.txt b/ihaveafile.txt new file mode 100644 index 0000000..08a24f2 --- /dev/null +++ b/ihaveafile.txt @@ -0,0 +1 @@ +this is a test file which i can check for errors with diff --git a/out-ihaveafile.txt b/out-ihaveafile.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/client/Client.java b/src/client/Client.java index 63e32b0..dc9b43b 100644 --- a/src/client/Client.java +++ b/src/client/Client.java @@ -1,11 +1,59 @@ package client; +import server.Server; +import shared.FileHeader; + +import java.io.*; +import java.net.Socket; + public class Client { - public static void main(String[] args) { + public static class ClientInvalidUsageException extends RuntimeException { + public ClientInvalidUsageException(String str){ + super(str); + } + } + private final Socket serverConnection; + private final DataOutputStream out; + private final DataInputStream in; + public Client(String address, int port) throws IOException { + serverConnection = new Socket(address, port); + out = new DataOutputStream(new BufferedOutputStream(serverConnection.getOutputStream())); + in = new DataInputStream(new BufferedInputStream(serverConnection.getInputStream())); + } + + void sendFile(String path){ + if (new File(path).isDirectory()) + throw new ClientInvalidUsageException("Unable to send directory. Did you mean sendDir()?"); + System.out.println("Sending path " + path); + new FileHeader(path).write(out); + try { + out.flush(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + void sendDir(String path){ } + void close(){ + try { + in.close(); + out.close(); + serverConnection.close(); + } catch (Exception ignored){} + } + + public static void main(String[] args) { + try { + new Client("localhost", Server.SERVER_PORT).sendFile("ihaveafile.txt"); + } catch (Exception e){ + e.printStackTrace(); + } + } + } diff --git a/src/server/Connection.java b/src/server/Connection.java index 08cc3e0..ff09b13 100644 --- a/src/server/Connection.java +++ b/src/server/Connection.java @@ -1,24 +1,23 @@ package server; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; +import shared.FileHeader; + +import java.io.*; import java.net.Socket; public class Connection extends Thread { private final Socket clientSocket; private final Server server; - private BufferedWriter out; - private BufferedReader in; + private DataOutputStream out; + private DataInputStream in; public Connection(Server server, Socket clientSocket) { this.server = server; this.clientSocket = clientSocket; try { - out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())); - in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + out = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream())); + in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream())); } catch (Exception ignored) { } } @@ -26,7 +25,18 @@ public class Connection extends Thread { @Override public void run() { while (server.isRunning()) { + if (!clientSocket.isConnected()) + break; + try { + if (in.available() > 0) { + byte command = in.readByte(); + if (command == FileHeader.COMMAND.WRITE.type) + FileHeader.receive(in); + } + } catch (IOException e) { + throw new RuntimeException(e); + } } try { out.close(); diff --git a/src/server/Server.java b/src/server/Server.java index 38e5752..6ea1781 100644 --- a/src/server/Server.java +++ b/src/server/Server.java @@ -25,7 +25,7 @@ public class Server { } public boolean isRunning(){ - return isRunning(); + return running; } public static void main(String[] args) { diff --git a/src/shared/FileHeader.java b/src/shared/FileHeader.java index e7e639c..78bd81e 100644 --- a/src/shared/FileHeader.java +++ b/src/shared/FileHeader.java @@ -1,5 +1,7 @@ package shared; +import client.Client; + import java.io.*; public class FileHeader { @@ -8,9 +10,9 @@ public class FileHeader { public enum COMMAND { WRITE((byte) 1); - private final byte type; + public final byte type; - private COMMAND(byte type) { + COMMAND(byte type) { this.type = type; } } @@ -19,18 +21,22 @@ public class FileHeader { private final String full_path; public FileHeader(String path) { + File pf = new File(path); + if (!pf.exists()) + throw new Client.ClientInvalidUsageException("Unable to send a file which doesn't exist!"); + if (pf.isDirectory()) + throw new Client.ClientInvalidUsageException("Path is a directory unable to send!"); String workingDirectory = System.getProperty("user.dir"); this.full_path = path; this.relative_path = path.replace(workingDirectory, ""); System.out.println(relative_path); - this.size = 0; } - void write(DataOutputStream writer) { + public void write(DataOutputStream writer) { try { DataInputStream reader = new DataInputStream(new BufferedInputStream(new FileInputStream(full_path))); - writer.write(COMMAND.WRITE.type); + writer.writeByte(COMMAND.WRITE.type); writer.writeUTF(relative_path); while (reader.available() > 0) { @@ -38,18 +44,25 @@ public class FileHeader { byte[] bytes = new byte[read]; int amount = reader.read(bytes); + if (amount <= 0) + break; + System.out.println("Writing " + amount + " bytes"); writer.writeInt(amount); writer.write(bytes, 0, amount); } + reader.close(); writer.writeInt(0); + writer.flush(); } catch (Exception ignored) { } } - void receive(DataInputStream reader) { + public static void receive(DataInputStream reader) { try { - String relative = reader.readUTF(); - DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(relative))); + String path = System.getProperty("user.dir") + "/out-" + reader.readUTF(); + System.out.println("Writing to file: " + path); + + DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path))); int size = 0; while ((size = reader.readInt()) > 0){ byte[] data = new byte[size];