Answer the question
In order to leave comments, you need to log in
How to solve Java abstraction problem?
I made a program that transfers client-server files (via sockets), initially everything was in the public static void main(String[] args) method, I decided to make an abstraction (break the program into a maximum number of methods and classes). But I encountered such a problem, it reads and accepts (for example, text) the client - the server alternately, that is, it is impossible to accept or send a certain variable, etc. After I made an abstraction, it gives an error, the problem is most likely precisely in this, how to solve it? It is necessary to ask the server for the name of the file and the server must send it to the client.
This is my first such "large-scale" project. There are also a couple of questions:
1) Is it normal that I added throws IOException to almost every file (the compiler requires it)
2) Is it normal to make an abstraction?
3) I had to make a "startProgram" method, because in public static void main(String[] args) each method would have to call client.METHOD , because my methods are not static. How can you do better?
public class Client {
Socket sock = null;
//InputStream sin = null;
DataInputStream in = null;
//OutputStream sout = null;
DataOutputStream out = null;
BufferedReader reader = null;
public void makeServer() throws IOException {
sock = new Socket("127.0.0.1", 4444);
}
public String readFName() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Введите полный путь файла (например C:/image/cat.jpg)");
String s = reader.readLine();
out.writeUTF(s);
return s;
}
public void fExists() throws IOException {
if (!in.readBoolean()) {
System.out.println("Файл не существует");
System.exit(0);
}
}
public String pFile(String s) throws IOException {
String[] c = s.split("\\/"); //Разбиваем весь путь по знаку / для того чтобы взять имя файла
System.out.println("Введи папку для сохранения файла (например C:/downloads) ");
String fp = reader.readLine(); //Читаем путь куда сохранить
String p = fp + "/" + c[c.length-1]; //Берем имя файла
return p;
}
public void writeFile(String s) throws IOException {
pFile(readFName());
byte[] mybytearray = new byte[4*1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream(s);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
while (true) {
bos.write(mybytearray, 0, bytesRead);
if ((in.read(mybytearray)) == -1) {
break;
}
}
System.out.println("Файл передан");
bos.close();
sock.close();
}
public void startProgram() throws IOException {
makeServer();
readFName();
fExists();
writeFile(pFile(readFName()));
}
public static void main(String[] args) throws Exception {
Client client = new Client();
client.startProgram();
}
}
Answer the question
In order to leave comments, you need to log in
This is a decomposition and not an "abstraction" :)
I did something on sockets once, but I don't remember a damn thing :). But here's what immediately catches your eye:
First of all, divide into classes, then Client should be a separate class. Secondly, DataInputStream and DataOutputStream are not instantiated - naturally, an exception is thrown on out.writeUTF() . Thirdly, it seems to me that instead of throws it is better to use try catch blocks.
Fourth, why call readFname() three times? . Write the result of the first call to the variable .
Well, in order to get part of the string, it is not necessary to do split() , you can use substring() for example.
In addition to the fact that the code is not working, there are many architectural problems here:
1. public marked methods that are not used outside the class.
2. fExists(); does not return values. Here it would be nice to check for the existence of a file with the return of a boolean variable.
3. Validate data well immediately after entering.
4. If the application is a client server, then it would be nice to have both a client and a server, and it doesn’t matter that they work on the same computer.
5. Well, as mentioned above, you do not need to choose where to correctly handle the exception in the try - catch block, and not to throw the exception through all methods.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question