Search in sources :

Example 81 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelMulticastTest method createChannelAndSendMulticastMessage.

private static void createChannelAndSendMulticastMessage(InetAddress group, int port, String msg, NetworkInterface sendingInterface) throws IOException {
    // Any datagram socket can send to a group. It does not need to have joined the group.
    DatagramChannel dc = DatagramChannel.open();
    BindableChannel channel = new BindableChannel(dc, sendingInterface);
    channel.sendMulticastMessage(msg, new InetSocketAddress(group, port));
    dc.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel)

Example 82 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelMulticastTest method test_joinSourceSpecific_nullSourceAddress_IPv4.

public void test_joinSourceSpecific_nullSourceAddress_IPv4() throws Exception {
    if (!supportsMulticast) {
        return;
    }
    DatagramChannel dc = createReceiverChannel();
    try {
        dc.join(GOOD_MULTICAST_IPv4, ipv4NetworkInterface, null);
        fail();
    } catch (NullPointerException expected) {
    }
    dc.close();
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel)

Example 83 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelMulticastTest method test_join_canMixTypesOnDifferentInterfaces.

/**
 * Confirms that the scope of each membership is network interface-level.
 */
public void test_join_canMixTypesOnDifferentInterfaces() throws Exception {
    if (!supportsMulticast) {
        return;
    }
    DatagramChannel dc = DatagramChannel.open();
    MembershipKey membershipKey1 = dc.join(GOOD_MULTICAST_IPv4, ipv4NetworkInterface);
    MembershipKey membershipKey2 = dc.join(GOOD_MULTICAST_IPv4, loopbackInterface, UNICAST_IPv4_1);
    assertNotSame(membershipKey1, membershipKey2);
    dc.close();
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) MembershipKey(java.nio.channels.MembershipKey)

Example 84 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelTest method test_bind_unresolvedAddress.

public void test_bind_unresolvedAddress() throws IOException {
    DatagramChannel dc = DatagramChannel.open();
    try {
        dc.socket().bind(new InetSocketAddress("unresolvedname", 31415));
        fail();
    } catch (IOException expected) {
    }
    assertTrue(dc.isOpen());
    assertFalse(dc.isConnected());
    dc.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException)

Example 85 with DatagramChannel

use of java.nio.channels.DatagramChannel in project j2objc by google.

the class DatagramChannelTest method test_read_intoReadOnlyByteArrays.

/* J2ObjC removed: not supported by Junit 4.11 (https://github.com/google/j2objc/issues/1318).
    @Rule
    public ResourceLeakageDetector.LeakageDetectorRule guardRule =
            ResourceLeakageDetector.getRule();
     */
public void test_read_intoReadOnlyByteArrays() throws Exception {
    ByteBuffer readOnly = ByteBuffer.allocate(1).asReadOnlyBuffer();
    try (DatagramSocket ds = new DatagramSocket(0);
        DatagramChannel dc = DatagramChannel.open()) {
        dc.connect(ds.getLocalSocketAddress());
        try {
            dc.read(readOnly);
            fail();
        } catch (IllegalArgumentException expected) {
        }
        try {
            dc.read(new ByteBuffer[] { readOnly });
            fail();
        } catch (IllegalArgumentException expected) {
        }
        try {
            dc.read(new ByteBuffer[] { readOnly }, 0, 1);
            fail();
        } catch (IllegalArgumentException expected) {
        }
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DatagramChannel (java.nio.channels.DatagramChannel)211 InetSocketAddress (java.net.InetSocketAddress)91 ByteBuffer (java.nio.ByteBuffer)71 IOException (java.io.IOException)57 DatagramSocket (java.net.DatagramSocket)21 SocketAddress (java.net.SocketAddress)21 MembershipKey (java.nio.channels.MembershipKey)21 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 SocketChannel (java.nio.channels.SocketChannel)17 SelectionKey (java.nio.channels.SelectionKey)16 InetAddress (java.net.InetAddress)13 Selector (java.nio.channels.Selector)13 Test (org.junit.Test)11 SocketException (java.net.SocketException)9 ClosedChannelException (java.nio.channels.ClosedChannelException)9 Histogram (org.HdrHistogram.Histogram)8 CancelledKeyException (java.nio.channels.CancelledKeyException)7 DatagramPacket (java.net.DatagramPacket)5 NetworkInterface (java.net.NetworkInterface)5 ArrayList (java.util.ArrayList)5