diff --git a/server/sendv2.c b/server/sendv2.c index f9f98b9..a3d691d 100644 --- a/server/sendv2.c +++ b/server/sendv2.c @@ -12,7 +12,7 @@ int main(int argc, char **argv) { const int port = 7332; - const char ip[512] = "192.168.0.43"; + const char ip[512] = "192.168.0.102"; struct sockaddr_in endpoint; struct dirent **namelist; char buff[512]; diff --git a/src/main/java/ftp/sar/server/Server.java b/src/main/java/ftp/sar/server/Server.java index 911ec65..88f9d1c 100644 --- a/src/main/java/ftp/sar/server/Server.java +++ b/src/main/java/ftp/sar/server/Server.java @@ -2,6 +2,7 @@ package ftp.sar.server; import ftp.sar.controller.ServerPanelController; +import java.net.Socket; import java.util.ArrayList; public class Server { @@ -10,6 +11,16 @@ public class Server { private ArrayList fileList = new ArrayList<>(); private ServerPanelController serverPanelController; + public Socket getSocket() { + return socket; + } + + public void setSocket(Socket socket) { + this.socket = socket; + } + + private Socket socket; + public Server (String ip, int port) { this.ip = ip; this.port = port; diff --git a/src/main/java/ftp/sar/server/ServerConnector.java b/src/main/java/ftp/sar/server/ServerConnector.java index 3de5de9..9a5b0a0 100644 --- a/src/main/java/ftp/sar/server/ServerConnector.java +++ b/src/main/java/ftp/sar/server/ServerConnector.java @@ -4,33 +4,49 @@ import ftp.sar.PropertiesLoader; import ftp.sar.exception.CannotConnectException; import java.io.*; -import java.net.Socket; +import java.util.ArrayList; public class ServerConnector { private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance(); + ArrayList filterFileList = new ArrayList() { + { + add("."); + add(".."); + add("server"); + } + }; + public ServerConnector() { } public boolean readFileList(Server server) throws CannotConnectException { - - this.sendRequest("GET_ALL_FILES", server); + this.sendRequest(server, "GET_ALL_FILES"); try { - Socket socket = new Socket(server.getIp(), server.getPort()); - DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); + DataInputStream dataInputStream = new DataInputStream(server.getSocket().getInputStream()); InputStreamReader inputStreamReader = new InputStreamReader(dataInputStream); BufferedReader reader = new BufferedReader(inputStreamReader); - while (true) { - String file = reader.readLine(); - System.out.println(file); + char[] buffer = new char[512]; - if (file == null) { - break; + reader.read(buffer); + + String file = ""; + for (char c:buffer) { + switch (c) { + case '\n': + if (!this.filterFileList.contains(file)) { + server.addToFileList(file); + } + file = ""; + break; + case '\u0000': + break; + default: + file += c; } - server.addToFileList(file); } return true; @@ -42,20 +58,16 @@ public class ServerConnector { } private void sendRequest( - String request, - Server server - ) throws CannotConnectException { - Socket socket; + Server server, + String message + ) throws CannotConnectException { try { - socket = new Socket(server.getIp(), server.getPort()); - DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()); + DataOutputStream dataOutputStream = new DataOutputStream(server.getSocket().getOutputStream()); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(dataOutputStream); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); - bufferedWriter.write(request, 0, request.length()); + bufferedWriter.write(message, 0, message.length()); bufferedWriter.flush(); - bufferedWriter.close(); - socket.close(); } catch (IOException e) { throw new CannotConnectException(); } diff --git a/src/main/java/ftp/sar/server/ServerFactory.java b/src/main/java/ftp/sar/server/ServerFactory.java index 464ad69..b00df9d 100644 --- a/src/main/java/ftp/sar/server/ServerFactory.java +++ b/src/main/java/ftp/sar/server/ServerFactory.java @@ -3,6 +3,9 @@ package ftp.sar.server; import ftp.sar.exception.CannotConnectException; import ftp.sar.validator.IpValidator; +import java.io.IOException; +import java.net.Socket; + public class ServerFactory { public Server create(String ip, int port) { @@ -14,6 +17,7 @@ public class ServerFactory { ServerConnector connector = new ServerConnector(); try { + this.createConnection(server); connector.readFileList(server); } catch (CannotConnectException e) { e.printStackTrace(); @@ -22,6 +26,14 @@ public class ServerFactory { return server; } + private void createConnection(Server server) throws CannotConnectException{ + try { + server.setSocket(new Socket(server.getIp(), server.getPort())); + } catch (IOException e) { + throw new CannotConnectException(); + } + } + private Server createServer(String ip, int port) { return new Server(ip, port); } diff --git a/target/classes/ftp/sar/server/Server.class b/target/classes/ftp/sar/server/Server.class index 8ed34a9..f58c39e 100644 Binary files a/target/classes/ftp/sar/server/Server.class and b/target/classes/ftp/sar/server/Server.class differ diff --git a/target/classes/ftp/sar/server/ServerConnector.class b/target/classes/ftp/sar/server/ServerConnector.class index db611ed..2d7d702 100644 Binary files a/target/classes/ftp/sar/server/ServerConnector.class and b/target/classes/ftp/sar/server/ServerConnector.class differ diff --git a/target/classes/ftp/sar/server/ServerFactory.class b/target/classes/ftp/sar/server/ServerFactory.class index 240a6f8..7ceb28f 100644 Binary files a/target/classes/ftp/sar/server/ServerFactory.class and b/target/classes/ftp/sar/server/ServerFactory.class differ