Back to project page notify.
The source code is released under:
GNU General Public License
If you think the Android project notify listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.notify; /* w w w . ja v a2s .co m*/ import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketTimeoutException; public class Broadcast implements Runnable { public Socket s; public ServerSocket ss; public Boolean reading = false; public void run() { try { if (BackgroundService.reading) { return; } String myip = OurUtils.getIP(); if (myip == null) { return; } DatagramSocket ds = new DatagramSocket(); ds.setBroadcast(true); byte[] data = ("notify " + myip).getBytes(); DatagramPacket dg = new DatagramPacket(data, data.length, InetAddress.getByName(myip.replaceAll("\\.\\d+$", ".255")), 18082); ds.send(dg); if (!this.reading && !BackgroundService.reading) { this.listen(); } } catch (Exception e) { e.printStackTrace(); } } public void listen() { try { this.ss = new ServerSocket(18081); this.ss.setSoTimeout(4000); this.reading = true; Socket s1 = this.ss.accept(); this.s = s1; OurUtils.sendMessageToActivity(EnumMessage.broadcast_response); } catch (SocketTimeoutException e) { OurUtils.sendMessageToActivity(EnumMessage.broadcast_listen_failed, "listen timed out"); } catch (Exception e) { OurUtils.sendMessageToActivity(EnumMessage.broadcast_listen_failed, "unexpected error occurred"); } try{ this.ss.close(); } catch (Exception e) { this.ss = null; } this.reading = false; } }