Search in sources :

Example 11 with Address

use of zmq.io.net.Address in project jeromq by zeromq.

the class TcpAddressTest method parsesIpv6AddressSimple.

@Test
public void parsesIpv6AddressSimple() throws IOException {
    String addressString = "2000::a1";
    int port = Utils.findOpenPort();
    Address addr = new Address(NetProtocol.tcp, addressString + ":" + port);
    addr.resolve(true);
    InetSocketAddress expected = new InetSocketAddress(addressString, port);
    Address.IZAddress resolved = addr.resolved();
    assertEquals(expected, resolved.address());
    InetSocketAddress sa = (InetSocketAddress) resolved.address();
    Assert.assertTrue(sa.getAddress() instanceof Inet6Address);
    Assert.assertEquals(port, sa.getPort());
}
Also used : Address(zmq.io.net.Address) Inet4Address(java.net.Inet4Address) InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) Test(org.junit.Test)

Example 12 with Address

use of zmq.io.net.Address in project jeromq by zeromq.

the class TcpConnecter method open.

// Open TCP connecting socket.
// Returns true if connect was successful immediately.
// Returns false if async connect was launched.
private boolean open() throws IOException {
    assert (fd == null);
    // Resolve the address
    if (addr == null) {
        throw new IOException("Null address");
    }
    addr.resolve(options.ipv6);
    Address.IZAddress resolved = addr.resolved();
    if (resolved == null) {
        throw new IOException("Address not resolved");
    }
    SocketAddress sa = resolved.address();
    if (sa == null) {
        throw new IOException("Socket address not resolved");
    }
    // Create the socket.
    if (options.selectorChooser == null) {
        fd = SocketChannel.open();
    } else {
        fd = options.selectorChooser.choose(resolved, options).openSocketChannel();
    }
    // The method enableIpv4Mapping is empty. Still to be written
    if (resolved.family() == StandardProtocolFamily.INET6) {
        TcpUtils.enableIpv4Mapping(fd);
    }
    // Set the socket to non-blocking mode so that we get async connect().
    TcpUtils.unblockSocket(fd);
    // Set the socket buffer limits for the underlying socket.
    if (options.sndbuf != 0) {
        TcpUtils.setTcpSendBuffer(fd, options.sndbuf);
    }
    if (options.rcvbuf != 0) {
        TcpUtils.setTcpReceiveBuffer(fd, options.rcvbuf);
    }
    // Set the IP Type-Of-Service priority for this socket
    if (options.tos != 0) {
        TcpUtils.setIpTypeOfService(fd, options.tos);
    }
    // TODO V4 Set a source address for conversations
    if (resolved.sourceAddress() != null) {
    // SocketChannel bind = channel.bind(resolved.sourceAddress());
    // if (bind == null) {
    // return false;
    // }
    }
    // Connect to the remote peer.
    boolean rc;
    try {
        rc = fd.connect(sa);
        if (rc) {
        // Connect was successful immediately.
        } else {
            // Translate error codes indicating asynchronous connect has been
            // launched to a uniform EINPROGRESS.
            errno.set(ZError.EINPROGRESS);
        }
    } catch (IllegalArgumentException e) {
        // I've found that IAE is thrown in openjdk as well as on android.
        throw new IOException(e.getMessage(), e);
    }
    return rc;
}
Also used : SocketAddress(java.net.SocketAddress) Address(zmq.io.net.Address) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress)

Aggregations

Address (zmq.io.net.Address)12 InetSocketAddress (java.net.InetSocketAddress)10 Inet4Address (java.net.Inet4Address)9 Inet6Address (java.net.Inet6Address)9 Test (org.junit.Test)9 ZMQException (org.zeromq.ZMQException)2 NetProtocol (zmq.io.net.NetProtocol)2 IOException (java.io.IOException)1 SocketAddress (java.net.SocketAddress)1 Ignore (org.junit.Ignore)1 IOThread (zmq.io.IOThread)1 SessionBase (zmq.io.SessionBase)1 IZAddress (zmq.io.net.Address.IZAddress)1 IpcConnecter (zmq.io.net.ipc.IpcConnecter)1 NormEngine (zmq.io.net.norm.NormEngine)1 PgmReceiver (zmq.io.net.pgm.PgmReceiver)1 PgmSender (zmq.io.net.pgm.PgmSender)1 SocksConnecter (zmq.io.net.tcp.SocksConnecter)1 TcpConnecter (zmq.io.net.tcp.TcpConnecter)1 TipcConnecter (zmq.io.net.tipc.TipcConnecter)1