Example usage for java.net DatagramPacket DatagramPacket

List of usage examples for java.net DatagramPacket DatagramPacket

Introduction

In this page you can find the example usage for java.net DatagramPacket DatagramPacket.

Prototype

public DatagramPacket(byte buf[], int length, InetAddress address, int port) 

Source Link

Document

Constructs a datagram packet for sending packets of length length to the specified port number on the specified host.

Usage

From source file:org.rifidi.edge.adapter.csl.util.CslRfidTagServer.java

private boolean setMode(ReaderMode mode) {
    try {//from   ww  w  .java 2s. co m

        InetAddress IPInet = InetAddress.getByName(this.IPAddress);

        // Set low-level API mode
        String cmd = "80" + byteArrayToHexString(IPInet.getAddress(), IPInet.getAddress().length)
                + (mode == ReaderMode.lowLevel ? "000C" : "000D");
        logger.info("Set low-level API with command 0x" + cmd.toString());

        DatagramPacket sendPacket = new DatagramPacket(hexStringToByteArray(cmd),
                hexStringToByteArray(cmd).length, IPInet, 3041);
        byte[] receiveData = new byte[4];
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.send(sendPacket);
        clientSocket.setSoTimeout(2000);
        clientSocket.receive(receivePacket);
        logger.info("Return: " + byteArrayToHexString(receivePacket.getData(), receivePacket.getData().length));

        if (byteArrayToHexString(receivePacket.getData(), receivePacket.getData().length).startsWith("810100"))
            return true;
        else
            return false;
    } catch (UnknownHostException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
}

From source file:org.rifidi.edge.adapter.csl.util.CslRfidTagServer.java

private ReaderMode checkMode() {
    try {/*  w  w w.  j a  va2  s. com*/

        InetAddress IPInet = InetAddress.getByName(this.IPAddress);

        // Set low-level API mode
        String cmd = "80" + byteArrayToHexString(IPInet.getAddress(), IPInet.getAddress().length) + "000E";
        logger.info("Check mode with command 0x" + cmd.toString());

        DatagramPacket sendPacket = new DatagramPacket(hexStringToByteArray(cmd),
                hexStringToByteArray(cmd).length, IPInet, 3041);
        byte[] receiveData = new byte[5];
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.send(sendPacket);
        clientSocket.setSoTimeout(2000);
        clientSocket.receive(receivePacket);
        logger.info(String.format(
                "Return: " + byteArrayToHexString(receivePacket.getData(), receivePacket.getData().length)));

        if (byteArrayToHexString(receivePacket.getData(), receivePacket.getData().length)
                .startsWith("8101010E00"))
            return ReaderMode.highLevel;
        else if (byteArrayToHexString(receivePacket.getData(), receivePacket.getData().length)
                .startsWith("8101010E01"))
            return ReaderMode.lowLevel;
        else
            return null;
    } catch (UnknownHostException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}

From source file:org.tinymediamanager.core.Utils.java

/**
 * Sends a wake-on-lan packet for specified MAC address across subnet
 * //from  w ww . jav  a  2 s .c  om
 * @param macAddr
 *          the mac address to 'wake up'
 */
public static final void sendWakeOnLanPacket(String macAddr) {
    // Broadcast IP address
    final String IP = "255.255.255.255";
    final int port = 7;

    try {
        final byte[] MACBYTE = new byte[6];
        final String[] hex = macAddr.split("(\\:|\\-)");

        for (int i = 0; i < 6; i++) {
            MACBYTE[i] = (byte) Integer.parseInt(hex[i], 16);
        }
        final byte[] bytes = new byte[6 + 16 * MACBYTE.length];
        for (int i = 0; i < 6; i++) {
            bytes[i] = (byte) 0xff;
        }
        for (int i = 6; i < bytes.length; i += MACBYTE.length) {
            System.arraycopy(MACBYTE, 0, bytes, i, MACBYTE.length);
        }

        // Send UDP packet here
        final InetAddress address = InetAddress.getByName(IP);
        final DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, port);
        final DatagramSocket socket = new DatagramSocket();
        socket.send(packet);
        socket.close();

        LOGGER.info("Sent WOL packet to " + macAddr);
    } catch (final Exception e) {
        LOGGER.error("Error sending WOL packet to " + macAddr, e);
    }
}

From source file:serverTools.java

private String callOvnmanager(String node, String action) {
    String result = "";
    DatagramSocket socket = null;
    int serverPort = 9999;
    DatagramPacket packet2Send;/*  w w  w.ja  va 2  s  .  c  om*/
    DatagramPacket receivedPacket;
    InetAddress theServerAddress;
    byte[] outBuffer;
    byte[] inBuffer;
    //inBuffer = new byte[8192];
    //outBuffer = new byte[8192];
    inBuffer = new byte[65536];
    outBuffer = new byte[8192];

    try {
        HttpSession session = RuntimeAccess.getInstance().getSession();
        String sessionUser = (String) session.getAttribute("User");
        if (sessionUser == null) {
            sessionUser = "administrator";
        }

        JSONObject joAction = new JSONObject(action);
        JSONObject joCmd = new JSONObject();
        joCmd.put("sender", sessionUser);
        joCmd.put("target", "NODE");
        joCmd.put("node", node);
        joCmd.put("action", joAction);
        String output = joCmd.toString();

        socket = new DatagramSocket();
        // set timeout
        String actionName = joAction.get("name").toString();
        if (actionName.equals("add")) {
            socket.setSoTimeout(90000);
        } else if (actionName.equals("connect")) {
            socket.setSoTimeout(60000);
        } else {
            socket.setSoTimeout(60000);
        }
        InetAddress serverInet = InetAddress.getByName("localhost");
        socket.connect(serverInet, serverPort);
        outBuffer = output.getBytes();
        packet2Send = new DatagramPacket(outBuffer, outBuffer.length, serverInet, serverPort);
        receivedPacket = new DatagramPacket(inBuffer, inBuffer.length);

        try {
            // send the data
            socket.send(packet2Send);
            // receive reply            
            socket.receive(receivedPacket);
            // the server reply is...
            result = new String(receivedPacket.getData(), 0, receivedPacket.getLength());
            session.setAttribute("LastActive", System.currentTimeMillis());
        } catch (Exception excep) {
            String msg = excep.getMessage();
            //String msg = excep.toString();
            joCmd.remove("action");
            joAction.put("result", "Error:" + msg);
            joCmd.put("action", joAction);
            result = joCmd.toString();
        }
        socket.close();

    } catch (Exception e) {
        log(ERROR, "callOvnmanager", e);
        return e.toString();
    }
    return result;
}

From source file:vmTools.java

private String callOvnmanager(String node, String action) {
    String result = "";
    DatagramSocket socket = null;
    int serverPort = 9999;
    DatagramPacket packet2Send;// w  ww . j av a2  s  .  com
    DatagramPacket receivedPacket;
    InetAddress theServerAddress;
    byte[] outBuffer;
    byte[] inBuffer;
    inBuffer = new byte[65536];
    outBuffer = new byte[8192];
    /*
    if (this.user == null) {
    this.user = "admin";
    }*/

    try {
        HttpSession session = RuntimeAccess.getInstance().getSession();

        String sessionUser = (String) session.getAttribute("User");
        if (sessionUser == null) {
            sessionUser = "administrator";
        }

        JSONObject joCmd = new JSONObject();
        JSONObject joAction = new JSONObject(action);
        joCmd.put("sender", sessionUser);
        joCmd.put("target", "VM");
        joCmd.put("node", node);
        joCmd.put("action", joAction);
        String output = joCmd.toString();

        socket = new DatagramSocket();
        String actionName = joAction.get("name").toString();
        /*if (actionName.equals("migrate")) {
        socket.setSoTimeout(600000);
        } else {
        socket.setSoTimeout(60000);
        }*/
        socket.setSoTimeout(60000);

        InetAddress serverInet = InetAddress.getByName("localhost");
        socket.connect(serverInet, serverPort);
        outBuffer = output.getBytes();
        packet2Send = new DatagramPacket(outBuffer, outBuffer.length, serverInet, serverPort);

        try {
            // send the data
            socket.send(packet2Send);
            receivedPacket = new DatagramPacket(inBuffer, inBuffer.length);
            socket.receive(receivedPacket);
            // the server response is...
            result = new String(receivedPacket.getData(), 0, receivedPacket.getLength());
            session.setAttribute("LastActive", System.currentTimeMillis());
        } catch (Exception excep) {
            String msg = excep.getMessage();
            //String msg = excep.toString();
            joCmd.remove("action");
            joAction.put("result", "Error:" + msg);
            joCmd.put("action", joAction);
            result = joCmd.toString();
        }
        socket.close();

    } catch (Exception e) {
        log(ERROR, "callOvnmanager", e);
        return e.toString();
    }
    return result;
}

From source file:vmXmlFile.java

private String callOvnmanager(String node, String action) {
    String result = "";
    DatagramSocket socket = null;
    int serverPort = 9999;
    DatagramPacket packet2Send;//from   w  w w .  j  a va 2 s. c o  m
    DatagramPacket receivedPacket;
    InetAddress theServerAddress;
    byte[] outBuffer;
    byte[] inBuffer;
    inBuffer = new byte[65536];
    outBuffer = new byte[8192];

    try {
        HttpSession session = RuntimeAccess.getInstance().getSession();
        String sessionUser = (String) session.getAttribute("User");
        if (sessionUser == null) {
            sessionUser = "administrator";
        }
        JSONObject joCmd = new JSONObject();
        JSONObject joAction = new JSONObject(action);
        joCmd.put("sender", sessionUser);
        joCmd.put("target", "VM");
        joCmd.put("node", node);
        joCmd.put("action", joAction);
        String output = joCmd.toString();

        socket = new DatagramSocket();
        InetAddress serverInet = InetAddress.getByName("localhost");
        socket.connect(serverInet, serverPort);
        outBuffer = output.getBytes();
        packet2Send = new DatagramPacket(outBuffer, outBuffer.length, serverInet, serverPort);
        // send the data
        socket.send(packet2Send);

        receivedPacket = new DatagramPacket(inBuffer, inBuffer.length);
        socket.receive(receivedPacket);

        // the server response is...
        result = new String(receivedPacket.getData(), 0, receivedPacket.getLength());
        session.setAttribute("LastActive", System.currentTimeMillis());
        socket.close();

    } catch (Exception e) {
        log(ERROR, "callOvnmanager", e);
        return e.toString();
    }
    return result;
}