List of usage examples for java.net DatagramSocket DatagramSocket
public DatagramSocket() throws SocketException
From source file:easy.api.service.ComponentSocket.java
public String send() throws UnknownHostException, SocketException, IOException { String result = ""; // create socket to IP final InetAddress inetAddress = InetAddress.getByName(this.ipAddress); byte[] base64String = Base64.decodeBase64(this.magicPacketBase64.getBytes()); //byte[] base64String = Base64.decodeBase64(this.magicPacketBase64); DatagramPacket datagramPacket = new DatagramPacket(base64String, base64String.length, inetAddress, this.udpPort); try (DatagramSocket datagramSocket = new DatagramSocket()) { datagramSocket.send(datagramPacket); result += this.magicPacketBase64 + " send successfull!!\n"; } catch (Exception e) { result += e.getMessage();/*from w ww .j a va2 s. c om*/ } return result; }
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);//ww w. ja v a2s. c om 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: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 {//from w ww.ja v a 2 s. co 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:com.blueshift.mvpremote.MVPremoteActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//from w w w . j a va2 s.c o m MyPagerAdapter adapter = new MyPagerAdapter(); ViewPager myPager = (ViewPager) findViewById(R.id.mypanelpager); myPager.setAdapter(adapter); myPager.setCurrentItem(0); mPrefs = getPreferences(MODE_PRIVATE); hostname = mPrefs.getString("hostname", "192.168.1.255"); try { s = new DatagramSocket(); addr = InetAddress.getByName(hostname); } catch (SocketException e) { // TODO Auto-generated catch blck e.printStackTrace(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
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.j ava2 s. 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:org.libreoffice.impressremote.communication.TcpServersFinder.java
private void setUpSearchSocket() { try {//www. jav a 2 s.c o m mSearchSocket = new DatagramSocket(); mSearchSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(BLOCKING_TIMEOUT_IN_SECONDS)); } catch (SocketException e) { throw new RuntimeException("Unable to open search socket."); } }
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 va2 s. c o m packet = new DatagramPacket(data, data.length); socket.receive(packet); NtpResponse response = NtpResponse.decodeResponse(now(), packet.getData()); return response; }
From source file:org.vesalainen.nmea.processor.SNTPBroadcaster.java
public SNTPBroadcaster(SntpBroadcasterType sntpBroadcasterType) throws SocketException, UnknownHostException { log.setLogger(this.getClass()); int pollInterval = 6; Integer interval = sntpBroadcasterType.getPollInterval(); if (interval != null) { pollInterval = interval;/* w w w . j a v a2 s .c om*/ } period = (long) Math.pow(2, pollInterval) * 1000; socket = new DatagramSocket(); socket.setBroadcast(true); ntpMessage = new NtpV4Impl(); ntpMessage.setMode(MODE_BROADCAST); ntpMessage.setPoll(pollInterval); ntpMessage.setPrecision(-6); ntpMessage.setStratum(1); ntpMessage.setReferenceId(ReferenceIdentifier.GPS); }
From source file:QuoteServerThread.java
QuoteServerThread() { super("QuoteServer"); try {/* w w w. ja v a2s . co m*/ socket = new DatagramSocket(); System.out.println("QuoteServer listening on port: " + socket.getLocalPort()); } catch (java.io.IOException e) { System.err.println("Could not create datagram socket."); } this.openInputFile(); }
From source file:com.neophob.sematrix.core.output.E1_31Device.java
/** * /*from w w w . j a va 2s.c o m*/ * @param controller */ public E1_31Device(ApplicationConfigurationHelper ph, int nrOfScreens) { super(OutputDeviceEnum.E1_31, ph, 8, nrOfScreens); this.displayOptions = ph.getE131Device(); //Get dmx specific config this.pixelsPerUniverse = ph.getE131PixelsPerUniverse(); try { String ip = ph.getE131Ip(); String sendMode = "Unicast"; if (StringUtils.startsWith(ip, MULTICAST_START)) { this.sendMulticast = true; sendMode = "Multicast"; } this.targetAdress = InetAddress.getByName(ip); this.firstUniverseId = ph.getE131StartUniverseId(); calculateNrOfUniverse(); packet = new DatagramPacket(new byte[0], 0, targetAdress, E1_31DataPacket.E131_PORT); dsocket = new DatagramSocket(); this.initialized = true; LOG.log(Level.INFO, "E1.31 device initialized, send mode: " + sendMode + ", use " + this.displayOptions.size() + " panels"); } catch (Exception e) { LOG.log(Level.WARNING, "failed to initialize E1.31 device", e); } }