-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
68 lines (49 loc) · 2.25 KB
/
Client.java
File metadata and controls
68 lines (49 loc) · 2.25 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
import java.io.IOException;
import java.net.Socket;
public class Client {
/**
Initialise the client and make it connect to the server, create a thread for writing user response, and continue
waiting for server response
**/
public PacketTransceiver packetTransceiver;
public PacketReceiver packetReceiver;
private final String serverAddress;
private final int portNumber;
public static final Object lock = new Object(); // Defining an object that is going to allow me to use the wait(). Wait() requires synchronized to only allowed one thread to use the wait on another thread
public Client(String serverAddress, int portNumber){
this.serverAddress = serverAddress;
this.portNumber = portNumber;
}
/**
Creates the client socket that connects to the server. Client closes when its PacketReceiver status
returns, "Not Operational". It checks for this status message evert fifteen seconds in a while loop.
**/
public void run(){
String[] args = new String[3];
try(Socket socket = new Socket(serverAddress, portNumber)){
// Defining an object that is going to allow me to use the wait(). Wait() requires synchronized to only allow one thread to use the wait on another thread
this.packetTransceiver = new PacketTransceiver(packetReceiver, socket, args);
Thread packetTransceiverThread = new Thread(packetTransceiver);
packetTransceiverThread.start();
this.packetReceiver = new PacketReceiver(this.packetTransceiver, socket);
Thread packetReceiverThread = new Thread(packetReceiver);
packetReceiverThread.start();
try {
synchronized (lock){
lock.wait();
}
} catch(InterruptedException e){
System.out.println(e.getMessage());
}
System.out.println("Closing out of this client");
} catch (IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args){
String serverAddress = "127.0.0.1";
int portNumber = 8080;
Client client = new Client(serverAddress, portNumber);
client.run();
}
}