MulticastSocket setNetworkInterface - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for MulticastSocket setNetworkInterface method.

This code example shows how to use the following methods:isClosed, getNetworkInterface, joinGroup, setNetworkInterface, setTimeToLive
                if (mcSocket == null) {
                    mcSocket = new MulticastSocket(port);
                } 
 
                if (networkInterface != null) {
                    mcSocket.setNetworkInterface(networkInterface);
                } 
 
                mcSocket.joinGroup(address);
                mcSocket.setTimeToLive(64);
 
                if (!Tools.isAndroid()) { // Crashes in Android 2.3.3 emulator 
                    LOG.log(Level.FINE, "Connected to " + mcSocket.getNetworkInterface());
                } 
 
                connected = true;
            } 
        } 
 
        catch (final IOException e) {

6
CodeRank
This code example shows how to use the following methods:isClosed, getNetworkInterface, joinGroup, setNetworkInterface
                if (mcSocket == null) {
                    mcSocket = new MulticastSocket(port);
                } 
 
                if (networkInterface != null) {
                    mcSocket.setNetworkInterface(networkInterface);
                } 
 
                mcSocket.joinGroup(address);
 
                if (!Tools.isAndroid()) { // Crashes in Android 2.3.3 emulator 
                    LOG.log(Level.FINE, "Connected to " + mcSocket.getNetworkInterface());
                } 
 
                connected = true;
            } 
        } 
 
        catch (final IOException e) {
            LOG.log(Level.SEVERE, "Could not start receiver: " + e.toString(), e);

6
CodeRank
This code example shows how to use the following methods:setNetworkInterface, setTimeToLive
     * 
     * @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),

5
CodeRank
This code example shows how to use the following methods:setNetworkInterface, setTimeToLive
     * 
     * @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),

6
CodeRank
This code example shows how to use the following methods:close, getNetworkInterface, setNetworkInterface
        mss.close();
    } 
 
    public void test_setNetworkInterfaceLjava_net_NetworkInterface_round_trip() throws Exception { 
        // validate that we can get and set the interface 
        MulticastSocket mss = new MulticastSocket();
        mss.setNetworkInterface(networkInterface1);
        assertEquals("Interface did not seem to be set by setNeworkInterface", networkInterface1, mss.getNetworkInterface());
        mss.close();
    } 
 
    public void test_setNetworkInterfaceLjava_net_NetworkInterface_IPv4() throws Exception { 
        test_setNetworkInterfaceLjava_net_NetworkInterface(GOOD_IPv4);
    } 
 
    public void test_setNetworkInterfaceLjava_net_NetworkInterface_IPv6() throws Exception { 
        test_setNetworkInterfaceLjava_net_NetworkInterface(GOOD_IPv6);
    } 
 
    private void test_setNetworkInterfaceLjava_net_NetworkInterface(InetAddress group) throws IOException, InterruptedException {
4
CodeRank
This code example shows how to use the following methods:setNetworkInterface
        ThreadPoolExecutor exec = new ThreadPoolExecutor(10, 20, 2,
                TimeUnit.MINUTES, new LinkedBlockingQueue(20));
        volatile boolean running;
 
        ListenerThread() throws IOException { 
            socket = new MulticastSocket(multicastPort);
            group = InetAddress.getByName(broadcastAddress);
            LOG.log(Level.INFO, "Broadcast address {0}:{1}", new Object[]{broadcastAddress, multicastPort});
            if(networkInterface != null){
                LOG.log(Level.INFO, "Network interface {0}", networkInterface);
                socket.setNetworkInterface(NetworkInterface.getByName(networkInterface));
            } 
        } 
 
        @Override 
        public void run() { 
            try { 
                //socket.joinGroup(group); 
                while (running) {
                    byte[] buffer = new byte[1048576];
4
CodeRank
This code example shows how to use the following methods:getInterface, setInterface, setNetworkInterface
		int groupPort = Support_PortManager.getNextPortForUDP();
 
                if (atLeastOneInterface) {
                        // validate that we get the expected response when one was not 
                        // set 
                        mss = new MulticastSocket(groupPort);
                        String preferIPv4StackValue = System
                                        .getProperty("java.net.preferIPv4Stack");
                        String preferIPv6AddressesValue = System
                                        .getProperty("java.net.preferIPv6Addresses");
                        if (((preferIPv4StackValue == null) || preferIPv4StackValue
                                        .equalsIgnoreCase("false"))
                                        && (preferIPv6AddressesValue != null)
                                        && (preferIPv6AddressesValue.equals("true"))) {
                                // we expect an IPv6 ANY in this case 
                                assertEquals("inet Address returned when not set", 
                                             InetAddress.getByName("::0"),
                                             mss.getInterface());
                        } else { 
                                // we expect an IPv4 ANY in this case 

4
CodeRank
This code example shows how to use the following methods:getInterface, setInterface, setNetworkInterface
		int groupPort = Support_PortManager.getNextPortForUDP();
 
                if (atLeastOneInterface) {
                        // validate that we get the expected response when one was not 
                        // set 
                        mss = new MulticastSocket(groupPort);
                        String preferIPv4StackValue = System
                                        .getProperty("java.net.preferIPv4Stack");
                        String preferIPv6AddressesValue = System
                                        .getProperty("java.net.preferIPv6Addresses");
                        if (((preferIPv4StackValue == null) || preferIPv4StackValue
                                        .equalsIgnoreCase("false"))
                                        && (preferIPv6AddressesValue != null)
                                        && (preferIPv6AddressesValue.equals("true"))) {
                                // we expect an IPv6 ANY in this case 
                                assertEquals("inet Address returned when not set", 
                                             InetAddress.getByName("::0"),
                                             mss.getInterface());
                        } else { 
                                // we expect an IPv4 ANY in this case 

4
CodeRank
This code example shows how to use the following methods:getInterface, setInterface, setNetworkInterface
		int groupPort = Support_PortManager.getNextPortForUDP();
 
                if (atLeastOneInterface) {
                        // validate that we get the expected response when one was not 
                        // set 
                        mss = new MulticastSocket(groupPort);
                        String preferIPv4StackValue = System
                                        .getProperty("java.net.preferIPv4Stack");
                        String preferIPv6AddressesValue = System
                                        .getProperty("java.net.preferIPv6Addresses");
                        if (((preferIPv4StackValue == null) || preferIPv4StackValue
                                        .equalsIgnoreCase("false"))
                                        && (preferIPv6AddressesValue != null)
                                        && (preferIPv6AddressesValue.equals("true"))) {
                                // we expect an IPv6 ANY in this case 
                                assertEquals("inet Address returned when not set", 
                                             InetAddress.getByName("::0"),
                                             mss.getInterface());
                        } else { 
                                // we expect an IPv4 ANY in this case 

4
CodeRank
setNetworkInterface Method Overview
Sets the outgoing network interface used by this socket to the given networkInterface.
See Code Examples for other Java 8 MulticastSocket Methods: