From a4af498354223a9b4ca1acdb77b2f081cb9e6d13 Mon Sep 17 00:00:00 2001 From: "isaacklugman@gmail.com" <ik1g19@soton.ac.uk> Date: Tue, 4 May 2021 20:25:33 +0100 Subject: [PATCH] fixed bug causing client sockets to close before they should --- ClientAndLoggers/ClientMain.java | 6 +----- src/ftp/DStore.java | 18 ++++++++++++------ src/ftp/Server.java | 8 ++++---- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/ClientAndLoggers/ClientMain.java b/ClientAndLoggers/ClientMain.java index 849530b..5b761bc 100644 --- a/ClientAndLoggers/ClientMain.java +++ b/ClientAndLoggers/ClientMain.java @@ -11,13 +11,9 @@ public class ClientMain { File downloadFolder = new File("downloads"); if (!downloadFolder.exists()) if (!downloadFolder.mkdir()) throw new RuntimeException("Cannot create download folder (folder absolute path: " + downloadFolder.getAbsolutePath() + ")"); - - File uploadFolder = new File("to_store"); - if (!uploadFolder.exists()) - throw new RuntimeException("to_store folder does not exist"); - testClient(cport, timeout, downloadFolder); + testClient(cport, timeout, downloadFolder, uploadFolder); } diff --git a/src/ftp/DStore.java b/src/ftp/DStore.java index af5d246..8346c8d 100644 --- a/src/ftp/DStore.java +++ b/src/ftp/DStore.java @@ -106,7 +106,7 @@ public class DStore extends Server { if (response.equals("ACK")) threadIDOutput("Successfully joined Controller"); - closeConnection(client); + closeConnection("Client", client); } else if (command.equals("STORE")) { @@ -129,8 +129,8 @@ public class DStore extends Server { send("STORE_ACK " + filename, controller); } else threadIDErr("Unable to connect to Controller"); - closeConnection(controller); - } + closeConnection("Controller", controller); + } else if (command.equals("LOAD_DATA")) { String filename = args[1]; @@ -155,7 +155,7 @@ public class DStore extends Server { FileOutputStream out = new FileOutputStream(outputFile); - threadIDOutput("Starting file Read..."); + threadIDOutput("Starting file Write..."); while ((buflen = in.read(buf)) != -1) { out.write(buf,0,buflen); @@ -164,7 +164,8 @@ public class DStore extends Server { threadIDOutput("Finished file Write"); - in.close(); out.close(); + //in.close(); + out.close(); } @@ -178,13 +179,18 @@ public class DStore extends Server { byte[] buf = new byte[1000]; int buflen; + threadIDOutput("Starting file Send"); while ((buflen = inf.read(buf)) != -1) { out.write(buf, 0, buflen); } + threadIDOutput("Finished file Send"); + - inf.close(); client.close(); out.close(); + inf.close(); + //client.close(); + //out.close(); } } diff --git a/src/ftp/Server.java b/src/ftp/Server.java index 59c55ec..c5e1469 100644 --- a/src/ftp/Server.java +++ b/src/ftp/Server.java @@ -27,7 +27,7 @@ public abstract class Server { if (request == null) closed = true; else handleRequest(request, client); } - closeConnection(client); + closeConnection("Client", client); } catch (Exception e) { threadIDErr("Exception thrown: " + e.getMessage()); } @@ -86,7 +86,7 @@ public abstract class Server { threadIDOutput("Recieved: " + request); } catch (IOException e) { - threadIDErr("Error: " + e); + threadIDErr("Read Socket Error: " + e); } return request; @@ -104,8 +104,8 @@ public abstract class Server { - protected void closeConnection(Socket client) { - try {client.close(); threadIDOutput("Connection Closed");} catch (IOException e) {threadIDErr(e.getMessage());} + protected void closeConnection(String name, Socket client) { + try {client.close(); threadIDOutput(name + " Socket Closed");} catch (IOException e) {threadIDErr(e.getMessage());} } } \ No newline at end of file -- GitLab