-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroadcastIP.java
More file actions
49 lines (39 loc) · 1.33 KB
/
broadcastIP.java
File metadata and controls
49 lines (39 loc) · 1.33 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
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class broadcastIP implements Runnable{
//Properties
Model theModel;
//Methods
public void run(){
try{
//Create a DatagramSocket
DatagramSocket outSocket = new DatagramSocket();
//Set the broadcast address
InetAddress broadcastAddress = InetAddress.getByName("255.255.255.255");
while(!theModel.blnGameStarted){
//Message to be sent
String message = theModel.getStatus();
byte[] data = message.getBytes();
//Create a DatagramPacket with the message, length, and broadcast address
DatagramPacket outPacket = new DatagramPacket(data, data.length, broadcastAddress, 6001);
//Send the packet
outSocket.send(outPacket);
System.out.println("Broadcasted IP");
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
}
}
//Close the socket
outSocket.close();
}
catch(Exception e){
}
}
//Constructor
public broadcastIP(Model theModel){
this.theModel = theModel;
}
}