-
Notifications
You must be signed in to change notification settings - Fork 0
Java02. ДЗ 03, Кравченко Юрий, подгруппа 2 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .idea/ | ||
| target/ | ||
| /*.iml |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package ru.spbau.mit.hw3; | ||
|
|
||
| import java.io.DataInputStream; | ||
| import java.io.DataOutputStream; | ||
| import java.io.IOException; | ||
| import java.net.Socket; | ||
|
|
||
| /** | ||
| * Created by YuryKravchenko on 14/03/16. | ||
| */ | ||
| public class Client{ | ||
|
|
||
| Socket socket; | ||
| DataInputStream in; | ||
| DataOutputStream out; | ||
|
|
||
| public Client() throws IOException { | ||
| socket = new Socket("127.0.0.1", 12345); | ||
| in = new DataInputStream(socket.getInputStream()); | ||
| out = new DataOutputStream(socket.getOutputStream()); | ||
| } | ||
|
|
||
| public String List(String path) throws IOException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. методы именуются с маленкой буквы, -0.5 |
||
| out.writeInt(1); | ||
| out.writeUTF(path); | ||
| int count = in.readInt(); | ||
| StringBuilder builder = new StringBuilder(); | ||
| for (int i = 0; i < count; ++i) { | ||
| builder.append(in.readUTF()).append(' ').append(in.readBoolean()).append('\n'); | ||
| } | ||
| return builder.toString(); | ||
| } | ||
|
|
||
| public String Get(String s) throws IOException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. методы именуются с маленкой буквы |
||
| out.writeInt(2); | ||
| out.writeUTF(s); | ||
| Long size = in.readLong(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. long |
||
| byte[] file = new byte[size.intValue()]; | ||
| in.read(file, 0, size.intValue()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. по этим двум строчкам аналогичные замечания: файл может не влезть в память, read может прочитать не все |
||
| return file.toString(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. файл должен быть сохранен, в консоли ему делать нечего ) |
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package ru.spbau.mit.hw3; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Scanner; | ||
|
|
||
| /** | ||
| * Created by YuryKravchenko on 14/03/16. | ||
| */ | ||
| public class MainClient { | ||
| public static void main(String[] args) { | ||
| Scanner in = new Scanner(System.in); | ||
| Client client = null; | ||
| while (true) { | ||
| String cmd = in.nextLine(); | ||
| switch (cmd) { | ||
| case "connect": | ||
| try { | ||
| client = new Client(); | ||
| } catch (IOException e) { | ||
| System.out.println("starting client failed, try again"); | ||
| } | ||
| break; | ||
| case "executeList": | ||
| cmd = in.nextLine(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cmd -> path |
||
| try { | ||
| System.out.println(client.List(cmd)); | ||
| } catch (IOException e) { | ||
| System.out.println("connection failed"); | ||
| } catch (NullPointerException e) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это надо проверять перед исполнением запроса |
||
| System.out.println("no connection"); | ||
| } | ||
| break; | ||
| case "executeGet": | ||
| cmd = in.nextLine(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cmd -> path |
||
| try { | ||
| System.out.println(client.Get(cmd)); | ||
| } catch (IOException e) { | ||
| System.out.println("connection failed"); | ||
| } catch (NullPointerException e) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это надо проверять перед исполнением запроса |
||
| System.out.println("no connection"); | ||
| } | ||
| break; | ||
| case "disconnect": | ||
| return; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. клиент должен закрывать подключение, -0.5 |
||
| } | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. делай также, как с сервером, запускай клиент сразу, а потом в цикле обрабатывай команды |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package ru.spbau.mit.hw3; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Scanner; | ||
|
|
||
| /** | ||
| * Created by YuryKravchenko on 14/03/16. | ||
| */ | ||
| public class MainServer { | ||
| public static void main(String[] args) { | ||
| Scanner in = new Scanner(System.in); | ||
| while (true) { | ||
| String cmd = in.nextLine(); | ||
| try { | ||
| switch (cmd) { | ||
| case "start": | ||
| Thread newThread = new Thread(new Server()); | ||
| newThread.setDaemon(true); | ||
| newThread.start(); | ||
| break; | ||
| case "stop": | ||
| return; | ||
| } | ||
| } catch (IOException e) { | ||
| System.out.println("starting server failed, try again"); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. больше одного сервера все равно не запустить, можно было запустить сервер, а потом ждать команды stop |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package ru.spbau.mit.hw3; | ||
|
|
||
| import java.io.*; | ||
| import java.net.ServerSocket; | ||
| import java.net.Socket; | ||
|
|
||
| /** | ||
| * Created by YuryKravchenko on 14/03/16. | ||
| */ | ||
| public class Server implements Runnable { | ||
|
|
||
| ServerSocket socket; | ||
| DataInputStream in; | ||
| DataOutputStream out; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. модификаторы, -1 |
||
|
|
||
| public Server() throws IOException { | ||
| socket = new ServerSocket(12345); | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| try { | ||
| Socket socket = this.socket.accept(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. в единицу времени приложение может обрабатывать только один запрос, было бы лучше, если бы это было не так. Здесь исправлять не надо, в следующем дз надо к этому стремиться |
||
| in = new DataInputStream(socket.getInputStream()); | ||
| out = new DataOutputStream(socket.getOutputStream()); | ||
| } catch (IOException e) { | ||
| System.out.println("connection error"); | ||
| return; | ||
| } | ||
| try { | ||
| while (true) { | ||
| int cmd = in.readInt(); | ||
| String path = in.readUTF(); | ||
| switch (cmd) { | ||
| case 1: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. обработка каждого запроса должна быть в отдельном методе |
||
| if (!new File(path).exists()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new FIle(path) должно быть выделено в отдельную переменную, оно используется 6!!! раз, -1 |
||
| out.writeInt(0); | ||
| break; | ||
| } | ||
| out.writeInt(new File(path).listFiles().length); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. надо listFiles() проверять на null, см документацию |
||
| for (File f : new File(path).listFiles()) { | ||
| out.writeUTF(f.getName()); | ||
| out.writeBoolean(f.isDirectory()); | ||
| } | ||
| out.flush(); | ||
| break; | ||
| case 2: | ||
| if (!new File(path).exists() || new File(path).isDirectory()) { | ||
| out.writeInt(0); | ||
| break; | ||
| } | ||
| DataInputStream file = new DataInputStream(new FileInputStream(path)); | ||
| Long length = new File(path).length(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. long, -0.5 |
||
| out.writeLong(length); | ||
| byte[] b = new byte[length.intValue()]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. файл может не влезть в память, -0.5 |
||
| file.read(b); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. см документацию read, туда могут записаться не все данные, -0.5 |
||
| out.write(b); | ||
| out.flush(); | ||
| break; | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| System.out.println("connection failed"); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
модификаторы