DatagramConnection connection;
try {
connection = (DatagramConnection) Connector.open("datagram://:37");
Datagram incoming = connection.newDatagram(128);
Datagram response = connection.newDatagram(4);
while (true) {
try {
connection.receive(incoming);
response.reset();
response.setAddress(incoming);
response.setData(getTime(), 0, 4);
connection.send(response);
incoming.reset();
}
catch (IOException ex) {
// As long as it's just an error on this one connection
// we can ignore it
}
}
}
catch (IOException ex) {
// If we can't open the channel, put up an Alert
Alert alert = new Alert("UDP Error");
alert.setTimeout(Alert.FOREVER);
alert.setString("Could not connect to port 37. Needs root privileges?");
Display.getDisplay(this).setCurrent(alert);
}