Compare commits

..

No commits in common. "132717765640e818800e06eefdec3254b2ae4ed4" and "3ba1c8a9f0ade8fff87ad79e20b540c74412bf5a" have entirely different histories.

6 changed files with 14 additions and 59 deletions

3
.gitignore vendored
View File

@ -3,9 +3,6 @@ out/
!**/src/main/**/out/
!**/src/test/**/out/
in/
write/
### Eclipse ###
.apt_generated
.classpath

View File

@ -1 +0,0 @@
this is a test file which i can check for errors with

View File

@ -1,12 +1,10 @@
package client;
import server.Server;
import shared.ExceptionLogger;
import shared.FileHeader;
import java.io.*;
import java.net.Socket;
import java.util.*;
public class Client {
@ -34,20 +32,12 @@ public class Client {
try {
out.flush();
} catch (IOException e) {
ExceptionLogger.log(e);
throw new RuntimeException(e);
}
}
void sendDir(String path){
File p = new File(path);
ArrayDeque<File> filesToCheck = new ArrayDeque<>(Arrays.asList(Objects.requireNonNull(p.listFiles())));
while (!filesToCheck.isEmpty()) {
File f = filesToCheck.remove();
if (f.isDirectory()){
filesToCheck.add(f);
} else
sendFile(f.getPath());
}
}
void close(){
@ -55,16 +45,14 @@ public class Client {
in.close();
out.close();
serverConnection.close();
} catch (Exception e){
ExceptionLogger.log(e);
}
} catch (Exception ignored){}
}
public static void main(String[] args) {
try {
new Client("localhost", Server.SERVER_PORT).sendDir("in/");
new Client("localhost", Server.SERVER_PORT).sendFile("ihaveafile.txt");
} catch (Exception e){
ExceptionLogger.log(e);
e.printStackTrace();
}
}

View File

@ -1,6 +1,5 @@
package server;
import shared.ExceptionLogger;
import java.io.IOException;
import java.net.ServerSocket;
@ -8,11 +7,12 @@ public class Server {
public static final int SERVER_PORT = 42069;
private ServerSocket serverSocket;
private volatile boolean running = true;
public Server() {
try {
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
serverSocket = new ServerSocket(SERVER_PORT);
while (running) {
new Connection(this, serverSocket.accept()).start();
@ -20,7 +20,7 @@ public class Server {
serverSocket.close();
} catch (IOException e) {
ExceptionLogger.log(e);
throw new RuntimeException(e);
}
}

View File

@ -1,15 +0,0 @@
package shared;
public class ExceptionLogger {
public static void log(Exception e){
System.err.println("We have caught an exception:");
System.err.println("\tCaused by: ");
System.err.println("\t\t" + e.getLocalizedMessage());
System.err.println("\tStack trace:");
StackTraceElement[] stack = e.getStackTrace();
for (StackTraceElement s : stack)
System.err.println("\t\tAt " + s.toString());
}
}

View File

@ -3,8 +3,6 @@ package shared;
import client.Client;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FileHeader {
@ -36,7 +34,7 @@ public class FileHeader {
public void write(DataOutputStream writer) {
try {
DataInputStream reader = new DataInputStream(new BufferedInputStream(Files.newInputStream(Paths.get(full_path))));
DataInputStream reader = new DataInputStream(new BufferedInputStream(new FileInputStream(full_path)));
writer.writeByte(COMMAND.WRITE.type);
writer.writeUTF(relative_path);
@ -55,35 +53,23 @@ public class FileHeader {
reader.close();
writer.writeInt(0);
writer.flush();
} catch (Exception e) {
ExceptionLogger.log(e);
} catch (Exception ignored) {
}
}
public static void receive(DataInputStream reader) {
try {
String userFile = reader.readUTF();
String[] pathParts = userFile.split("/");
String userDirectory = userFile.replace(pathParts[pathParts.length-1], "");
File ld = new File(System.getProperty("user.dir") + "/write/" + userDirectory);
if (!ld.exists())
if(!ld.mkdirs())
System.out.println("Failed to create directory");
String path = System.getProperty("user.dir") + "/write/" + userFile;
String path = System.getProperty("user.dir") + "/out-" + reader.readUTF();
System.out.println("Writing to file: " + path);
DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(Files.newOutputStream(Paths.get(path))));
DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
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);
}
writer.flush();
} catch (Exception e){
ExceptionLogger.log(e);
} catch (Exception ignored){
}
}