-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameThread.java
More file actions
53 lines (48 loc) · 1.58 KB
/
gameThread.java
File metadata and controls
53 lines (48 loc) · 1.58 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
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class gameThread implements Runnable {
String multicast;
int port;
// peerUtilites peerU;
public gameThread(int port, String multicast){
this.port = port;
this.multicast = multicast;
// this.peerU = peerU;
}
public void run(){
try{
while(true) {
String IPadrress = multicast;
System.setProperty("java.net.preferIPv4Stack", "true");
@SuppressWarnings("resource")
MulticastSocket socket = new MulticastSocket(port);
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);
byte[] rbuf = new byte[256];
DatagramPacket packet = new DatagramPacket(rbuf,rbuf.length);
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();
if (line.equals("2")) {
System.out.println("PLAYER 2: " + reader.readLine());
}
else{ //can't figure pout why it wont read player 1
System.out.println("PLAYER 1: " + reader.readLine());
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}