-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
136 lines (118 loc) · 3.57 KB
/
Client.java
File metadata and controls
136 lines (118 loc) · 3.57 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package socket;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Client {
private String ip;
private int port;
private int connectTimeOut;
private int sendTimeOut;
private int receiveTimeOut;
public Client() {
FileReader reader = null;
BufferedReader bufferedReader = null;
try {
reader = new FileReader("config/SocketConfig.txt");
bufferedReader = new BufferedReader(reader);
String line;
ArrayList<String> list = new ArrayList<>();
while ((line = bufferedReader.readLine()) != null) {
list.add(line.trim());
}
this.ip = list.get(0);
this.port = Integer.parseInt(list.get(1));
this.connectTimeOut = Integer.parseInt(list.get(2));
this.sendTimeOut = Integer.parseInt(list.get(3));
this.receiveTimeOut = Integer.parseInt(list.get(4));
} catch (FileNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void connect() {
SocketAddress socketAddress = new InetSocketAddress(ip, port);
Socket socket = new Socket();
PrintStream printStream;
try {
// Connect to server
socket.connect(socketAddress, connectTimeOut);
while (socket.isConnected()) {
printStream = new PrintStream(socket.getOutputStream());
String str = randomMessage();
printStream.println(str);
System.out.println(str + socket.isConnected());
Thread.sleep(1000);
}
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Không có kết nối sever");
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String randomMessage() {
// Tạo một đối tượng Random
Random random = new Random();
// Khai báo các ký tự có thể có trong chuỗi
String charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// Khai báo độ dài của chuỗi
int length = 10;
// Tạo chuỗi ngẫu nhiên
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
int randomIndex = random.nextInt(charset.length());
char randomChar = charset.charAt(randomIndex);
sb.append(randomChar);
}
return sb.toString();
}
public String getIp() {
return ip;
}
public int getPort() {
return port;
}
public int getConnectTimeOut() {
return connectTimeOut;
}
public int getSendTimeOut() {
return sendTimeOut;
}
public int getReceiveTimeOut() {
return receiveTimeOut;
}
public static void main(String[] args) {
Client client = new Client();
client.connect();
}
}