From 78747b1071e1fe80671ba24a617b6d73a3ed67e4 Mon Sep 17 00:00:00 2001 From: MKDrwal Date: Fri, 13 Dec 2019 22:10:58 +0100 Subject: [PATCH] add getting file list on one connection --- server/sendv2.c | 2 +- src/main/java/ftp/sar/server/Server.java | 11 ++++ .../java/ftp/sar/server/ServerConnector.java | 52 +++++++++++------- .../java/ftp/sar/server/ServerFactory.java | 12 ++++ target/classes/ftp/sar/server/Server.class | Bin 1458 -> 1939 bytes .../ftp/sar/server/ServerConnector.class | Bin 2740 -> 3391 bytes .../ftp/sar/server/ServerFactory.class | Bin 1381 -> 1808 bytes 7 files changed, 56 insertions(+), 21 deletions(-) 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 8ed34a967083aaa7c0b98737d07a693fe5577c64..f58c39e493b630f84889816d3f38474b0dd12097 100644 GIT binary patch literal 1939 zcmah~X-^YT6g{u2w1WtgB8!UR(nYB|vRFmbgrrfD5{!Nu$^fIaCDXx#|KtZvL=#Q? z0sbiCx$~yeZHQmqa_8Q2&%O7&_Rrs+zX2@b#Sl_>ltLOyNi3%j#gbT7WLcHvvDnIT zdXmJ`B-Run_MyU9<-PgIEY;1%eyQp@c4L1x6baa_xF) z&2h|QG1GgfYVS8p_sFpn@?k!!gE`8Kn`l|iC(GG18&-Y2*>Ig^y>2-Qh030LSZbM0 zsTNpE)i7UzBwEed2aD`Rb&`hVma3jfnC)Axz9@|6d~Q7+vGwLIX^&LwhV|;`HAtTkgIw<{sEBg~@=RuD~ZJ30s|nncM)Q{5A=%+6~(+D`fre-L2mc zk2qOxa5R}V<8BXrW)As05fU%qNrrEF^NZ&4oM4jv`X0-9_-Q)445m8S&N499V)&O+ zrkQ4%4(fTITpy_L%l3Bm_^{pGo1rnG=>%uk+}+)3c2dSfsS0Oh1Rb#`+{y*x`mde@ zH&tyOIWLEG7+H#xttPdo{h)890Yg0|tf-oW+QNbv!qa!31a3R&ojjA?uvW7@D1vV=QhJN;TcCT%d?>!Z~+&2Q@DgVt`b}(ctpGl#S^HnJVhYU zDbG4iav2XHtG#>#K^H2L_%g2eSO@%ViZXV(@B@*;FGSz|K!5&k`9d zBF17ySW1R$rkGjS&S{h&*RC3aX$H?S zcrIWBErH3(sdwhNb+1u#t5Fy!Tpv~)yPVGcN-F+Q~-nk4j zJ6jEE4KM11zCbZ4XX`r0HtW(`K_iH^1@eXK2|LAo>Ui5cBIsBpX!!3tUk?0m&pW6y zGFfRJdi8xT4CFlyn$bzn7MQIZM=iJQg>Ksq&-~D>s*QwJ&5w5Y=IKH#bMVzxTYsKO zkR#P(SiY-_NfnB;Cc}JvHw#K`jtTF&cFkU~Tr;ek{N)Cla-`{@&c)(}K&8I?peg6A zD`VCYYZB=s&Zu{EwAbuWnsNjL>}sYLEtXIk<%}2R3pJ z9E>`6iB}w?#0CkB_eSx=%+Z_B;v`CTxusf zh^x*M%;V%>it98ZC#*l+USBtcpQ#}%NX6JkJt1uQZm zC2up^kl9LOPoDg(!3>L7Qc_-LbtR=^Ndk9pH^%B0lK#q~ej#&-LhkXc!$KAYe9}S| zDdf%#LZlueN1+@2R7q5q6w6g+2#K|+571L diff --git a/target/classes/ftp/sar/server/ServerConnector.class b/target/classes/ftp/sar/server/ServerConnector.class index db611edc0c77be3b175aa5ffcc8a95d98566276b..2d7d702969605b9871cb8ac3da594bea13b07cbd 100644 GIT binary patch literal 3391 zcmaJ^`*$0~75-M1G}2}r#ZH`9i6KCoU^@v4Z5o1|h9n}11+qhtacV-FuBElTvAipG zSAoz6(5AGdrKJzveGzC)L;3^f6uSragx~oa_(M279MW%gSJKK>Djm(v-nq~3-nldS z?5j_10(b!bRIv{;DxSoJH1^^tf$yk@sG^D`$!qmey(8eN*bYyiE@dyP=*Kex&#KrZpI21ef#*~_g73@O59IJe zfgcGxuVN2o1YQt$QGr&lEW^oHb=Ngq1zT#4T{9fdG~A-CR}4qN&f=U`%elIfdu-j> zn1ao7X4NyCGiKE&ny#myuXstntmo>US)$plfJ&T0uN-SEwOVG%aJYzBFq%Q~GR`l-{CU)~nOHW6JlC z821)T26b060@rXZ8&0m|Kk_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8odWb!drp7aj;VgYVfz&PDwN-(= z0^v_+MPiOKKeDdQV#{6g^|fYI_*3fQ^hH?8r7o36MYxF#%&hLB7MC(?soe2o&M{;-+l+MA3y2Xi8DIR zBGHT;m~6s1o%4Bx3p$SCq6|qLAxtS;lKHX@9j_|9rlUpXX&r5NT?VrmGf3%3!xF7o zg*hE-aYZ2`idS`X4ax&bI!4h)^FB+ef>~A}8oDa7#bqIy z9~k%$cNIP|@G(A7_|(8>xNqQd+}E($*IQgK2EM=mORZvZ>E;;t5?_hsuX&s*pym6q zLQp00MfDp4-%9e|;d=u=V8FnS;U!@(ljhZ@6;^XM1~{fy3VXdbJ>p+6CN0AoPD);xIfcktmD^}7NO z=~f|$Dc%$UexSp=N&p>PUBe5*T5LrZ*Sm3m*M`Gbj}zE{I5uLOgq*_`T;vsD3fpms z^h}c$N$f0|i0#5&?BiXcn|F-o@d8uZ${WvqS`tr#co9QP^8jre#33}&LJ%+cG&1or zDe`t0FEh#`w7U<%CsYV2>`*wW4<8HBIa-sMLx8$=sIXI7XMTCXY9-q?nL)?SjWVo@24MU-DXdu9?*f@`8gwRbg zY^KhZ2WSzUZOJ0G|M7@EL9WJ`cZu6QEC6tm+sVuZW)mSVd&$T!lNuo(C&@^h9E_8Z zGvp-6+=b7}XeK^N?oTr62s4S{6*9ILVVt5yh!zj~90#b^L g&u0T|Yh?{#wiONkU|eDAV>FZ3I?gmkBSk#%KQ<4lq5uE@ diff --git a/target/classes/ftp/sar/server/ServerFactory.class b/target/classes/ftp/sar/server/ServerFactory.class index 240a6f89c14eed2f4c44186988d346fc9ddd9038..7ceb28ffa65da1020efe0471876523b56f5e4d15 100644 GIT binary patch literal 1808 zcmah}+j84f6kW%c$PvobxsWt%?G}PBNd@835|^}Kn%2d+fYWr+H$_o{+8}vkH3QH5 z1wYUi9?}jN(V?R*ci(EbCqLiPuqn@V4fl2AB|shzG&D3k z6d1YfdTzKOkS>+?1u}L2$Pt)mx}LNB;-^C=*s~5#Xqj&Mwso>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