files
parent
a6e42a8934
commit
3ba1c8a9f0
|
@ -0,0 +1 @@
|
||||||
|
this is a test file which i can check for errors with
|
|
@ -1,11 +1,59 @@
|
||||||
package client;
|
package client;
|
||||||
|
|
||||||
|
import server.Server;
|
||||||
|
import shared.FileHeader;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
|
|
||||||
|
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) {
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
new Client("localhost", Server.SERVER_PORT).sendFile("ihaveafile.txt");
|
||||||
|
} catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,23 @@
|
||||||
package server;
|
package server;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import shared.FileHeader;
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.*;
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
public class Connection extends Thread {
|
public class Connection extends Thread {
|
||||||
|
|
||||||
private final Socket clientSocket;
|
private final Socket clientSocket;
|
||||||
private final Server server;
|
private final Server server;
|
||||||
private BufferedWriter out;
|
private DataOutputStream out;
|
||||||
private BufferedReader in;
|
private DataInputStream in;
|
||||||
|
|
||||||
public Connection(Server server, Socket clientSocket) {
|
public Connection(Server server, Socket clientSocket) {
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.clientSocket = clientSocket;
|
this.clientSocket = clientSocket;
|
||||||
try {
|
try {
|
||||||
out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
|
out = new DataOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
|
||||||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +25,18 @@ public class Connection extends Thread {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (server.isRunning()) {
|
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 {
|
try {
|
||||||
out.close();
|
out.close();
|
||||||
|
|
|
@ -25,7 +25,7 @@ public class Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isRunning(){
|
public boolean isRunning(){
|
||||||
return isRunning();
|
return running;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package shared;
|
package shared;
|
||||||
|
|
||||||
|
import client.Client;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class FileHeader {
|
public class FileHeader {
|
||||||
|
@ -8,9 +10,9 @@ public class FileHeader {
|
||||||
|
|
||||||
public enum COMMAND {
|
public enum COMMAND {
|
||||||
WRITE((byte) 1);
|
WRITE((byte) 1);
|
||||||
private final byte type;
|
public final byte type;
|
||||||
|
|
||||||
private COMMAND(byte type) {
|
COMMAND(byte type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,18 +21,22 @@ public class FileHeader {
|
||||||
private final String full_path;
|
private final String full_path;
|
||||||
|
|
||||||
public FileHeader(String 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");
|
String workingDirectory = System.getProperty("user.dir");
|
||||||
this.full_path = path;
|
this.full_path = path;
|
||||||
this.relative_path = path.replace(workingDirectory, "");
|
this.relative_path = path.replace(workingDirectory, "");
|
||||||
System.out.println(relative_path);
|
System.out.println(relative_path);
|
||||||
this.size = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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(new FileInputStream(full_path)));
|
||||||
|
|
||||||
writer.write(COMMAND.WRITE.type);
|
writer.writeByte(COMMAND.WRITE.type);
|
||||||
writer.writeUTF(relative_path);
|
writer.writeUTF(relative_path);
|
||||||
|
|
||||||
while (reader.available() > 0) {
|
while (reader.available() > 0) {
|
||||||
|
@ -38,18 +44,25 @@ public class FileHeader {
|
||||||
byte[] bytes = new byte[read];
|
byte[] bytes = new byte[read];
|
||||||
|
|
||||||
int amount = reader.read(bytes);
|
int amount = reader.read(bytes);
|
||||||
|
if (amount <= 0)
|
||||||
|
break;
|
||||||
|
System.out.println("Writing " + amount + " bytes");
|
||||||
writer.writeInt(amount);
|
writer.writeInt(amount);
|
||||||
writer.write(bytes, 0, amount);
|
writer.write(bytes, 0, amount);
|
||||||
}
|
}
|
||||||
|
reader.close();
|
||||||
writer.writeInt(0);
|
writer.writeInt(0);
|
||||||
|
writer.flush();
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void receive(DataInputStream reader) {
|
public static void receive(DataInputStream reader) {
|
||||||
try {
|
try {
|
||||||
String relative = reader.readUTF();
|
String path = System.getProperty("user.dir") + "/out-" + reader.readUTF();
|
||||||
DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(relative)));
|
System.out.println("Writing to file: " + 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];
|
||||||
|
|
Loading…
Reference in New Issue