Search in sources :

Example 46 with Socket

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();
    }
}
Also used : Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 47 with Socket

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);
            }
        }
    });
}
Also used : SocketException(java.net.SocketException) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) NamedRunnable(okhttp3.internal.NamedRunnable) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 48 with Socket

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));
}
Also used : Proxy(java.net.Proxy) Address(okhttp3.Address) SSLSocket(javax.net.ssl.SSLSocket) RealWebSocket(okhttp3.internal.ws.RealWebSocket) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

Example 49 with Socket

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);
}
Also used : Socket(java.net.Socket)

Example 50 with 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;
}
Also used : IOException(java.io.IOException) Route(okhttp3.Route) Socket(java.net.Socket)

Aggregations

Socket (java.net.Socket)1633 IOException (java.io.IOException)705 ServerSocket (java.net.ServerSocket)578 OutputStream (java.io.OutputStream)377 InetSocketAddress (java.net.InetSocketAddress)367 Test (org.junit.Test)362 InputStream (java.io.InputStream)259 InputStreamReader (java.io.InputStreamReader)179 BufferedReader (java.io.BufferedReader)160 SocketException (java.net.SocketException)137 SSLSocket (javax.net.ssl.SSLSocket)111 SocketTimeoutException (java.net.SocketTimeoutException)97 UnknownHostException (java.net.UnknownHostException)86 ConnectException (java.net.ConnectException)84 InetAddress (java.net.InetAddress)78 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 OutputStreamWriter (java.io.OutputStreamWriter)70 DataOutputStream (java.io.DataOutputStream)68 ServletOutputStream (javax.servlet.ServletOutputStream)68 CountDownLatch (java.util.concurrent.CountDownLatch)65