-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame_Client.java
217 lines (201 loc) · 7.79 KB
/
Game_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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class Game_Client {
public static boolean isInteger(String s) {
boolean isValidInteger = false;
try {
Integer.parseInt(s);
// s is a valid integer
isValidInteger = true;
}
catch (NumberFormatException ex) {
// s is not an integer
}
return isValidInteger;
}
public static void main(String[] args) throws Exception {
String multicast_address = "";
int port_number = 0;
int player_number = 0;
boolean isDone = false;
if ((args.length != 2)) { // Test for correct # of args
throw new IllegalArgumentException("Parameter(s): <Server> <Port>");
}
DatagramSocket clientSocket = new DatagramSocket(); // for network
BufferedReader fromKeyboard = new BufferedReader(new InputStreamReader(System.in)); // from keyboard
InetAddress serverAddress = InetAddress.getByName(args[0]); // Server address
int servPort = Integer.parseInt(args[1]); // get port number
String clientInput; // from user
System.out.println("Please enter your name to get started and enter done when finished playing");
while (multicast_address=="" && port_number==0 && player_number == 0) {
clientInput = fromKeyboard.readLine();
if(clientInput.equals("done")) {
isDone = true;
}
byte[] buf = clientInput.getBytes(); // byte array for data to SEND
// create a UDP (datagram) packet TO send to server
DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddress, servPort);
clientSocket.send(packet); // now send it
byte[] rbuf = new byte[256]; // new byte array to RECEIVE from
// server
packet = new DatagramPacket(rbuf, rbuf.length); // UDP packet to
clientSocket.receive(packet); // receive it
ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData(), 0, packet.getLength());
BufferedReader reader = new BufferedReader(new InputStreamReader(bin));
multicast_address = reader.readLine();
port_number = Integer.parseInt(reader.readLine());
player_number = Integer.parseInt(reader.readLine());
}
clientSocket.close();
while(true){
if(isDone==true) {
DatagramSocket cs = new DatagramSocket(); //for network
InetAddress sa = InetAddress.getByName(args[0]); // Server address
int sp = Integer.parseInt(args[1]); //get port number
String cin; //from user
// String echoString; //echo from server
System.out.println("Thank you for playing");
cin = "Done";
byte[] buf=cin.getBytes(); //byte array for data to SEND to server
//create a UDP (datagram) packet TO send to server
DatagramPacket packet = new DatagramPacket(buf,buf.length, sa, sp);
cs.send(packet); //now send it
}
else if(player_number == 1) {
String IPadrress = multicast_address;
System.setProperty("java.net.preferIPv4Stack", "true");
@SuppressWarnings("resource")
MulticastSocket socket = new MulticastSocket(port_number);
int ttl = 64;
socket.setTimeToLive(ttl);
InetAddress group = InetAddress.getByName(IPadrress);
InetAddress IP=InetAddress.getLocalHost();
socket.setInterface(IP);
group = InetAddress.getByName(IPadrress);
socket.joinGroup(group);
BufferedReader fromKeyboard2 = new BufferedReader(new InputStreamReader(System.in));
String output = "1" + System.lineSeparator();
int num = 0;
System.out.println("Choose a number between 1 and 50");
String in = fromKeyboard2.readLine();
System.out.println("Wait for player 2 to guess correctly... ");
if(isInteger(in)){
num = Integer.parseInt(in);
}
else if(in.equals("exit")){
isDone = true;
break;
}
else {
output += "enter valid integer";
}
while(num!=0){
//process a datagram
output = "1" + System.lineSeparator();
byte[] rbuf = new byte[256];
byte[] sbuf = new byte [256];
boolean toBreak = false;
DatagramPacket packet = new DatagramPacket(rbuf, rbuf.length);
socket.receive(packet);
// Obtain a byte input stream to really read the UDP packet
ByteArrayInputStream bin = new ByteArrayInputStream (packet.getData(), 0, packet.getLength() );
// Connect a reader for easier access
BufferedReader reader = new BufferedReader (new InputStreamReader ( bin ) );
String line = reader.readLine(); //we can read a string, since Reader has a method for char reading
if(line.equals("2")){
line = reader.readLine();
//If it is not an integer, it will process as a name
if (isInteger(line)) {
int guess = Integer.parseInt(line);
if(num > guess) {
output += "Too low";
}
else if (num < guess) {
output += "Too high";
}
else {
output += "Correct!";
player_number = 2;
toBreak = true;
}
}
else if(line.equals("exit")) {
isDone=true;
break;
}
else {
output += "Enter a valid integer. You entered: " + line;
}
InetAddress address = InetAddress.getByName(multicast_address);
sbuf = output.getBytes();
//create a datagram packet
DatagramPacket sendPacket = new DatagramPacket(sbuf, sbuf.length, address, port_number);
socket.send(sendPacket);
if(toBreak){
break;
}
}
}
}
else if(player_number==2){ //guesser
String IPadrress = multicast_address;
System.setProperty("java.net.preferIPv4Stack", "true");
@SuppressWarnings("resource")
MulticastSocket socket = new MulticastSocket(port_number);
int ttl = 64;
socket.setTimeToLive(ttl);
InetAddress group = InetAddress.getByName(IPadrress);
InetAddress IP=InetAddress.getLocalHost();
socket.setInterface(IP);
group = InetAddress.getByName(IPadrress);
socket.joinGroup(group);
BufferedReader fromKeyboard1 = new BufferedReader(new InputStreamReader(System.in));
String clientInput1; //from user
String echoString; //echo from server
System.out.println("Guess a number between 1 and 50");
boolean isCorrect = false;
while(!isCorrect && !isDone) {
boolean isRead = false;
clientInput1 = "2" + System.lineSeparator() + fromKeyboard1.readLine();
if (clientInput1.equals("exit")){ //user want to stop having fun with the server
isDone=true;
break;
}
byte[] buf=clientInput1.getBytes(); //byte array for data to SEND to server
//create a UDP (datagram) packet TO send to server
DatagramPacket packet = new DatagramPacket(buf,buf.length, group, port_number);
socket.send(packet); //now send it
while(isRead==false){
byte[] rbuf = new byte[256]; //new byte array to RECEIVE from server
packet = new DatagramPacket(rbuf,rbuf.length); //UDP packet to receive FROM server
socket.receive(packet); //receive it
ByteArrayInputStream bin = new ByteArrayInputStream (packet.getData(), 0, packet.getLength() );
BufferedReader reader = new BufferedReader (new InputStreamReader ( bin ) );
String line = reader.readLine(); //we can read a string, since Reader has a method for char reading
if(line.equals("1")){ //coming from player 1
isRead=true;
line = reader.readLine();
if(line.equals("Correct!")){
System.out.println(line);
player_number=1;
isCorrect=true;
break;
}else if(line.equals("exit")) {
isDone=true;
break;
}
System.out.println(line);
}
}
}
socket.close();
}
}
}
}