Compare commits
No commits in common. "132717765640e818800e06eefdec3254b2ae4ed4" and "3ba1c8a9f0ade8fff87ad79e20b540c74412bf5a" have entirely different histories.
1327177656
...
3ba1c8a9f0
|
@ -3,9 +3,6 @@ out/
|
||||||
!**/src/main/**/out/
|
!**/src/main/**/out/
|
||||||
!**/src/test/**/out/
|
!**/src/test/**/out/
|
||||||
|
|
||||||
in/
|
|
||||||
write/
|
|
||||||
|
|
||||||
### Eclipse ###
|
### Eclipse ###
|
||||||
.apt_generated
|
.apt_generated
|
||||||
.classpath
|
.classpath
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
this is a test file which i can check for errors with
|
|
|
@ -1,12 +1,10 @@
|
||||||
package client;
|
package client;
|
||||||
|
|
||||||
import server.Server;
|
import server.Server;
|
||||||
import shared.ExceptionLogger;
|
|
||||||
import shared.FileHeader;
|
import shared.FileHeader;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
|
|
||||||
|
@ -34,20 +32,12 @@ public class Client {
|
||||||
try {
|
try {
|
||||||
out.flush();
|
out.flush();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
ExceptionLogger.log(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendDir(String path){
|
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(){
|
void close(){
|
||||||
|
@ -55,16 +45,14 @@ public class Client {
|
||||||
in.close();
|
in.close();
|
||||||
out.close();
|
out.close();
|
||||||
serverConnection.close();
|
serverConnection.close();
|
||||||
} catch (Exception e){
|
} catch (Exception ignored){}
|
||||||
ExceptionLogger.log(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
new Client("localhost", Server.SERVER_PORT).sendDir("in/");
|
new Client("localhost", Server.SERVER_PORT).sendFile("ihaveafile.txt");
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
ExceptionLogger.log(e);
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package server;
|
package server;
|
||||||
|
|
||||||
import shared.ExceptionLogger;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
|
|
||||||
|
@ -8,11 +7,12 @@ public class Server {
|
||||||
|
|
||||||
public static final int SERVER_PORT = 42069;
|
public static final int SERVER_PORT = 42069;
|
||||||
|
|
||||||
|
private ServerSocket serverSocket;
|
||||||
private volatile boolean running = true;
|
private volatile boolean running = true;
|
||||||
|
|
||||||
public Server() {
|
public Server() {
|
||||||
try {
|
try {
|
||||||
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
|
serverSocket = new ServerSocket(SERVER_PORT);
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
new Connection(this, serverSocket.accept()).start();
|
new Connection(this, serverSocket.accept()).start();
|
||||||
|
@ -20,7 +20,7 @@ public class Server {
|
||||||
|
|
||||||
serverSocket.close();
|
serverSocket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
ExceptionLogger.log(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -3,8 +3,6 @@ package shared;
|
||||||
import client.Client;
|
import client.Client;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
|
|
||||||
public class FileHeader {
|
public class FileHeader {
|
||||||
|
|
||||||
|
@ -36,7 +34,7 @@ public class FileHeader {
|
||||||
|
|
||||||
public void write(DataOutputStream writer) {
|
public void write(DataOutputStream writer) {
|
||||||
try {
|
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.writeByte(COMMAND.WRITE.type);
|
||||||
writer.writeUTF(relative_path);
|
writer.writeUTF(relative_path);
|
||||||
|
@ -55,35 +53,23 @@ public class FileHeader {
|
||||||
reader.close();
|
reader.close();
|
||||||
writer.writeInt(0);
|
writer.writeInt(0);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
ExceptionLogger.log(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void receive(DataInputStream reader) {
|
public static void receive(DataInputStream reader) {
|
||||||
try {
|
try {
|
||||||
String userFile = reader.readUTF();
|
String path = System.getProperty("user.dir") + "/out-" + 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;
|
|
||||||
System.out.println("Writing to file: " + path);
|
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;
|
int size = 0;
|
||||||
while ((size = reader.readInt()) > 0){
|
while ((size = reader.readInt()) > 0){
|
||||||
byte[] data = new byte[size];
|
byte[] data = new byte[size];
|
||||||
int amount = reader.read(data, 0, size);
|
int amount = reader.read(data, 0, size);
|
||||||
writer.write(data, 0, amount);
|
writer.write(data, 0, amount);
|
||||||
}
|
}
|
||||||
writer.flush();
|
} catch (Exception ignored){
|
||||||
} catch (Exception e){
|
|
||||||
ExceptionLogger.log(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue