Code example for MulticastSocket

Methods: setNetworkInterfacesetTimeToLive

0
     * 
     * @return A ready-to-use multicast socket. 
     */ 
    public static MulticastSocket createMulticastSocket(Context context)
            throws UnknownHostException, SocketException, IOException { 
        MulticastSocket multicastSocket = new MulticastSocket();
 
        if (connectedToEthernet(context)) {
            NetworkInterface netIf = NetworkInterface.getByName("eth0");
            if (netIf != null)
                multicastSocket.setNetworkInterface(netIf);
        } 
        else if (isWirelessDirect(context)) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int intaddr = wifiInfo.getIpAddress();
            byte[] byteaddr = new byte[] {
                    (byte) (intaddr & 0xff),
                    (byte) (intaddr >> 8 & 0xff),
                    (byte) (intaddr >> 16 & 0xff),
                    (byte) (intaddr >> 24 & 0xff)};
            InetAddress addr = InetAddress.getByAddress(byteaddr);
            NetworkInterface netIf = NetworkInterface.getByInetAddress(addr);
 
            multicastSocket.setNetworkInterface(netIf);
        } 
        multicastSocket.setTimeToLive(MULTICAST_TTL);
        return multicastSocket;
    } 
     
    public static boolean isWirelessDirect(Context context) {
        ConnectivityManager connManager =
Connect your IDE to all the code out there  Get Codota for Java