-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
151 lines (126 loc) · 4.01 KB
/
Client.java
File metadata and controls
151 lines (126 loc) · 4.01 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package order;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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;
private String nameClient;
private ArrayList<Product> products;
public Client(String name, ArrayList<Product> products) {
this.nameClient = name;
this.products = products;
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);
}
}
}
}
@SuppressWarnings("resource")
public void connect() {
SocketAddress socketAddress = new InetSocketAddress(ip, port);
try (Socket socket = new Socket()) {
socket.connect(socketAddress, connectTimeOut);
// Sending to server
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
Order order = new Order(products, nameClient);
objectOutputStream.writeObject(order);
// ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
// Message message = (Message) objectInputStream.readObject();
// if (message != null) {
// System.out.println("Receive from server: " + message.getMSG());
// }
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Lỗi2");
// } catch (ClassNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
}
}
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 ArrayList<Product> getProduct() {
return products;
}
public void setProducts(ArrayList<Product> products) {
this.products = products;
}
public String getNameClient() {
return nameClient;
}
public static void main(String[] args) {
ArrayList<Product> products = new ArrayList<>();
products.add(new Product("SamPham1", "NguoiBan1", 20000));
products.add(new Product("SamPham2", "NguoiBan1", 25000));
ExecutorService pool = Executors.newFixedThreadPool(500);
for (int i = 0; i < 1000; i++) {
pool.submit(new MultiThreadClient(i) {
public void run() {
Client client = new Client("Client" + i, products);
client.connect();
};
});
}
}
}