Search in sources :

Example 1 with AlreadyBoundException

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

the class SocketChannelImpl method bind.

/** @hide Until ready for a public API change */
@Override
public final synchronized SocketChannel bind(SocketAddress local) throws IOException {
    if (!isOpen()) {
        throw new ClosedChannelException();
    }
    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 : ClosedChannelException(java.nio.channels.ClosedChannelException) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) AlreadyBoundException(java.nio.channels.AlreadyBoundException) InetSocketAddress(java.net.InetSocketAddress)

Example 2 with AlreadyBoundException

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

the class SocketChannelTest method test_bind.

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

Example 3 with AlreadyBoundException

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

the class AsynchronousServerSocketChannelTest method test_bind_null.

public void test_bind_null() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    assertTrue(assc.isOpen());
    assertNull(assc.getLocalAddress());
    assc.bind(null);
    assertNotNull(assc.getLocalAddress());
    try {
        assc.bind(null);
        fail();
    } catch (AlreadyBoundException expected) {
    }
    assc.close();
    assertFalse(assc.isOpen());
}
Also used : AlreadyBoundException(java.nio.channels.AlreadyBoundException) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel)

Example 4 with AlreadyBoundException

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

the class AsynchronousServerSocketChannelTest method test_bind.

/* J2ObjC removed: unsupported ResourceLeakageDetector
    @Rule
    public LeakageDetectorRule leakageDetectorRule = ResourceLeakageDetector.getRule();
     */
public void test_bind() throws Throwable {
    AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
    assertTrue(assc.isOpen());
    assertNull(assc.getLocalAddress());
    assc.bind(new InetSocketAddress(0));
    assertNotNull(assc.getLocalAddress());
    try {
        assc.bind(new InetSocketAddress(0));
        fail();
    } catch (AlreadyBoundException expected) {
    }
    assc.close();
    assertFalse(assc.isOpen());
}
Also used : AlreadyBoundException(java.nio.channels.AlreadyBoundException) InetSocketAddress(java.net.InetSocketAddress) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel)

Example 5 with AlreadyBoundException

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

the class SocketChannelImpl method bind.

@Override
public SocketChannel bind(SocketAddress local) throws IOException {
    synchronized (readLock) {
        synchronized (writeLock) {
            synchronized (stateLock) {
                if (!isOpen())
                    throw new ClosedChannelException();
                if (state == ST_PENDING)
                    throw new ConnectionPendingException();
                if (localAddress != null)
                    throw new AlreadyBoundException();
                InetSocketAddress isa = (local == null) ? new InetSocketAddress(0) : Net.checkAddress(local);
                SecurityManager sm = System.getSecurityManager();
                if (sm != null) {
                    sm.checkListen(isa.getPort());
                }
                NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort());
                Net.bind(fd, isa.getAddress(), isa.getPort());
                localAddress = Net.localAddress(fd);
            }
        }
    }
    return this;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) AlreadyBoundException(java.nio.channels.AlreadyBoundException) InetSocketAddress(java.net.InetSocketAddress) ConnectionPendingException(java.nio.channels.ConnectionPendingException) NoConnectionPendingException(java.nio.channels.NoConnectionPendingException)

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