-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
64 lines (52 loc) · 1.76 KB
/
Client.java
File metadata and controls
64 lines (52 loc) · 1.76 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
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Client {
static int id = 0;
public static void main(String[] args) throws IOException {
/*
* if (args.length != 2) {
* System.err.println("Usage: java SimpleClient <host name> <port number>");
* System.exit(1); }
*/
String hostName = "localhost"; // args[0];
int port = 30123; // Integer.parseInt(args[1]);
try (
Socket socket = new Socket(hostName, port);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))
) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter 1 for Job1 or 2 for Job2 or 3 to quit: ");
int job = scanner.nextInt();
scanner.nextLine();
if (job == 3) {
break; // Exit loop if user wants to stop
}
BlockingQueue<Packet> queue = new LinkedBlockingQueue<>();
new ClientToMaster ctm = new ClientToMaster(socket, queue);
Packet packet = null;
if (job == 1) {
packet = new Packet(id++, Operation.JOB_1);
} else if (job == 2) {
packet = new Packet(id++, Operation.JOB_2);
}
out.println(packet.toString()); // Send job to master
System.out.println("Sent: " + packet.toString());
// Receive a response packet
String response = in.readLine();
System.out.println("Received: " + response);
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " + hostName);
e.printStackTrace();
System.exit(1);
}
}
}