-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
56 lines (47 loc) · 2.01 KB
/
Client.java
File metadata and controls
56 lines (47 loc) · 2.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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
if(args.length < 2) {
System.out.println("use: java Client <hostname> <port>\n");
System.exit(1);
}
var hostname = args[0];
int port = Integer.parseInt(args[1]);
try (Socket s = new Socket(hostname, port)) {
BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
BufferedReader netIn = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
BufferedWriter netOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"));
for(;;) {
String mese, tipo, n_res;
System.out.println("mese ('fine per terminare'):");
if((mese = userIn.readLine()).equals("fine")) break;
System.out.println("tipo ('fine per terminare'):");
if((tipo = userIn.readLine()).equals("fine")) break;
System.out.println("risultati [ > 0] ('fine per terminare'):");
if((n_res = userIn.readLine()).equals("fine")) break;
netOut.write(mese); netOut.newLine();
netOut.write(tipo); netOut.newLine();
netOut.write(n_res); netOut.newLine();
netOut.flush();
for(;;) {
String line;
if((line = netIn.readLine()).equals("----- END REQUEST -----")) {
System.out.println(line);
break;
}
System.out.println(line);
}
}
s.close();
} catch(IOException e ) {
System.err.println(e.getMessage());
e.printStackTrace();
System.exit(2);
}
}
}