Search in sources :

Example 41 with SocketAddress

use of java.net.SocketAddress in project robovm by robovm.

the class OldSocketTest method test_ConstructorLjava_net_Proxy_Exception.

public void test_ConstructorLjava_net_Proxy_Exception() {
    SocketAddress addr1 = InetSocketAddress.createUnresolved("127.0.0.1", 80);
    SocketAddress addr2 = new InetSocketAddress("localhost", 80);
    Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1);
    // IllegalArgumentException test
    try {
        new Socket(proxy1);
        fail("should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // expected
    }
    Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr1);
    // should not throw any exception
    new Socket(proxy2);
    new Socket(Proxy.NO_PROXY);
    try {
        new Socket((Proxy) null);
        fail("IllegalArgumentException was not thrown.");
    } catch (IllegalArgumentException iae) {
    //expected
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 42 with SocketAddress

use of java.net.SocketAddress in project blade by biezhi.

the class SelectorManager method chooseSelector.

private ManagedSelector chooseSelector(SelectableChannel channel) {
    // Ideally we would like to have all connections from the same client end
    // up on the same selector (to try to avoid smearing the data from a single
    // client over all cores), but because of proxies, the remote address may not
    // really be the client - so we have to hedge our bets to ensure that all
    // channels don't end up on the one selector for a proxy.
    ManagedSelector candidate1 = null;
    if (channel != null) {
        try {
            if (channel instanceof SocketChannel) {
                SocketAddress remote = ((SocketChannel) channel).getRemoteAddress();
                if (remote instanceof InetSocketAddress) {
                    byte[] addr = ((InetSocketAddress) remote).getAddress().getAddress();
                    if (addr != null) {
                        int s = addr[addr.length - 1] & 0xFF;
                        candidate1 = _selectors[s % getSelectorCount()];
                    }
                }
            }
        } catch (IOException x) {
            LOG.ignore(x);
        }
    }
    // The ++ increment here is not atomic, but it does not matter,
    // so long as the value changes sometimes, then connections will
    // be distributed over the available selectors.
    long s = _selectorIndex++;
    int index = (int) (s % getSelectorCount());
    ManagedSelector candidate2 = _selectors[index];
    if (candidate1 == null || candidate1.size() >= candidate2.size() * 2)
        return candidate2;
    return candidate1;
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 43 with SocketAddress

use of java.net.SocketAddress in project Conversations by siacs.

the class JingleSocks5Transport method connect.

public void connect(final OnTransportConnected callback) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                final boolean useTor = connection.getAccount().isOnion() || connection.getConnectionManager().getXmppConnectionService().useTorToConnect();
                if (useTor) {
                    socket = SocksSocketFactory.createSocketOverTor(candidate.getHost(), candidate.getPort());
                } else {
                    socket = new Socket();
                    SocketAddress address = new InetSocketAddress(candidate.getHost(), candidate.getPort());
                    socket.connect(address, Config.SOCKET_TIMEOUT * 1000);
                }
                inputStream = socket.getInputStream();
                outputStream = socket.getOutputStream();
                SocksSocketFactory.createSocksConnection(socket, destination, 0);
                isEstablished = true;
                callback.established();
            } catch (IOException e) {
                callback.failed();
            }
        }
    }).start();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket)

Example 44 with SocketAddress

use of java.net.SocketAddress in project j2objc by google.

the class DatagramChannelImpl method receiveImpl.

private SocketAddress receiveImpl(ByteBuffer target, boolean loop) throws IOException {
    SocketAddress retAddr = null;
    DatagramPacket receivePacket;
    int oldposition = target.position();
    int received;
    // TODO: disallow mapped buffers and lose this conditional?
    if (target.hasArray()) {
        receivePacket = new DatagramPacket(target.array(), target.position() + target.arrayOffset(), target.remaining());
    } else {
        receivePacket = new DatagramPacket(new byte[target.remaining()], target.remaining());
    }
    do {
        received = NetworkBridge.recvfrom(false, fd, receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength(), 0, receivePacket, isConnected());
        if (receivePacket.getAddress() != null) {
            if (received > 0) {
                if (target.hasArray()) {
                    target.position(oldposition + received);
                } else {
                    // copy the data of received packet
                    target.put(receivePacket.getData(), 0, received);
                }
            }
            retAddr = receivePacket.getSocketAddress();
            break;
        }
    } while (loop);
    return retAddr;
}
Also used : DatagramPacket(java.net.DatagramPacket) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 45 with SocketAddress

use of java.net.SocketAddress in project j2objc by google.

the class DatagramChannelImpl method onBind.

/**
     * Initialise the isBound, localAddress and localPort state from the file descriptor. Used when
     * some or all of the bound state has been left to the OS to decide, or when the Socket handled
     * bind() or connect().
     *
     * @param updateSocketState
     *        if the associated socket (if present) needs to be updated
     * @hide used to sync state, non-private to avoid synthetic method
     */
void onBind(boolean updateSocketState) {
    SocketAddress sa;
    try {
        sa = NetworkOs.getsockname(fd);
    } catch (ErrnoException errnoException) {
        throw new AssertionError(errnoException);
    }
    isBound = true;
    InetSocketAddress localSocketAddress = (InetSocketAddress) sa;
    localAddress = localSocketAddress.getAddress();
    localPort = localSocketAddress.getPort();
    if (updateSocketState && socket != null) {
        socket.onBind(localAddress, localPort);
    }
}
Also used : ErrnoException(libcore.io.ErrnoException) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

SocketAddress (java.net.SocketAddress)717 InetSocketAddress (java.net.InetSocketAddress)542 Test (org.junit.Test)169 IOException (java.io.IOException)154 Socket (java.net.Socket)105 InetAddress (java.net.InetAddress)56 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)42 Proxy (java.net.Proxy)39 ArrayList (java.util.ArrayList)37 SocketChannel (java.nio.channels.SocketChannel)36 SocketException (java.net.SocketException)34 ServerSocket (java.net.ServerSocket)33 HashMap (java.util.HashMap)33 Channel (org.jboss.netty.channel.Channel)33 UnknownHostException (java.net.UnknownHostException)32 ByteBuffer (java.nio.ByteBuffer)32 Map (java.util.Map)31 Set (java.util.Set)31 Channel (io.netty.channel.Channel)30 HashSet (java.util.HashSet)30