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
}
}
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;
}
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();
}
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;
}
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);
}
}
Aggregations