Package java.net

Examples of java.net.MulticastSocket


   * @tests java.net.MulticastSocket#MulticastSocket(int)
   */
  public void test_ConstructorI() {
    // Test for method java.net.MulticastSocket(int)
    // Used in tests
    MulticastSocket dup = null;
    try {
      mss = new MulticastSocket();
      int port = mss.getLocalPort();
      dup = new MulticastSocket(port);
            // regression test for Harmony-1162
            assertTrue(dup.getReuseAddress());
    } catch (IOException e) {
      fail("duplicate binding not allowed: " + e);
    }
    if (dup != null)
      dup.close();
  }
View Full Code Here


    int groupPort = Support_PortManager.getNextPortForUDP();
    try {
      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
          assertTrue("inet Address returned when not set:"
              + mss.getInterface().toString(), InetAddress
              .getByName("::0").equals(mss.getInterface()));
        } else {
          // we expect an IPv4 ANY in this case
          assertTrue("inet Address returned when not set:"
              + mss.getInterface().toString(), InetAddress
              .getByName("0.0.0.0").equals(mss.getInterface()));
        }

        // validate that we get the expected response when we set via
        // setInterface
        Enumeration addresses = networkInterface1.getInetAddresses();
        if (addresses != null) {
          InetAddress firstAddress = (InetAddress) addresses
              .nextElement();
          mss.setInterface(firstAddress);
          assertTrue(
              "getNetworkInterface did not return interface set by setInterface.  Expected:"
                  + firstAddress + " Got:"
                  + mss.getInterface(), firstAddress
                  .equals(mss.getInterface()));

          groupPort = Support_PortManager.getNextPortForUDP();
          mss = new MulticastSocket(groupPort);
          mss.setNetworkInterface(networkInterface1);
          assertTrue(
              "getInterface did not return interface set by setNeworkInterface Expected: "
                  + firstAddress + "Got:"
                  + mss.getInterface(), NetworkInterface
View Full Code Here

  public void test_getNetworkInterface() throws IOException {
    int groupPort = Support_PortManager.getNextPortForUDP();
    if (atLeastOneInterface) {
            // validate that we get the expected response when one was not
            // set
            mss = new MulticastSocket(groupPort);
            NetworkInterface theInterface = mss.getNetworkInterface();
            assertNotNull(
                    "network interface returned wrong network interface when not set:"
                            + theInterface, theInterface.getInetAddresses());
            InetAddress firstAddress = (InetAddress) theInterface
                    .getInetAddresses().nextElement();
            // validate we the first address in the network interface is the
            // ANY address
            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"))) {
                assertTrue(
                        "network interface returned wrong network interface when not set:"
                                + theInterface, InetAddress.getByName("::0")
                                .equals(firstAddress));

            } else {
                assertTrue(
                        "network interface returned wrong network interface when not set:"
                                + theInterface, InetAddress
                                .getByName("0.0.0.0").equals(firstAddress));
            }

            mss.setNetworkInterface(networkInterface1);
            assertTrue(
                    "getNetworkInterface did not return interface set by setNeworkInterface",
                    networkInterface1.equals(mss.getNetworkInterface()));

            if (atLeastTwoInterfaces) {
                mss.setNetworkInterface(networkInterface2);
                assertTrue(
                        "getNetworkInterface did not return network interface set by second setNetworkInterface call",
                        networkInterface2.equals(mss.getNetworkInterface()));
            }

            groupPort = Support_PortManager.getNextPortForUDP();
            mss = new MulticastSocket(groupPort);
            if (IPV6networkInterface1 != null) {
                mss.setNetworkInterface(IPV6networkInterface1);
                assertTrue(
                        "getNetworkInterface did not return interface set by setNeworkInterface",
                        IPV6networkInterface1.equals(mss.getNetworkInterface()));
            }

            // validate that we get the expected response when we set via
            // setInterface
            groupPort = Support_PortManager.getNextPortForUDP();
            mss = new MulticastSocket(groupPort);
            Enumeration addresses = networkInterface1.getInetAddresses();
            if (addresses != null) {
                firstAddress = (InetAddress) addresses.nextElement();
                mss.setInterface(firstAddress);
                assertTrue(
View Full Code Here

  /**
   * @tests java.net.MulticastSocket#getTimeToLive()
   */
  public void test_getTimeToLive() {
    try {
      mss = new MulticastSocket();
      mss.setTimeToLive(120);
      assertTrue("Returned incorrect 1st TTL: " + mss.getTimeToLive(),
          mss.getTimeToLive() == 120);
      mss.setTimeToLive(220);
      assertTrue("Returned incorrect 2nd TTL: " + mss.getTimeToLive(),
View Full Code Here

   */
  public void test_getTTL() {
    // Test for method byte java.net.MulticastSocket.getTTL()

    try {
      mss = new MulticastSocket();
      mss.setTTL((byte) 120);
      assertTrue("Returned incorrect TTL: " + mss.getTTL(),
          mss.getTTL() == 120);
      ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
    } catch (Exception e) {
View Full Code Here

      group = InetAddress.getByName("224.0.0.3");
      server = new MulticastServer(group, groupPort);
      server.start();
      Thread.sleep(1000);
      msg = "Hello World";
      mss = new MulticastSocket(ports[1]);
      DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
          .length(), group, groupPort);
      mss.send(sdp, (byte) 10);
      Thread.sleep(1000);
    } catch (Exception e) {
View Full Code Here

    int serverPort = ports[1];

      Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces();

        // first validate that we handle a null group ok
        mss = new MulticastSocket(groupPort);
        try {
            mss.joinGroup(null, null);
            fail("Did not get exception when group was null");
        } catch (IllegalArgumentException e) {
        }

        // now validate we get the expected error if the address specified
        // is not a multicast group
        try {
            groupSockAddr = new InetSocketAddress(InetAddress
                    .getByName("255.255.255.255"), groupPort);
            mss.joinGroup(groupSockAddr, null);
            fail("Did not get exception when group is not a multicast address");
        } catch (IOException e) {
        }

        // now try to join a group if we are not authorized
        // set the security manager that will make the first address not
        // visible
        System.setSecurityManager(new mySecurityManager());
        try {
            group = InetAddress.getByName("224.0.0.3");
            groupSockAddr = new InetSocketAddress(group, groupPort);
            mss.joinGroup(groupSockAddr, null);
            fail("Did not get exception when joining group is not allowed");
        } catch (SecurityException e) {
        }
        System.setSecurityManager(null);

        if (atLeastOneInterface) {
            // now validate that we can properly join a group with a null
            // network interface
            ports = Support_PortManager.getNextPortsForUDP(2);
            groupPort = ports[0];
            serverPort = ports[1];
            mss = new MulticastSocket(groupPort);
            mss.joinGroup(groupSockAddr, null);
            mss.setTimeToLive(2);
            Thread.sleep(1000);

            // set up the server and join the group on networkInterface1
            group = InetAddress.getByName("224.0.0.3");
            groupSockAddr = new InetSocketAddress(group, groupPort);
            server = new MulticastServer(groupSockAddr, serverPort,
                    networkInterface1);
            server.start();
            Thread.sleep(1000);
            msg = "Hello World";
            DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
                    .length(), group, serverPort);
            mss.setTimeToLive(2);
            mss.send(sdp);
            Thread.sleep(1000);
            // now vaildate that we received the data as expected
            assertTrue("Group member did not recv data: ", new String(
                    server.rdp.getData(), 0, server.rdp.getLength())
                    .equals(msg));
            server.stopServer();

            // now validate that we handled the case were we join a
            // different multicast address.
            // verify we do not receive the data
            ports = Support_PortManager.getNextPortsForUDP(2);
            serverPort = ports[0];
            server = new MulticastServer(groupSockAddr, serverPort,
                    networkInterface1);
            server.start();
            Thread.sleep(1000);

            groupPort = ports[1];
            mss = new MulticastSocket(groupPort);
            InetAddress group2 = InetAddress.getByName("224.0.0.4");
            mss.setTimeToLive(10);
            msg = "Hello World - Different Group";
            sdp = new DatagramPacket(msg.getBytes(), msg.length(), group2,
                    serverPort);
            mss.send(sdp);
            Thread.sleep(1000);
            assertFalse(
                    "Group member received data when sent on different group: ",
                    new String(server.rdp.getData(), 0, server.rdp.getLength())
                            .equals(msg));
            server.stopServer();

            // if there is more than one network interface then check that
            // we can join on specific interfaces and that we only receive
            // if data is received on that interface
            if (atLeastTwoInterfaces) {
                // set up server on first interfaces
                NetworkInterface loopbackInterface = NetworkInterface
                        .getByInetAddress(InetAddress.getByName("127.0.0.1"));

                theInterfaces = NetworkInterface.getNetworkInterfaces();
                while (theInterfaces.hasMoreElements()) {

                    NetworkInterface thisInterface = (NetworkInterface) theInterfaces
                            .nextElement();
                    if ((thisInterface.getInetAddresses() != null && thisInterface
                            .getInetAddresses().hasMoreElements())
                            && (Support_NetworkInterface
                                    .useInterface(thisInterface) == true)) {
                        // get the first address on the interface

                        // start server which is joined to the group and has
                        // only asked for packets on this interface
                        Enumeration addresses = thisInterface
                                .getInetAddresses();

                        NetworkInterface sendingInterface = null;
                        boolean isLoopback = false;
                        if (addresses != null) {
                            InetAddress firstAddress = (InetAddress) addresses
                                    .nextElement();
                            if (firstAddress.isLoopbackAddress()) {
                                isLoopback = true;
                            }
                            if (firstAddress instanceof Inet4Address) {
                                group = InetAddress.getByName("224.0.0.4");
                                if (networkInterface1.equals(NetworkInterface
                                        .getByInetAddress(InetAddress
                                                .getByName("127.0.0.1")))) {
                                    sendingInterface = networkInterface2;
                                } else {
                                    sendingInterface = networkInterface1;
                                }
                            } else {
                                // if this interface only seems to support
                                // IPV6 addresses
                                group = InetAddress
                                        .getByName("FF01:0:0:0:0:0:2:8001");
                                sendingInterface = IPV6networkInterface1;
                            }
                        }

                        InetAddress useAddress = null;
                        addresses = sendingInterface.getInetAddresses();
                        if ((addresses != null)
                                && (addresses.hasMoreElements())) {
                            useAddress = (InetAddress) addresses.nextElement();
                        }

                        ports = Support_PortManager.getNextPortsForUDP(2);
                        serverPort = ports[0];
                        groupPort = ports[1];
                        groupSockAddr = new InetSocketAddress(group, serverPort);
                        server = new MulticastServer(groupSockAddr, serverPort,
                                thisInterface);
                        server.start();
                        Thread.sleep(1000);

                        // Now send out a package on interface
                        // networkInterface 1. We should
                        // only see the packet if we send it on interface 1
                        InetSocketAddress theAddress = new InetSocketAddress(
                                useAddress, groupPort);
                        mss = new MulticastSocket(groupPort);
                        mss.setNetworkInterface(sendingInterface);
                        msg = "Hello World - Again" + thisInterface.getName();
                        sdp = new DatagramPacket(msg.getBytes(), msg.length(),
                                group, serverPort);
                        mss.send(sdp);
                        Thread.sleep(1000);
                        if (thisInterface.equals(sendingInterface)) {
                            assertTrue(
                                    "Group member did not recv data when bound on specific interface: ",
                                    new String(server.rdp.getData(), 0,
                                            server.rdp.getLength()).equals(msg));
                        } else {
                            assertFalse(
                                    "Group member received data on other interface when only asked for it on one interface: ",
                                    new String(server.rdp.getData(), 0,
                                            server.rdp.getLength()).equals(msg));
                        }

                        server.stopServer();
                    }
                }

                // validate that we can join the same address on two
                // different interfaces but not on the same interface
                groupPort = Support_PortManager.getNextPortForUDP();
                mss = new MulticastSocket(groupPort);
                mss.joinGroup(groupSockAddr, networkInterface1);
                mss.joinGroup(groupSockAddr, networkInterface2);
                try {
                    mss.joinGroup(groupSockAddr, networkInterface1);
                    fail("Did not get expected exception when joining for second time on same interface");
View Full Code Here

    int groupPort = ports[0];

    try {
      group = InetAddress.getByName("224.0.0.3");
      msg = "Hello World";
      mss = new MulticastSocket(ports[1]);
      DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
          .length(), group, groupPort);
      mss.send(sdp, (byte) 10);
      ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
    } catch (Exception e) {
View Full Code Here

    try {
      Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces();

      // first validate that we handle a null group ok
      mss = new MulticastSocket(groupPort);
      try {
        mss.leaveGroup(null, null);
        fail("Did not get exception when group was null");
      } catch (IllegalArgumentException e) {
      }

      // now validate we get the expected error if the address specified
      // is not a multicast group
      try {
        group = InetAddress.getByName("255.255.255.255");
        groupSockAddr = new InetSocketAddress(group, groupPort);
        mss.leaveGroup(groupSockAddr, null);
        fail("Did not get exception when group is not a multicast address");
      } catch (IOException e) {
      }

      // now try to leave a group if we are not authorized
      // set the security manager that will make the first address not
      // visible
      System.setSecurityManager(new mySecurityManager());
      try {
        group = InetAddress.getByName("224.0.0.3");
        groupSockAddr = new InetSocketAddress(group, groupPort);
        mss.leaveGroup(groupSockAddr, null);
        fail("Did not get exception when joining group is not allowed");
      } catch (SecurityException e) {
      }
      System.setSecurityManager(null);

      if (atLeastOneInterface) {

        // now test that we can join and leave a group successfully
        groupPort = Support_PortManager.getNextPortForUDP();
        mss = new MulticastSocket(groupPort);
        groupSockAddr = new InetSocketAddress(group, groupPort);
        mss.joinGroup(groupSockAddr, null);
        mss.leaveGroup(groupSockAddr, null);
        try {
          mss.leaveGroup(groupSockAddr, null);
View Full Code Here

    int[] ports = Support_PortManager.getNextPortsForUDP(2);
    int groupPort = ports[0];

    try {
      group = InetAddress.getByName("224.0.0.3");
      mss = new MulticastSocket(ports[1]);
      server = new MulticastServer(group, groupPort);
      server.start();
      Thread.sleep(200);
      DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
          .length(), group, groupPort);
View Full Code Here

TOP

Related Classes of java.net.MulticastSocket

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.