-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerReceiver.java~
More file actions
52 lines (47 loc) · 1.59 KB
/
Copy pathServerReceiver.java~
File metadata and controls
52 lines (47 loc) · 1.59 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
import java.net.*;
import java.util.*;
public class ServerReceiver extends Thread{
ServerSocket ssocket;
Vector<MyConnection> connection_vector = new Vector<MyConnection>();
int total = 0, rand_winner = 0;
String winnner, bets[2];
public ServerReceiver(ServerSocket ssocket){
this.ssocket = ssocket;
}
public void announce(String msg){
for(int i=0; i<connection_vector.size(); i++){
MyConnection mc = connection_vector.get(i);
try{
mc.sendMessage(msg); Thread.sleep(100);
}catch(Exception e){
e.printStackTrace();
}
}
}
public void startGame(){
this.announce("GAME START!");
rand_winner = 1 + (int)(Math.random() * 4);
String winner = Integer.toString(rand_winner);
this.announce(winner); //System.out.println("Frog winner: " + winner);
this.announce(bets[0]);
this.announce(bets[1]);
}
public void run(){
try{
while (true) {
System.out.println("Server: Waiting for connections...");
Socket socket = ssocket.accept();
MyConnection connect = new MyConnection(socket);
connection_vector.add(connect);
System.out.println("Server: " + socket.getInetAddress() +
" connected!");
//System.out.println(connection_vector.toString());
bets[total] = connect.getMessage();
total = total + 1;
if(total == 2) startGame();
}
}catch(Exception e){
e.printStackTrace();
}
}
}