From e6d950d16ba65e4230b5e0f027f2113ebff725dd Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 2 Mar 2020 15:31:37 +0100 Subject: [PATCH] client is able to close the connection, but the server IS NOT --- src/clientP2P/ClientManagementTCP.java | 5 +++-- src/clientP2P/ClientManagementUDP.java | 4 ++-- src/serverP2P/ServerManagementTCP.java | 26 +++++++++++++------------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/clientP2P/ClientManagementTCP.java b/src/clientP2P/ClientManagementTCP.java index aaf1e9c..d220d7b 100644 --- a/src/clientP2P/ClientManagementTCP.java +++ b/src/clientP2P/ClientManagementTCP.java @@ -123,7 +123,7 @@ public class ClientManagementTCP implements Runnable { * @throws EmptyFile */ private void download(String filename) throws EmptyFile, NotFound, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { - final long MAX_PARTIAL_SIZE = 1024; + final long MAX_PARTIAL_SIZE = 4096; ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, 0, MAX_PARTIAL_SIZE)); d.sendRequest((Object)socket); boolean fileFullyWritten = false; @@ -177,7 +177,7 @@ public class ClientManagementTCP implements Runnable { d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, offset, MAX_PARTIAL_SIZE)); d.sendRequest((Object)socket); } - + } else { System.err.println("offset: " + fp.getOffset() + " ; content.length: " + fp.getPartialContent().length + " ; totalSize: " + fp.getTotalSize()); System.err.println("Error: cannot handle non-consecutive partial files (not implemented)"); @@ -188,6 +188,7 @@ public class ClientManagementTCP implements Runnable { throw new ProtocolError(); } } while(!fileFullyWritten); + socket.close(); } /** list server’s directory content diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 3bb9fcb..78d84c5 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -121,7 +121,7 @@ public class ClientManagementUDP implements Runnable { * @throws EmptyFile */ private void download(String filename) throws EmptyFile, NotFound, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { - final long MAX_PARTIAL_SIZE = 1024; + final long MAX_PARTIAL_SIZE = 4096; ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, 0, MAX_PARTIAL_SIZE)); d.sendRequest((Object)socket); boolean fileFullyWritten = false; @@ -175,7 +175,7 @@ public class ClientManagementUDP implements Runnable { d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, offset, MAX_PARTIAL_SIZE)); d.sendRequest((Object)socket); } - + } else { System.err.println("offset: " + fp.getOffset() + " ; content.length: " + fp.getPartialContent().length + " ; totalSize: " + fp.getTotalSize()); System.err.println("Error: cannot handle non-consecutive partial files (not implemented)"); diff --git a/src/serverP2P/ServerManagementTCP.java b/src/serverP2P/ServerManagementTCP.java index ab5f876..ba04f4e 100644 --- a/src/serverP2P/ServerManagementTCP.java +++ b/src/serverP2P/ServerManagementTCP.java @@ -57,7 +57,7 @@ public class ServerManagementTCP implements Runnable { System.exit(-1); } catch (IOException e) { System.err.println("Error: cannot openning TCP socket"); - System.exit(-2); + System.exit(-2); } } @@ -68,7 +68,7 @@ public class ServerManagementTCP implements Runnable { try { Socket s = socket.accept(); System.err.println("Accepting new connection"); - while(true) { + do { try { ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)s); Payload p = pd.getPayload(); @@ -110,7 +110,7 @@ public class ServerManagementTCP implements Runnable { for (String f: fileList) { System.err.println("- " + f); } - + throw new IOException(); // to send a NOT_FOUND in the catch block } } catch (IOException e) { @@ -139,14 +139,17 @@ public class ServerManagementTCP implements Runnable { default: sendInternalError(pd); } - } catch (IOException e) { - } catch (TransmissionError e) { - } catch (ProtocolError e) { - } catch (VersionError e) { - } catch (InternalError e) { - } catch (SizeError e) { + } catch (SocketException e) { + System.out.println("connection closed"); + s.close(); } - } + catch (IOException e) {} + catch (TransmissionError e) {} + catch (ProtocolError e) {} + catch (VersionError e) {} + catch (InternalError e) {} + catch (SizeError e) {} + } while(true); } catch (IOException e) { } } @@ -168,8 +171,6 @@ public class ServerManagementTCP implements Runnable { Arrays.sort(fileList); } - - /** Send an internal error message. * @param pd ProtocolP2PPacketTCP to respond */ @@ -182,4 +183,3 @@ public class ServerManagementTCP implements Runnable { } } -