-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClient.java
70 lines (57 loc) · 1.89 KB
/
Client.java
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
/********************************************************
* This class establishes a connection to server
* and transfers data between server and client side gui
********************************************************/
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
private Socket socket;
private Boolean isConnected = false;
ObjectOutputStream oos;
ObjectInputStream ois;
public Client() throws UnknownHostException, ClassNotFoundException {
try {
this.socket = new Socket(ClueGameConstants.IP, 55332);
this.isConnected = true;
} catch (IOException e) {
try {
if(this.socket != null)
this.socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
try {
ois = new ObjectInputStream(socket.getInputStream());
oos = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e2) {
System.out.println("Error openining Input or Output Stream in Client.java");
e2.printStackTrace();
}
}
synchronized public Message getMessage() throws IOException, EOFException, ClassNotFoundException {
Message msg = (Message) ois.readUnshared();
return msg;
}
synchronized public void send(Message msg) throws IOException {
oos.writeUnshared(msg);
}
public Boolean isPlayerConnected(){
return this.isConnected;
}
public void closeConnection() {
try {
socket.close();
oos.close();
ois.close();
} catch (IOException e) {
System.out.println("Error closing connection in Client.java");
e.printStackTrace();
}
}
} // Client Class