add getting file list on one connection

This commit is contained in:
MKDrwal 2019-12-13 22:10:58 +01:00
parent 127f000183
commit 78747b1071
7 changed files with 56 additions and 21 deletions

View File

@ -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];

View File

@ -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<String> 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;

View File

@ -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<String> filterFileList = new ArrayList<String>() {
{
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) {
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
Server server,
String message
) throws CannotConnectException {
Socket socket;
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();
}

View File

@ -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);
}