-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
73 lines (64 loc) · 3.1 KB
/
Client.java
File metadata and controls
73 lines (64 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import com.google.gson.JsonSyntaxException;
import javax.xml.crypto.Data;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import com.google.gson.Gson;
import java.util.Scanner;
public class Client {
public static void main(String[] args){
try {
InetAddress address = InetAddress.getByName("localhost");
DatagramSocket socket = new DatagramSocket();
byte[] buffer = new byte[1000000000];
byte[] bufferResponce = new byte[1000000000];
int port = 6879;
Scanner scanner = new Scanner(System.in);
String line = new String();
do{
line = scanner.nextLine();
if (line.split(" ")[0].equals("import")){
try {
String path = new String(line.split(" ")[1]);
File importedFile = new File(path);
if (!(importedFile.canRead())) throw new SecurityException("Данный файл не может быть прочитан.");
//date = new Date(importedFile.lastModified());
Scanner importedFileText = new Scanner(System.in);
importedFileText = new Scanner(importedFile);
String importedCollectionText = new String();
while (importedFileText.hasNextLine()) {
importedCollectionText = importedCollectionText.concat(importedFileText.nextLine());
}
importedFileText.close();
line = "import " + importedCollectionText;
} catch (FileNotFoundException mes) {
System.out.println(mes);
} catch (SecurityException mes) {
System.out.println(mes);
}
catch(JsonSyntaxException mes){
System.out.println("Неверный формат данных");
}
}
buffer = line.getBytes();
DatagramPacket sending = new DatagramPacket(buffer,buffer.length,address,port);
DatagramPacket response = new DatagramPacket(bufferResponce, bufferResponce.length);
socket.send(sending);
socket.setSoTimeout(3000);
try {
socket.receive(response);
String quote = new String(bufferResponce, 0, response.getLength());
System.out.println(quote);
}
catch (SocketTimeoutException e) {
// timeout exception.
System.out.println("Ответ от сервера не получен");
}
}while(!line.equalsIgnoreCase("exit"));
}catch (Exception o){
}
}
}