Package java.net

Examples of java.net.MulticastSocket


    // Test for method void
    // java.net.MulticastSocket.setInterface(java.net.InetAddress)
    // Note that the machine is not multi-homed

    try {
      mss = new MulticastSocket();
      mss.setInterface(InetAddress.getLocalHost());
      ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST_INTERFACE);
    } catch (Exception e) {
      handleException(e, SO_MULTICAST_INTERFACE);
      return;
    }
    try {
      InetAddress theInterface = mss.getInterface();
      // under IPV6 we are not guarrenteed to get the same address back as
      // the address, all we should be guaranteed is that we get an
      // address on the same interface
      if (theInterface instanceof Inet6Address) {
        assertTrue(
            "Failed to return correct interface IPV6",
            NetworkInterface
                .getByInetAddress(mss.getInterface())
                .equals(
                    NetworkInterface
                        .getByInetAddress(theInterface)));
      } else {
        assertTrue("Failed to return correct interface IPV4 got:"
            + mss.getInterface() + " excpeted: "
            + InetAddress.getLocalHost(), mss.getInterface()
            .equals(InetAddress.getLocalHost()));
      }
      ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
    } catch (SocketException e) {
      handleException(e, SO_MULTICAST);
    } catch (UnknownHostException e) {
      fail("Exception during setInterface test: " + e.toString());
    }

    // Regression test for Harmony-2410
    try {
      mss = new MulticastSocket();
      mss.setInterface(InetAddress.getByName("224.0.0.5"));
    } catch (UnknownHostException uhe) {
      fail("Unable to get InetAddress by name from '224.0.0.5' addr: " + uhe.toString());
    } catch (SocketException se) {
      // expected
View Full Code Here


    int[] ports = Support_PortManager.getNextPortsForUDP(2);
    int groupPort = ports[0];
    int serverPort = ports[1];
    if (atLeastOneInterface) {
            // validate that null interface is handled ok
            mss = new MulticastSocket(groupPort);

            // this should through a socket exception to be compatible
            try {
                mss.setNetworkInterface(null);
                fail("No socket exception when we set then network interface with NULL");
            } catch (SocketException ex) {
            }

            // validate that we can get and set the interface
            groupPort = Support_PortManager.getNextPortForUDP();
            mss = new MulticastSocket(groupPort);
            mss.setNetworkInterface(networkInterface1);
            assertTrue(
                    "Interface did not seem to be set by setNeworkInterface",
                    networkInterface1.equals(mss.getNetworkInterface()));

            // set up the server and join the group
            group = InetAddress.getByName("224.0.0.3");

            Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces();
            while (theInterfaces.hasMoreElements()) {
                NetworkInterface thisInterface = (NetworkInterface) theInterfaces
                        .nextElement();
                if (thisInterface.getInetAddresses() != null
                        && thisInterface.getInetAddresses().hasMoreElements()) {
                    if ((!((InetAddress) thisInterface.getInetAddresses()
                            .nextElement()).isLoopbackAddress())
                            &&
                            // for windows we cannot use these pseudo
                            // interfaces for the test as the packets still
                            // come from the actual interface, not the
                            // Pseudo interface that was set
                            (Support_NetworkInterface
                                    .useInterface(thisInterface) == true)) {
                        ports = Support_PortManager.getNextPortsForUDP(2);
                        serverPort = ports[0];
                        server = new MulticastServer(group, serverPort);
                        server.start();
                        // give the server some time to start up
                        Thread.sleep(1000);

                        // Send the packets on a particular interface. The
                        // source address in the received packet
                        // should be one of the addresses for the interface
                        // set
                        groupPort = ports[1];
                        mss = new MulticastSocket(groupPort);
                        mss.setNetworkInterface(thisInterface);
                        msg = thisInterface.getName();
                        byte theBytes[] = msg.getBytes();
                        DatagramPacket sdp = new DatagramPacket(theBytes,
                                theBytes.length, group, serverPort);
View Full Code Here

  /**
   * @tests java.net.MulticastSocket#setTimeToLive(int)
   */
  public void test_setTimeToLiveI() {
    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

   * @tests java.net.MulticastSocket#setTTL(byte)
   */
  public void test_setTTLB() {
    // Test for method void java.net.MulticastSocket.setTTL(byte)
    try {
      mss = new MulticastSocket();
      mss.setTTL((byte) 120);
      assertTrue("Failed to set TTL: " + mss.getTTL(),
          mss.getTTL() == 120);
      ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
    } catch (Exception e) {
View Full Code Here

  /**
   * @tests java.net.MulticastSocket#MulticastSocket(java.net.SocketAddress)
   */
  public void test_ConstructorLjava_net_SocketAddress() throws Exception
    MulticastSocket ms = new MulticastSocket((SocketAddress) null);
        assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.bind(new InetSocketAddress(InetAddress.getLocalHost(),
                Support_PortManager.getNextPortForUDP()));
        assertTrue("should be bound", ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
        ms = new MulticastSocket(new InetSocketAddress(InetAddress
                .getLocalHost(), Support_PortManager.getNextPortForUDP()));
        assertTrue("should be bound", ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
        ms = new MulticastSocket(new InetSocketAddress("localhost",
                Support_PortManager.getNextPortForUDP()));
        assertTrue("should be bound", ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
        boolean exception = false;
        try {
            ms = new MulticastSocket(new InetSocketAddress("unresolvedname",
                    Support_PortManager.getNextPortForUDP()));
        } catch (IOException e) {
            exception = true;
        }
        assertTrue("Expected IOException", exception);

        // regression test for Harmony-1162
        InetSocketAddress addr = new InetSocketAddress("0.0.0.0", 0);
        MulticastSocket s = new MulticastSocket(addr);
        assertTrue(s.getReuseAddress());
  }
View Full Code Here

  /**
   * @tests java.net.MulticastSocket#getLoopbackMode()
   */
  public void test_getLoopbackMode() {
    try {
      MulticastSocket ms = new MulticastSocket((SocketAddress) null);
      assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
          && !ms.isConnected());
      ms.getLoopbackMode();
      assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
          && !ms.isConnected());
      ms.close();
      assertTrue("should be closed", ms.isClosed());
      ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_USELOOPBACK);
    } catch (IOException e) {
      handleException(e, SO_USELOOPBACK);
    }
  }
View Full Code Here

  /**
   * @tests java.net.MulticastSocket#setLoopbackMode(boolean)
   */
  public void test_setLoopbackModeZ() {
    try {
      MulticastSocket ms = new MulticastSocket();
      ms.setLoopbackMode(true);
      assertTrue("loopback should be true", ms.getLoopbackMode());
      ms.setLoopbackMode(false);
      assertTrue("loopback should be false", !ms.getLoopbackMode());
      ms.close();
      assertTrue("should be closed", ms.isClosed());
      ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_USELOOPBACK);
    } catch (IOException e) {
      handleException(e, SO_USELOOPBACK);
    }
  }
View Full Code Here

        final String ADDRESS = "224.1.2.3";
        final int PORT = Support_PortManager.getNextPortForUDP();
        final String message = "Hello, world!";

        // test send receive
        MulticastSocket socket = null;
        try {
            // open a multicast socket
            socket = new MulticastSocket(PORT);
            socket.setLoopbackMode(false); // false indecates doing loop back
            socket.joinGroup(InetAddress.getByName(ADDRESS));

            // send the datagram
            byte[] sendData = message.getBytes();
            DatagramPacket sendDatagram = new DatagramPacket(sendData, 0,
                    sendData.length, new InetSocketAddress(InetAddress
                            .getByName(ADDRESS), PORT));
            socket.send(sendDatagram);

            // receive the datagram
            byte[] recvData = new byte[100];
            DatagramPacket recvDatagram = new DatagramPacket(recvData,
                    recvData.length);
            socket.setSoTimeout(5000); // prevent eternal block in
            // socket.receive()
            socket.receive(recvDatagram);
            String recvMessage = new String(recvData, 0, recvDatagram
                    .getLength());
            assertEquals(message, recvMessage);
        }finally {
            if (socket != null)
                socket.close();
        }
    }
View Full Code Here

   * @tests java.net.MulticastSocket#setReuseAddress(boolean)
   */
  public void test_setReuseAddressZ() {
    try {
      // test case were we set it to false
      MulticastSocket theSocket1 = null;
      MulticastSocket theSocket2 = null;
      try {
        InetSocketAddress theAddress = new InetSocketAddress(
            InetAddress.getLocalHost(), Support_PortManager
                .getNextPortForUDP());
        theSocket1 = new MulticastSocket(null);
        theSocket2 = new MulticastSocket(null);
        theSocket1.setReuseAddress(false);
        theSocket2.setReuseAddress(false);
        theSocket1.bind(theAddress);
        theSocket2.bind(theAddress);
        fail(
            "No exception when trying to connect to do duplicate socket bind with re-useaddr set to false");
      } catch (BindException e) {

      }
      if (theSocket1 != null)
        theSocket1.close();
      if (theSocket2 != null)
        theSocket2.close();

      // test case were we set it to true
      try {
        InetSocketAddress theAddress = new InetSocketAddress(
            InetAddress.getLocalHost(), Support_PortManager
                .getNextPortForUDP());
        theSocket1 = new MulticastSocket(null);
        theSocket2 = new MulticastSocket(null);
        theSocket1.setReuseAddress(true);
        theSocket2.setReuseAddress(true);
        theSocket1.bind(theAddress);
        theSocket2.bind(theAddress);
      } catch (Exception e) {
        fail(
            "unexpected exception when trying to connect to do duplicate socket bind with re-useaddr set to true");
      }
      if (theSocket1 != null)
        theSocket1.close();
      if (theSocket2 != null)
        theSocket2.close();

      // test the default case which we expect to be the same on all
      // platforms
      try {
        InetSocketAddress theAddress = new InetSocketAddress(
            InetAddress.getLocalHost(), Support_PortManager
                .getNextPortForUDP());
        theSocket1 = new MulticastSocket(null);
        theSocket2 = new MulticastSocket(null);
        theSocket1.bind(theAddress);
        theSocket2.bind(theAddress);
      } catch (BindException e) {
        fail(
            "unexpected exception when trying to connect to do duplicate socket bind with re-useaddr left as default");
      }
      if (theSocket1 != null)
        theSocket1.close();
      if (theSocket2 != null)
        theSocket2.close();
      ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
    } catch (Exception e) {
      handleException(e, SO_REUSEADDR);
    }
  }
View Full Code Here

        public MulticastServer(InetAddress anAddress, int aPort)
                throws java.io.IOException {
            rbuf = new byte[512];
            rbuf[0] = -1;
            rdp = new DatagramPacket(rbuf, rbuf.length);
            ms = new MulticastSocket(aPort);
            ms.setSoTimeout(2000);
            groupAddr = anAddress;
            ms.joinGroup(groupAddr);
        }
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.