use of java.net.DatagramSocket in project openhab1-addons by openhab.
the class MaxCubeDiscover method discoverIp.
/**
* Automatic UDP discovery of a MAX!Cube
*
* @return if the cube is found, returns the IP address as a string. Otherwise returns null
*/
public static final String discoverIp() {
String maxCubeIP = null;
String maxCubeName = null;
String rfAddress = null;
Logger logger = LoggerFactory.getLogger(MaxCubeDiscover.class);
DatagramSocket bcReceipt = null;
DatagramSocket bcSend = null;
// Find the MaxCube using UDP broadcast
try {
bcSend = new DatagramSocket();
bcSend.setBroadcast(true);
byte[] sendData = "eQ3Max*\0**********I".getBytes();
// Broadcast the message over all the network interfaces
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback() || !networkInterface.isUp()) {
continue;
}
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress[] broadcast = new InetAddress[2];
broadcast[0] = InetAddress.getByName("224.0.0.1");
broadcast[1] = interfaceAddress.getBroadcast();
for (InetAddress bc : broadcast) {
// Send the broadcast package!
if (bc != null) {
try {
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, bc, 23272);
bcSend.send(sendPacket);
} catch (IOException e) {
logger.debug("IO error during MAX! Cube discovery: {}", e.getMessage());
} catch (Exception e) {
logger.debug(e.getMessage());
logger.debug(Utils.getStackTrace(e));
}
logger.trace("Request packet sent to: {} Interface: {}", bc.getHostAddress(), networkInterface.getDisplayName());
}
}
}
}
logger.trace("Done looping over all network interfaces. Now waiting for a reply!");
bcSend.close();
bcReceipt = new DatagramSocket(23272);
bcReceipt.setReuseAddress(true);
// Wait for a response
byte[] recvBuf = new byte[15000];
DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length);
bcReceipt.receive(receivePacket);
// We have a response
logger.trace("Broadcast response from server: {}", receivePacket.getAddress());
// Check if the message is correct
String message = new String(receivePacket.getData()).trim();
if (message.startsWith("eQ3Max")) {
maxCubeIP = receivePacket.getAddress().getHostAddress();
maxCubeName = message.substring(0, 8);
rfAddress = message.substring(8, 18);
logger.debug("Found at: {}", maxCubeIP);
logger.debug("Name : {}", maxCubeName);
logger.debug("Serial : {}", rfAddress);
logger.trace("Message : {}", message);
} else {
logger.info("No Max!Cube gateway found on network");
}
} catch (IOException e) {
logger.debug("IO error during MAX! Cube discovery: {}", e.getMessage());
} catch (Exception e) {
logger.debug(e.getMessage());
logger.debug(Utils.getStackTrace(e));
} finally {
try {
if (bcReceipt != null) {
bcReceipt.close();
}
} catch (Exception e) {
logger.debug(e.toString());
}
try {
if (bcSend != null) {
bcSend.close();
}
} catch (Exception e) {
logger.debug(e.toString());
}
}
return maxCubeIP;
}
use of java.net.DatagramSocket in project opennms by OpenNMS.
the class TrivialTimeClient method connect.
@Override
public void connect(InetAddress address, int port, int timeout) throws IOException, Exception {
// Validate Protocol
if (!isTcp() && !isUdp()) {
throw new IllegalArgumentException("Unsupported protocol, only TCP and UDP currently supported");
} else if (isUdp()) {
LOG.warn("UDP support is largely untested");
}
// Initialize Socket
if (isTcp()) {
tcpSocket = new Socket();
tcpSocket.connect(new InetSocketAddress(address, port), timeout);
tcpSocket.setSoTimeout(timeout);
} else {
udpSocket = new DatagramSocket();
udpSocket.setSoTimeout(timeout);
// Empty datagram per RFC868
udpPacket = new DatagramPacket(new byte[] {}, 0, address, port);
}
LOG.debug("Connected to host: {} on {} port: {}", address, protocol.toUpperCase(), port);
}
use of java.net.DatagramSocket in project opennms by OpenNMS.
the class SelectorTrackerTest method test.
@Test
public void test() throws IOException {
Selector selector = Selector.open();
assertTrue(selector.isOpen());
selector.close();
assertFalse(selector.isOpen());
DatagramChannel c = DatagramChannel.open();
DatagramSocket s = c.socket();
s.setSoTimeout(1000);
byte[] buf = new byte[1024];
DatagramPacket p = new DatagramPacket(buf, 1024, InetAddress.getLocalHost(), 7);
s.send(p);
}
use of java.net.DatagramSocket in project android_frameworks_base by ResurrectionRemix.
the class BandwidthEnforcementTestService method testDns.
/**
* Tests dns query.
* @return true if it was able to connect, false otherwise.
*/
public static boolean testDns() {
try {
final DatagramSocket socket = new DatagramSocket();
try {
socket.setSoTimeout(10000);
final byte[] query = buildDnsQuery("www", "android", "com");
final DatagramPacket queryPacket = new DatagramPacket(query, query.length, InetAddress.parseNumericAddress("8.8.8.8"), 53);
socket.send(queryPacket);
final byte[] reply = new byte[query.length];
final DatagramPacket replyPacket = new DatagramPacket(reply, reply.length);
socket.receive(replyPacket);
return true;
} finally {
socket.close();
}
} catch (IOException e) {
Log.d(TAG, "error: " + e);
}
return false;
}
use of java.net.DatagramSocket in project CloudStack-archive by CloudStack-extras.
the class DhcpProtocolParserServer method run.
public void run() {
while (_running) {
try {
DatagramSocket dhcpSocket = new DatagramSocket(dhcpServerPort, InetAddress.getByAddress(new byte[] { 0, 0, 0, 0 }));
dhcpSocket.setBroadcast(true);
while (true) {
byte[] buf = new byte[bufferSize];
DatagramPacket dgp = new DatagramPacket(buf, buf.length);
dhcpSocket.receive(dgp);
// _executor.execute(new DhcpPacketParser(buf));
}
} catch (IOException e) {
s_logger.debug(e.getMessage());
}
}
}
Aggregations