Android Open Source - MessengerApp Udp Broadcaster






From Project

Back to project page MessengerApp.

License

The source code is released under:

MIT License

If you think the Android project MessengerApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.myfirstapp;
//from  www.  j  a  v  a  2  s .c  o  m
import java.io.IOException;
import java.net.*;

public class UdpBroadcaster implements Runnable {
  public static int PORT = 10332;
    public static String BROADCAST_ADDRESS = "255.255.255.255";

    @Override
    public void run() {
      DatagramSocket sendSocket = null;
        while (!Thread.interrupted()) {
            try {
                sendSocket = new DatagramSocket();
                sendSocket.setBroadcast(true);

                byte[] buf = "".getBytes();

                InetAddress ip = InetAddress.getByName(BROADCAST_ADDRESS);

                DatagramPacket sendPacket = new DatagramPacket(buf, buf.length,
                        ip, PORT);

                sendSocket.send(sendPacket);
            } catch (UnknownHostException e) {
        System.out.println("Can't get broadcast address");
        e.printStackTrace();
      } catch (SocketException e) {
        System.out.println("Error opening socket");
        e.printStackTrace();
      } catch (IOException e) {
        System.out.println("Can't send packet");
        e.printStackTrace();
      } finally {
        if(sendSocket != null) {
          sendSocket.close();
          sendSocket = null;
        }
      }
            
            try {    
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                break;
            }
        }
    }

}




Java Source Code List

com.example.myfirstapp.MainActivity.java
com.example.myfirstapp.TcpReceiverWorker.java
com.example.myfirstapp.TcpReceiver.java
com.example.myfirstapp.TcpSender.java
com.example.myfirstapp.UdpBroadcaster.java
com.example.myfirstapp.UdpClient.java