Compare commits
3 Commits
3ba1c8a9f0
...
1327177656
Author | SHA1 | Date |
---|---|---|
Brett | 1327177656 | |
Brett | e2c0ee16a8 | |
Brett | 70ca6acadd |
|
@ -3,6 +3,9 @@ out/
|
||||||
!**/src/main/**/out/
|
!**/src/main/**/out/
|
||||||
!**/src/test/**/out/
|
!**/src/test/**/out/
|
||||||
|
|
||||||
|
in/
|
||||||
|
write/
|
||||||
|
|
||||||
### Eclipse ###
|
### Eclipse ###
|
||||||
.apt_generated
|
.apt_generated
|
||||||
.classpath
|
.classpath
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
this is a test file which i can check for errors with
|
|
@ -1,10 +1,12 @@
|
||||||
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 {
|
||||||
|
|
||||||
|
@ -32,12 +34,20 @@ public class Client {
|
||||||
try {
|
try {
|
||||||
out.flush();
|
out.flush();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
ExceptionLogger.log(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(){
|
||||||
|
@ -45,14 +55,16 @@ public class Client {
|
||||||
in.close();
|
in.close();
|
||||||
out.close();
|
out.close();
|
||||||
serverConnection.close();
|
serverConnection.close();
|
||||||
} catch (Exception ignored){}
|
} catch (Exception e){
|
||||||
|
ExceptionLogger.log(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
new Client("localhost", Server.SERVER_PORT).sendFile("ihaveafile.txt");
|
new Client("localhost", Server.SERVER_PORT).sendDir("in/");
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
e.printStackTrace();
|
ExceptionLogger.log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package server;
|
package server;
|
||||||
|
|
||||||
|
import shared.ExceptionLogger;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
|
|
||||||
|
@ -7,12 +8,11 @@ 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 = new ServerSocket(SERVER_PORT);
|
ServerSocket 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) {
|
||||||
throw new RuntimeException(e);
|
ExceptionLogger.log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
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,6 +3,8 @@ 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 {
|
||||||
|
|
||||||
|
@ -34,7 +36,7 @@ public class FileHeader {
|
||||||
|
|
||||||
public void write(DataOutputStream writer) {
|
public void write(DataOutputStream writer) {
|
||||||
try {
|
try {
|
||||||
DataInputStream reader = new DataInputStream(new BufferedInputStream(new FileInputStream(full_path)));
|
DataInputStream reader = new DataInputStream(new BufferedInputStream(Files.newInputStream(Paths.get(full_path))));
|
||||||
|
|
||||||
writer.writeByte(COMMAND.WRITE.type);
|
writer.writeByte(COMMAND.WRITE.type);
|
||||||
writer.writeUTF(relative_path);
|
writer.writeUTF(relative_path);
|
||||||
|
@ -53,23 +55,35 @@ public class FileHeader {
|
||||||
reader.close();
|
reader.close();
|
||||||
writer.writeInt(0);
|
writer.writeInt(0);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
} catch (Exception ignored) {
|
} catch (Exception e) {
|
||||||
|
ExceptionLogger.log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void receive(DataInputStream reader) {
|
public static void receive(DataInputStream reader) {
|
||||||
try {
|
try {
|
||||||
String path = System.getProperty("user.dir") + "/out-" + reader.readUTF();
|
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;
|
||||||
System.out.println("Writing to file: " + path);
|
System.out.println("Writing to file: " + path);
|
||||||
|
|
||||||
DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
|
DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(Files.newOutputStream(Paths.get(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);
|
||||||
}
|
}
|
||||||
} catch (Exception ignored){
|
writer.flush();
|
||||||
|
} catch (Exception e){
|
||||||
|
ExceptionLogger.log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue