List of usage examples for java.net DatagramPacket DatagramPacket
public DatagramPacket(byte buf[], int length, InetAddress address, int port)
From source file:org.openhim.mediator.engine.connectors.UDPFireForgetConnector.java
private void sendRequest(final MediatorSocketRequest req) { try {//from w w w . j a va 2 s . c o m final DatagramSocket socket = new DatagramSocket(); ExecutionContext ec = getContext().dispatcher(); Future<Boolean> f = future(new Callable<Boolean>() { public Boolean call() throws IOException { InetAddress addr = InetAddress.getByName(req.getHost()); byte[] payload = req.getBody().getBytes(); DatagramPacket packet = new DatagramPacket(payload, payload.length, addr, req.getPort()); socket.send(packet); return Boolean.TRUE; } }, ec); f.onComplete(new OnComplete<Boolean>() { @Override public void onComplete(Throwable throwable, Boolean result) throws Throwable { try { if (throwable != null) { throw throwable; } } catch (Exception ex) { log.error(ex, "Exception"); } finally { IOUtils.closeQuietly(socket); } } }, ec); } catch (Exception ex) { log.error(ex, "Exception"); } }
From source file:org.apache.camel.component.mina.MinaUdpWithInOutUsingPlainSocketTest.java
private String sendAndReceiveUdpMessages(String input) throws Exception { DatagramSocket socket = new DatagramSocket(); InetAddress address = InetAddress.getByName("127.0.0.1"); byte[] data = input.getBytes(); DatagramPacket packet = new DatagramPacket(data, data.length, address, PORT); LOG.debug("+++ Sending data +++"); socket.send(packet);/*from www . j a v a 2 s .c o m*/ Thread.sleep(1000); byte[] buf = new byte[128]; DatagramPacket receive = new DatagramPacket(buf, buf.length, address, PORT); LOG.debug("+++ Receving data +++"); socket.receive(receive); socket.close(); return new String(receive.getData(), 0, receive.getLength()); }
From source file:org.apache.camel.component.netty.NettyUdpWithInOutUsingPlainSocketTest.java
private String sendAndReceiveUdpMessages(String input) throws Exception { DatagramSocket socket = new DatagramSocket(); InetAddress address = InetAddress.getByName("127.0.0.1"); // must append delimiter byte[] data = (input + "\n").getBytes(); DatagramPacket packet = new DatagramPacket(data, data.length, address, PORT); LOG.debug("+++ Sending data +++"); socket.send(packet);//w w w . j av a 2 s . co m Thread.sleep(1000); byte[] buf = new byte[128]; DatagramPacket receive = new DatagramPacket(buf, buf.length, address, PORT); LOG.debug("+++ Receiving data +++"); socket.receive(receive); socket.close(); return new String(receive.getData(), 0, receive.getLength()); }
From source file:uk.co.ghosty.phonegap.plugins.WakeOnLan.java
@Override public PluginResult execute(String action, JSONArray data, String callbackId) { // TODO Auto-generated method stub Log.d("WakeOnLan", "Calling plugin execute message"); PluginResult result = null;/*from w w w. jav a2s. c o m*/ if (ACTION.equals(action)) { try { String macStr = data.getString(0); String ipStr = data.getString(1); byte[] macBytes = getMacBytes(macStr); byte[] bytes = new byte[6 + 16 * macBytes.length]; for (int i = 0; i < 6; i++) { bytes[i] = (byte) 0xff; } for (int i = 6; i < bytes.length; i += macBytes.length) { System.arraycopy(macBytes, 0, bytes, i, macBytes.length); } InetAddress address = InetAddress.getByName(ipStr); DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT); DatagramSocket socket = new DatagramSocket(); socket.send(packet); socket.close(); System.out.println("Wake-on-LAN packet sent."); result = new PluginResult(Status.OK, "Packet sent"); } catch (Exception e) { result = new PluginResult(Status.IO_EXCEPTION); Log.e("WakeOnLan", "Exception thrown", e); } } else { result = new PluginResult(Status.INVALID_ACTION); Log.d("DirectoryListPlugin", "Invalid action : " + action + " passed"); } return result; }
From source file:com.t_oster.visicut.misc.Helper.java
public static List<String> findVisiCamInstances() { List<String> result = new LinkedList<String>(); // Find the server using UDP broadcast try {/* w w w . j av a 2 s . c o m*/ //Open a random port to send the package DatagramSocket c = new DatagramSocket(); c.setBroadcast(true); byte[] sendData = "VisiCamDiscover".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); } catch (Exception e) { } // 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; // 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) { } } } //Wait for a response byte[] recvBuf = new byte[15000]; c.setSoTimeout(3000); while (true) { DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length); try { c.receive(receivePacket); //Check if the message is correct String message = new String(receivePacket.getData()).trim(); //Close the port! c.close(); if (message.startsWith("http")) { result.add(message); } } catch (SocketTimeoutException e) { break; } } } catch (IOException ex) { } return result; }
From source file:werewolf.client.DatagramReceiverThread.java
public static void sendUDPUnreliable(JSONObject message, String address, int udpPort) { System.out.println("send: " + message.toString()); System.out.println("to: " + address + " " + udpPort); try {//from ww w . ja v a 2 s .c o m InetAddress IPAddress = InetAddress.getByName(address); int targetPort = udpPort; UnreliableSender unreliableSender = new UnreliableSender(socket); byte[] sendData = message.toJSONString().getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, targetPort); unreliableSender.unreliableSend(sendPacket); } catch (UnknownHostException ex) { Logger.getLogger(WerewolfClient.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(WerewolfClient.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ru.asmsoft.p2p.outgoing.OutgoingChannelAdapter.java
public void send(Message<String> message) throws IOException { int port = (Integer) message.getHeaders().get("ip_port"); String ip = (String) message.getHeaders().get("ip_address"); byte[] bytesToSend = message.getPayload().getBytes(); InetAddress IPAddress = InetAddress.getByName(ip); DatagramPacket sendPacket = new DatagramPacket(bytesToSend, bytesToSend.length, IPAddress, port); clientSocket.send(sendPacket);/* w w w. j a va 2s .c o m*/ }
From source file:org.eredlab.g4.ccl.net.DaytimeUDPClient.java
/*** * Retrieves the time string from the specified server and port and * returns it.//w w w . j ava2 s. c o m * <p> * @param host The address of the server. * @param port The port of the service. * @return The time string. * @exception IOException If an error occurs while retrieving the time. ***/ public String getTime(InetAddress host, int port) throws IOException { DatagramPacket sendPacket, receivePacket; sendPacket = new DatagramPacket(__dummyData, __dummyData.length, host, port); receivePacket = new DatagramPacket(__timeData, __timeData.length); _socket_.send(sendPacket); _socket_.receive(receivePacket); return new String(receivePacket.getData(), 0, receivePacket.getLength()); }
From source file:us.nineworlds.serenity.core.services.GDMService.java
@Override protected void onHandleIntent(Intent intent) { try {//from ww w . j a v a 2s .c o m DatagramSocket socket = new DatagramSocket(32414); socket.setBroadcast(true); String data = "M-SEARCH * HTTP/1.1\r\n\r\n"; DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(), useMultiCastAddress(), 32414); // DatagramPacket packet = new DatagramPacket(data.getBytes(), // data.length(), getBroadcastAddress(), 32414); socket.send(packet); Log.d("GDMService", "Search Packet Broadcasted"); byte[] buf = new byte[256]; packet = new DatagramPacket(buf, buf.length); socket.setSoTimeout(2000); boolean listening = true; while (listening) { try { socket.receive(packet); String packetData = new String(packet.getData()); if (packetData.contains("HTTP/1.0 200 OK")) { Log.d("GDMService", "PMS Packet Received"); // Broadcast Received Packet Intent packetBroadcast = new Intent(GDMService.MSG_RECEIVED); packetBroadcast.putExtra("data", packetData); packetBroadcast.putExtra("ipaddress", packet.getAddress().toString()); LocalBroadcastManager.getInstance(this).sendBroadcast(packetBroadcast); } } catch (SocketTimeoutException e) { Log.d("GDMService", "Socket Timeout"); socket.close(); listening = false; Intent socketBroadcast = new Intent(GDMService.SOCKET_CLOSED); LocalBroadcastManager.getInstance(this).sendBroadcast(socketBroadcast); } } } catch (IOException e) { Log.e("GDMService", e.toString()); } }
From source file:org.hyperic.util.ntp.NtpClient.java
public NtpResponse getResponse() throws SocketException, UnknownHostException, IOException { DatagramSocket socket = new DatagramSocket(); socket.setSoTimeout(this.timeout); InetAddress address = InetAddress.getByName(this.hostname); byte[] data = NtpResponse.getRequestBytes(); DatagramPacket packet = new DatagramPacket(data, data.length, address, this.port); socket.send(packet);//from w w w . ja v a 2 s.co m packet = new DatagramPacket(data, data.length); socket.receive(packet); NtpResponse response = NtpResponse.decodeResponse(now(), packet.getData()); return response; }