-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerThread.java
45 lines (34 loc) · 963 Bytes
/
ServerThread.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
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
public class ServerThread extends Thread {
DatagramSocket socket;
String msg;
byte[] msgByte;
InetAddress host;
int serverSocket;
DatagramPacket request;
public ServerThread(){
init();
}
public void init(){
try{
socket = new DatagramSocket();
host = InetAddress.getByName("localhost");
//can be between 5800 and 5810
serverSocket = 5803;
}catch(Exception e){
System.out.println("Error: + " + e);
}
}
public void sendInfo(boolean targetFound, double angle, double distance){
msg = String.valueOf(targetFound) + ";" + String.valueOf(angle) + ";" + String.valueOf(distance);
msgByte = msg.getBytes();
try{
request = new DatagramPacket(msgByte, msgByte.length, host, serverSocket);
socket.send(request);
}catch(Exception e){
System.out.println("Error: " + e);
}
}