use of java.net.Socket in project j2objc by google.
the class SocketTest method testInitialState.
public void testInitialState() throws Exception {
Socket s = new Socket();
try {
assertFalse(s.isBound());
assertFalse(s.isClosed());
assertFalse(s.isConnected());
assertEquals(-1, s.getLocalPort());
assertTrue(s.getLocalAddress().isAnyLocalAddress());
assertNull(s.getLocalSocketAddress());
assertNull(s.getInetAddress());
assertEquals(0, s.getPort());
assertNull(s.getRemoteSocketAddress());
assertFalse(s.getReuseAddress());
assertNull(s.getChannel());
} finally {
s.close();
}
}
use of java.net.Socket in project okhttp by square.
the class SocksProxy method play.
public void play() throws IOException {
serverSocket = new ServerSocket(0);
executor.execute(new NamedRunnable("SocksProxy %s", serverSocket.getLocalPort()) {
@Override
protected void execute() {
try {
while (true) {
Socket socket = serverSocket.accept();
connectionCount.incrementAndGet();
service(socket);
}
} catch (SocketException e) {
logger.info(name + " done accepting connections: " + e.getMessage());
} catch (IOException e) {
logger.log(Level.WARNING, name + " failed unexpectedly", e);
}
}
});
}
use of java.net.Socket in project okhttp by square.
the class RealConnection method connectSocket.
/** Does all the work necessary to build a full HTTP or HTTPS connection on a raw socket. */
private void connectSocket(int connectTimeout, int readTimeout) throws IOException {
Proxy proxy = route.proxy();
Address address = route.address();
rawSocket = proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP ? address.socketFactory().createSocket() : new Socket(proxy);
rawSocket.setSoTimeout(readTimeout);
try {
Platform.get().connectSocket(rawSocket, route.socketAddress(), connectTimeout);
} catch (ConnectException e) {
ConnectException ce = new ConnectException("Failed to connect to " + route.socketAddress());
ce.initCause(e);
throw ce;
}
source = Okio.buffer(Okio.source(rawSocket));
sink = Okio.buffer(Okio.sink(rawSocket));
}
use of java.net.Socket in project okhttp by square.
the class StreamAllocation method streamFinished.
public void streamFinished(boolean noNewStreams, HttpCodec codec) {
Socket socket;
synchronized (connectionPool) {
if (codec == null || codec != this.codec) {
throw new IllegalStateException("expected " + this.codec + " but was " + codec);
}
if (!noNewStreams) {
connection.successCount++;
}
socket = deallocate(noNewStreams, false, true);
}
closeQuietly(socket);
}
use of java.net.Socket in project okhttp by square.
the class StreamAllocation method findConnection.
/**
* Returns a connection to host a new stream. This prefers the existing connection if it exists,
* then the pool, finally building a new connection.
*/
private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout, boolean connectionRetryEnabled) throws IOException {
Route selectedRoute;
synchronized (connectionPool) {
if (released)
throw new IllegalStateException("released");
if (codec != null)
throw new IllegalStateException("codec != null");
if (canceled)
throw new IOException("Canceled");
// Attempt to use an already-allocated connection.
RealConnection allocatedConnection = this.connection;
if (allocatedConnection != null && !allocatedConnection.noNewStreams) {
return allocatedConnection;
}
// Attempt to get a connection from the pool.
Internal.instance.get(connectionPool, address, this, null);
if (connection != null) {
return connection;
}
selectedRoute = route;
}
// If we need a route, make one. This is a blocking operation.
if (selectedRoute == null) {
selectedRoute = routeSelector.next();
}
RealConnection result;
synchronized (connectionPool) {
if (canceled)
throw new IOException("Canceled");
// Now that we have an IP address, make another attempt at getting a connection from the pool.
// This could match due to connection coalescing.
Internal.instance.get(connectionPool, address, this, selectedRoute);
if (connection != null)
return connection;
// Create a connection and assign it to this allocation immediately. This makes it possible
// for an asynchronous cancel() to interrupt the handshake we're about to do.
route = selectedRoute;
refusedStreamCount = 0;
result = new RealConnection(connectionPool, selectedRoute);
acquire(result);
}
// Do TCP + TLS handshakes. This is a blocking operation.
result.connect(connectTimeout, readTimeout, writeTimeout, connectionRetryEnabled);
routeDatabase().connected(result.route());
Socket socket = null;
synchronized (connectionPool) {
// Pool the connection.
Internal.instance.put(connectionPool, result);
// release this connection and acquire that one.
if (result.isMultiplexed()) {
socket = Internal.instance.deduplicate(connectionPool, address, this);
result = connection;
}
}
closeQuietly(socket);
return result;
}
Aggregations