add getting file list on one connection
This commit is contained in:
parent
127f000183
commit
78747b1071
@ -12,7 +12,7 @@
|
|||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
const int port = 7332;
|
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 sockaddr_in endpoint;
|
||||||
struct dirent **namelist;
|
struct dirent **namelist;
|
||||||
char buff[512];
|
char buff[512];
|
||||||
|
@ -2,6 +2,7 @@ package ftp.sar.server;
|
|||||||
|
|
||||||
import ftp.sar.controller.ServerPanelController;
|
import ftp.sar.controller.ServerPanelController;
|
||||||
|
|
||||||
|
import java.net.Socket;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
@ -10,6 +11,16 @@ public class Server {
|
|||||||
private ArrayList<String> fileList = new ArrayList<>();
|
private ArrayList<String> fileList = new ArrayList<>();
|
||||||
private ServerPanelController serverPanelController;
|
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) {
|
public Server (String ip, int port) {
|
||||||
this.ip = ip;
|
this.ip = ip;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
@ -4,33 +4,49 @@ import ftp.sar.PropertiesLoader;
|
|||||||
import ftp.sar.exception.CannotConnectException;
|
import ftp.sar.exception.CannotConnectException;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.Socket;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class ServerConnector {
|
public class ServerConnector {
|
||||||
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
|
private PropertiesLoader propertiesLoader = PropertiesLoader.getInstance();
|
||||||
|
|
||||||
|
ArrayList<String> filterFileList = new ArrayList<String>() {
|
||||||
|
{
|
||||||
|
add(".");
|
||||||
|
add("..");
|
||||||
|
add("server");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public ServerConnector() {
|
public ServerConnector() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean readFileList(Server server) throws CannotConnectException {
|
public boolean readFileList(Server server) throws CannotConnectException {
|
||||||
|
this.sendRequest(server, "GET_ALL_FILES");
|
||||||
this.sendRequest("GET_ALL_FILES", server);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Socket socket = new Socket(server.getIp(), server.getPort());
|
DataInputStream dataInputStream = new DataInputStream(server.getSocket().getInputStream());
|
||||||
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
|
|
||||||
InputStreamReader inputStreamReader = new InputStreamReader(dataInputStream);
|
InputStreamReader inputStreamReader = new InputStreamReader(dataInputStream);
|
||||||
BufferedReader reader = new BufferedReader(inputStreamReader);
|
BufferedReader reader = new BufferedReader(inputStreamReader);
|
||||||
|
|
||||||
while (true) {
|
char[] buffer = new char[512];
|
||||||
String file = reader.readLine();
|
|
||||||
System.out.println(file);
|
|
||||||
|
|
||||||
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;
|
break;
|
||||||
|
case '\u0000':
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
file += c;
|
||||||
}
|
}
|
||||||
|
|
||||||
server.addToFileList(file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -42,20 +58,16 @@ public class ServerConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void sendRequest(
|
private void sendRequest(
|
||||||
String request,
|
Server server,
|
||||||
Server server
|
String message
|
||||||
) throws CannotConnectException {
|
) throws CannotConnectException {
|
||||||
Socket socket;
|
|
||||||
try {
|
try {
|
||||||
socket = new Socket(server.getIp(), server.getPort());
|
DataOutputStream dataOutputStream = new DataOutputStream(server.getSocket().getOutputStream());
|
||||||
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
|
|
||||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(dataOutputStream);
|
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(dataOutputStream);
|
||||||
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
|
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
|
||||||
|
|
||||||
bufferedWriter.write(request, 0, request.length());
|
bufferedWriter.write(message, 0, message.length());
|
||||||
bufferedWriter.flush();
|
bufferedWriter.flush();
|
||||||
bufferedWriter.close();
|
|
||||||
socket.close();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new CannotConnectException();
|
throw new CannotConnectException();
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,9 @@ package ftp.sar.server;
|
|||||||
import ftp.sar.exception.CannotConnectException;
|
import ftp.sar.exception.CannotConnectException;
|
||||||
import ftp.sar.validator.IpValidator;
|
import ftp.sar.validator.IpValidator;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
public class ServerFactory {
|
public class ServerFactory {
|
||||||
|
|
||||||
public Server create(String ip, int port) {
|
public Server create(String ip, int port) {
|
||||||
@ -14,6 +17,7 @@ public class ServerFactory {
|
|||||||
|
|
||||||
ServerConnector connector = new ServerConnector();
|
ServerConnector connector = new ServerConnector();
|
||||||
try {
|
try {
|
||||||
|
this.createConnection(server);
|
||||||
connector.readFileList(server);
|
connector.readFileList(server);
|
||||||
} catch (CannotConnectException e) {
|
} catch (CannotConnectException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -22,6 +26,14 @@ public class ServerFactory {
|
|||||||
return server;
|
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) {
|
private Server createServer(String ip, int port) {
|
||||||
return new Server(ip, port);
|
return new Server(ip, port);
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user