Search in sources :

Example 6 with AlreadyBoundException

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

the class DatagramChannelImpl method bind.

/** @hide Until ready for a public API change */
@Override
public synchronized DatagramChannel bind(SocketAddress local) throws IOException {
    checkOpen();
    if (isBound) {
        throw new AlreadyBoundException();
    }
    if (local == null) {
        local = new InetSocketAddress(Inet4Address.ANY, 0);
    } else if (!(local instanceof InetSocketAddress)) {
        throw new UnsupportedAddressTypeException();
    }
    InetSocketAddress localAddress = (InetSocketAddress) local;
    NetworkBridge.bind(fd, localAddress.getAddress(), localAddress.getPort());
    onBind(true);
    return this;
}
Also used : UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) AlreadyBoundException(java.nio.channels.AlreadyBoundException) InetSocketAddress(java.net.InetSocketAddress)

Example 7 with AlreadyBoundException

use of java.nio.channels.AlreadyBoundException in project jdk8u_jdk by JetBrains.

the class Bind method testBindUnbind.

void testBindUnbind(boolean connected) {
    SctpChannel channel = null;
    SctpChannel peerChannel = null;
    debug("testBindUnbind, connected: " + connected);
    try {
        channel = SctpChannel.open();
        List<InetAddress> addresses = Util.getAddresses(true, false);
        Iterator iterator = addresses.iterator();
        InetSocketAddress a = new InetSocketAddress((InetAddress) iterator.next(), 0);
        debug("channel.bind( " + a + ")");
        channel.bind(a);
        while (iterator.hasNext()) {
            InetAddress ia = (InetAddress) iterator.next();
            debug("channel.bindAddress(" + ia + ")");
            channel.bindAddress(ia);
        }
        if (debug) {
            Util.dumpAddresses(channel, out);
        }
        if (connected) {
            /* Test with connected channel */
            peerChannel = connectChannel(channel);
        }
        /* TEST 1: bind/unbindAddresses on the system addresses */
        debug("bind/unbindAddresses on the system addresses");
        List<InetAddress> addrs = Util.getAddresses(true, false);
        for (InetAddress addr : addrs) {
            try {
                debug("unbindAddress: " + addr);
                check(boundAddress(channel, addr), "trying to remove address that is not bound");
                channel.unbindAddress(addr);
                if (debug) {
                    Util.dumpAddresses(channel, out);
                }
                check(!boundAddress(channel, addr), "address was not removed");
                debug("bindAddress: " + addr);
                channel.bindAddress(addr);
                if (debug) {
                    Util.dumpAddresses(channel, out);
                }
                check(boundAddress(channel, addr), "address is not bound");
            } catch (IOException ioe) {
                unexpected(ioe);
            }
        }
        /* TEST 2: bindAddress - already bound address. */
        InetAddress againAddress = addrs.get(0);
        try {
            debug("bind already bound address " + againAddress);
            channel.bindAddress(againAddress);
        } catch (AlreadyBoundException unused) {
            debug("Caught AlreadyBoundException - OK");
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 3: bind non local address */
        try {
            InetAddress nla = InetAddress.getByName("123.123.123.123");
            debug("bind non local address " + nla);
            channel.bindAddress(nla);
        } catch (IOException ioe) {
            debug("Informative only " + ioe);
        }
        /* TEST 4: unbind address that is not bound */
        try {
            debug("unbind address that is not bound " + againAddress);
            /* remove address first then again */
            channel.unbindAddress(againAddress);
            channel.unbindAddress(againAddress);
        } catch (IllegalUnbindException unused) {
            debug("Caught IllegalUnbindException - OK");
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 5: unbind address that is not bound */
        try {
            InetAddress nla = InetAddress.getByName("123.123.123.123");
            debug("unbind address that is not bound " + nla);
            channel.unbindAddress(nla);
        } catch (IllegalUnbindException unused) {
            debug("Caught IllegalUnbindException - OK");
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        if (connected) {
            channel.shutdown();
            BindNotificationHandler handler = new BindNotificationHandler();
            ByteBuffer buffer = ByteBuffer.allocate(10);
            MessageInfo info;
            while ((info = peerChannel.receive(buffer, null, handler)) != null) {
                if (info != null) {
                    if (info.bytes() == -1) {
                        debug("peerChannel Reached EOF");
                        break;
                    }
                }
            }
            while ((info = channel.receive(buffer, null, handler)) != null) {
                if (info != null) {
                    if (info.bytes() == -1) {
                        debug("channel Reached EOF");
                        break;
                    }
                }
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (channel != null)
                channel.close();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) AlreadyBoundException(java.nio.channels.AlreadyBoundException) IllegalUnbindException(com.sun.nio.sctp.IllegalUnbindException) Iterator(java.util.Iterator) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 8 with AlreadyBoundException

use of java.nio.channels.AlreadyBoundException in project jdk8u_jdk by JetBrains.

the class Bind method testBind.

void testBind() {
    SctpChannel channel = null;
    try {
        channel = SctpChannel.open();
        /* TEST 1: empty set if channel is not bound */
        check(channel.getAllLocalAddresses().isEmpty(), "getAllLocalAddresses returned non empty set for unbound channel");
        /* TEST 2: null to bind the channel to an automatically assigned
             *         socket address */
        channel.bind(null);
        /* TEST 3: non empty set if the channel is bound */
        check(!channel.getAllLocalAddresses().isEmpty(), "getAllLocalAddresses returned empty set for bound channel");
        debug("getAllLocalAddresses on channel bound to the wildcard:\n" + channel.getAllLocalAddresses());
        /* TEST 4: AlreadyBoundException if this channel is already bound */
        try {
            channel.bind(null);
        } catch (AlreadyBoundException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 5: UnsupportedAddressTypeException */
        try {
            channel.close();
            /* open a new unbound channel for test */
            channel = SctpChannel.open();
            channel.bind(new UnsupportedSocketAddress());
            fail("UnsupportedSocketAddress expected");
        } catch (UnsupportedAddressTypeException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 6: AlreadyConnectedException */
        try {
            channel.close();
            /* open a new unbound channel for test */
            channel = SctpChannel.open();
            connectChannel(channel);
            channel.bind(null);
            fail("AlreadyConnectedException expected");
        } catch (AlreadyConnectedException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 7: ClosedChannelException - If this channel is closed */
        try {
            channel.close();
            /* open a new unbound channel for test */
            channel = SctpChannel.open();
            channel.close();
            channel.bind(null);
            fail("ClosedChannelException expected");
        } catch (ClosedChannelException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 8: ClosedChannelException if channel is closed */
        try {
            channel.getAllLocalAddresses();
            fail("should have thrown ClosedChannelException");
        } catch (ClosedChannelException cce) {
            pass();
        } catch (Exception ioe) {
            unexpected(ioe);
        }
    } catch (IOException ioe) {
        unexpected(ioe);
    } finally {
        try {
            channel.close();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) ClosedChannelException(java.nio.channels.ClosedChannelException) AlreadyBoundException(java.nio.channels.AlreadyBoundException) AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) ClosedChannelException(java.nio.channels.ClosedChannelException) AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) AlreadyBoundException(java.nio.channels.AlreadyBoundException) IllegalUnbindException(com.sun.nio.sctp.IllegalUnbindException)

Example 9 with AlreadyBoundException

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

the class DatagramChannelTest method test_bind.

public void test_bind() throws IOException {
    InetSocketAddress socketAddress = new InetSocketAddress(Inet4Address.LOOPBACK, 0);
    DatagramChannel channel = DatagramChannel.open();
    channel.bind(socketAddress);
    assertEquals(socketAddress.getAddress(), ((InetSocketAddress) (channel.getLocalAddress())).getAddress());
    assertTrue(((InetSocketAddress) (channel.getLocalAddress())).getPort() > 0);
    try {
        channel.bind(socketAddress);
        fail();
    } catch (AlreadyBoundException expected) {
    }
    socketAddress = new InetSocketAddress(Inet4Address.LOOPBACK, ((InetSocketAddress) (channel.getLocalAddress())).getPort());
    try (DatagramChannel dc = DatagramChannel.open()) {
        dc.bind(socketAddress);
        fail();
    } catch (BindException expected) {
    }
    channel.close();
    socketAddress = new InetSocketAddress(Inet4Address.LOOPBACK, 0);
    try {
        channel.bind(socketAddress);
        fail();
    } catch (ClosedChannelException expected) {
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) AlreadyBoundException(java.nio.channels.AlreadyBoundException) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) BindException(java.net.BindException)

Aggregations

AlreadyBoundException (java.nio.channels.AlreadyBoundException)9 InetSocketAddress (java.net.InetSocketAddress)6 ClosedChannelException (java.nio.channels.ClosedChannelException)5 UnsupportedAddressTypeException (java.nio.channels.UnsupportedAddressTypeException)3 IllegalUnbindException (com.sun.nio.sctp.IllegalUnbindException)2 SctpChannel (com.sun.nio.sctp.SctpChannel)2 BindException (java.net.BindException)2 AsynchronousServerSocketChannel (java.nio.channels.AsynchronousServerSocketChannel)2 MessageInfo (com.sun.nio.sctp.MessageInfo)1 ByteBuffer (java.nio.ByteBuffer)1 AlreadyConnectedException (java.nio.channels.AlreadyConnectedException)1 ConnectionPendingException (java.nio.channels.ConnectionPendingException)1 DatagramChannel (java.nio.channels.DatagramChannel)1 NoConnectionPendingException (java.nio.channels.NoConnectionPendingException)1 SocketChannel (java.nio.channels.SocketChannel)1 Iterator (java.util.Iterator)1