-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindServer.java
More file actions
45 lines (36 loc) · 1.37 KB
/
findServer.java
File metadata and controls
45 lines (36 loc) · 1.37 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
//This class is intended to be run as a seperate thread. It receives server pings on the LAN and updates the buttons
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class findServer implements Runnable{
//Properties
Model theModel;
String[] strServerLoad;
//Methods
public void run(){
try{
// Create a DatagramSocket to listen on a specific port
DatagramSocket inSocket = new DatagramSocket(6001);
// Buffer to store incoming data
byte[] buffer = new byte[1024];
// Create a DatagramPacket to receive the incoming data
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
while(!theModel.blnConnected){
//Receive the packet
inSocket.receive(inPacket);
//Extract and display the received message
String receivedMessage = new String(inPacket.getData(), 0, inPacket.getLength());
strServerLoad = receivedMessage.split(",");
theModel.updateServerList(strServerLoad);
}
// Close the socket
inSocket.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//Constructor
public findServer(Model theModel){
this.theModel = theModel;
}
}