List of usage examples for java.net DatagramSocket receive
public synchronized void receive(DatagramPacket p) throws IOException
From source file:vmXmlFile.java
private String callOvnmanager(String node, String action) { String result = ""; DatagramSocket socket = null; int serverPort = 9999; DatagramPacket packet2Send;//from w ww .j a v a 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; }
From source file:Network.Network.java
private String discoverUDPServer() { DatagramSocket c; String foundIP = null;//from ww w.j ava 2 s .com // Find the server using UDP broadcast try { //Open a random port to send the package c = new DatagramSocket(); c.setBroadcast(true); byte[] sendData = "DISCOVER_BATTLESHIPSERVER_REQUEST".getBytes(); //Try the 255.255.255.255 first try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("255.255.255.255"), 8888); c.send(sendPacket); System.out.println(getClass().getName() + ">>> Request packet sent to: 255.255.255.255 (DEFAULT)"); } catch (Exception e) { } /*// Broadcast the message over all the network interfaces Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); if (networkInterface.isLoopback() || !networkInterface.isUp()) { continue; // Don't want to broadcast to the loopback interface } for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { InetAddress broadcast = interfaceAddress.getBroadcast(); if (broadcast == null) { continue; } // Send the broadcast package! try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, broadcast, 8888); c.send(sendPacket); } catch (Exception e) { } System.out.println(getClass().getName() + ">>> Request packet sent to: " + broadcast.getHostAddress() + "; Interface: " + networkInterface.getDisplayName()); } }*/ System.out.println(getClass().getName() + ">>> Done looping over all network interfaces. Now waiting for a reply!"); //Wait for a response byte[] recvBuf = new byte[15000]; DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length); c.receive(receivePacket); //We have a response System.out.println(getClass().getName() + ">>> Broadcast response from server: " + receivePacket.getAddress().getHostAddress()); //Check if the message is correct String message = new String(receivePacket.getData()).trim(); if (message.equals("DISCOVER_BATTLESHIPSERVER_RESPONSE")) { //DO SOMETHING WITH THE SERVER'S IP (for example, store it in your controller) foundIP = receivePacket.getAddress().getHostAddress(); } //Close the port! c.close(); } catch (IOException ex) { Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex); } return foundIP; }
From source file:vmTools.java
private String callOvnmanager(String node, String action) { String result = ""; DatagramSocket socket = null; int serverPort = 9999; DatagramPacket packet2Send;/* w w w . j a v a 2 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:serverTools.java
private String callOvnmanager(String node, String action) { String result = ""; DatagramSocket socket = null; int serverPort = 9999; DatagramPacket packet2Send;// w w w . j av a 2 s. c o m 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; }