Search in sources :

Example 16 with MulticastSocket

use of java.net.MulticastSocket in project jdk8u_jdk by JetBrains.

the class SocketPermissionTest method joinGroupMulticastTest.

@Test
public void joinGroupMulticastTest() throws Exception {
    InetAddress group = InetAddress.getByName("229.227.226.221");
    try (MulticastSocket s = new MulticastSocket(0)) {
        int port = s.getLocalPort();
        String addr = "localhost:" + port;
        AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "listen,resolve"), new SocketPermission("229.227.226.221", "connect,accept"));
        // Positive
        AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
            s.joinGroup(group);
            s.leaveGroup(group);
            return null;
        }, acc);
        // Negative
        try {
            AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
                s.joinGroup(group);
                s.leaveGroup(group);
                fail("Expected SecurityException");
                return null;
            }, RESTRICTED_ACC);
        } catch (SecurityException expected) {
        }
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) AccessControlContext(java.security.AccessControlContext) SocketPermission(java.net.SocketPermission) InetAddress(java.net.InetAddress) Test(org.testng.annotations.Test)

Example 17 with MulticastSocket

use of java.net.MulticastSocket in project wildfly by wildfly.

the class ManagedSocketFactoryTest method createMulticastSocket.

@Test
public void createMulticastSocket() throws IOException {
    MulticastSocket socket1 = mock(MulticastSocket.class);
    MulticastSocket socket2 = mock(MulticastSocket.class);
    MulticastSocket socket3 = mock(MulticastSocket.class);
    MulticastSocket socket4 = mock(MulticastSocket.class);
    SocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), 1);
    when(this.manager.createMulticastSocket("test", new InetSocketAddress(0))).thenReturn(socket1);
    when(this.manager.createMulticastSocket("test", new InetSocketAddress(1))).thenReturn(socket2);
    when(this.manager.createMulticastSocket("test", address)).thenReturn(socket3);
    when(this.manager.createMulticastSocket("test")).thenReturn(socket4);
    MulticastSocket result1 = this.subject.createMulticastSocket("test");
    MulticastSocket result2 = this.subject.createMulticastSocket("test", 1);
    MulticastSocket result3 = this.subject.createMulticastSocket("test", address);
    MulticastSocket result4 = this.subject.createMulticastSocket("test", null);
    assertSame(socket1, result1);
    assertSame(socket2, result2);
    assertSame(socket3, result3);
    assertSame(socket4, result4);
}
Also used : MulticastSocket(java.net.MulticastSocket) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 18 with MulticastSocket

use of java.net.MulticastSocket in project ha-bridge by bwssytems.

the class SystemControl method pingListener.

protected void pingListener() {
    try {
        byte[] buf = new byte[256];
        String testData = "M-SEARCH * HTTP/1.1\nHOST: " + Configuration.UPNP_MULTICAST_ADDRESS + ":" + Configuration.UPNP_DISCOVERY_PORT + "ST: urn:schemas-upnp-org:device:CloudProxy:1\nMAN: \"ssdp:discover\"\nMX: 3";
        buf = testData.getBytes();
        MulticastSocket socket = new MulticastSocket(Configuration.UPNP_DISCOVERY_PORT);
        InetAddress group = InetAddress.getByName(Configuration.UPNP_MULTICAST_ADDRESS);
        DatagramPacket packet;
        packet = new DatagramPacket(buf, buf.length, group, Configuration.UPNP_DISCOVERY_PORT);
        socket.send(packet);
        socket.close();
    } catch (IOException e) {
        log.warn("Error pinging listener. " + e.getMessage());
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 19 with MulticastSocket

use of java.net.MulticastSocket in project dubbo by alibaba.

the class MulticastRegistryTest method testDefaultPort.

@Test
public void testDefaultPort() {
    MulticastRegistry multicastRegistry = new MulticastRegistry(URL.valueOf("multicast://224.5.6.7"));
    try {
        MulticastSocket multicastSocket = multicastRegistry.getMutilcastSocket();
        Assert.assertEquals(1234, multicastSocket.getLocalPort());
    } finally {
        multicastRegistry.destroy();
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) Test(org.junit.Test)

Example 20 with MulticastSocket

use of java.net.MulticastSocket in project jdk8u_jdk by JetBrains.

the class SetGetNetworkInterfaceTest method main.

public static void main(String[] args) throws Exception {
    boolean passed = true;
    try {
        MulticastSocket ms = new MulticastSocket();
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface netIf = networkInterfaces.nextElement();
            if (isNetworkInterfaceTestable(netIf)) {
                printNetIfDetails(netIf);
                ms.setNetworkInterface(netIf);
                NetworkInterface msNetIf = ms.getNetworkInterface();
                if (netIf.equals(msNetIf)) {
                    System.out.println(" OK");
                } else {
                    System.out.println("FAILED!!!");
                    printNetIfDetails(msNetIf);
                    passed = false;
                }
                System.out.println("------------------");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        passed = false;
    }
    if (!passed) {
        throw new RuntimeException("Test Fail");
    }
    System.out.println("Test passed ");
}
Also used : MulticastSocket(java.net.MulticastSocket) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException)

Aggregations

MulticastSocket (java.net.MulticastSocket)50 IOException (java.io.IOException)19 DatagramPacket (java.net.DatagramPacket)19 InetSocketAddress (java.net.InetSocketAddress)16 InetAddress (java.net.InetAddress)15 Test (org.junit.Test)11 DatagramSocket (java.net.DatagramSocket)10 SocketException (java.net.SocketException)8 SocketTimeoutException (java.net.SocketTimeoutException)8 UnknownHostException (java.net.UnknownHostException)6 NetworkInterface (java.net.NetworkInterface)5 SocketAddress (java.net.SocketAddress)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 URI (java.net.URI)4 SubsetConfiguration (org.apache.commons.configuration2.SubsetConfiguration)4 ConfigBuilder (org.apache.hadoop.metrics2.impl.ConfigBuilder)4 ArrayList (java.util.ArrayList)3 Timer (java.util.Timer)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 BindException (java.net.BindException)2