Search in sources :

Example 91 with Socket

use of java.net.Socket in project XobotOS by xamarin.

the class SingleClientConnManager method releaseConnection.

// non-javadoc, see interface ClientConnectionManager
public void releaseConnection(ManagedClientConnection conn, long validDuration, TimeUnit timeUnit) {
    assertStillUp();
    if (!(conn instanceof ConnAdapter)) {
        throw new IllegalArgumentException("Connection class mismatch, " + "connection not obtained from this manager.");
    }
    if (log.isDebugEnabled()) {
        log.debug("Releasing connection " + conn);
    }
    ConnAdapter sca = (ConnAdapter) conn;
    if (sca.poolEntry == null)
        // already released
        return;
    ClientConnectionManager manager = sca.getManager();
    if (manager != null && manager != this) {
        throw new IllegalArgumentException("Connection not obtained from this manager.");
    }
    try {
        // BEGIN android-changed
        // When recycling a Socket, we un-tag it to avoid collecting
        // statistics from future users.
        final Socket socket = uniquePoolEntry.connection.getSocket();
        if (socket != null) {
            SocketTagger.get().untag(socket);
        }
        // make sure that the response has been read completely
        if (sca.isOpen() && (this.alwaysShutDown || !sca.isMarkedReusable())) {
            if (log.isDebugEnabled()) {
                log.debug("Released connection open but not reusable.");
            }
            // make sure this connection will not be re-used
            // we might have gotten here because of a shutdown trigger
            // shutdown of the adapter also clears the tracked route
            sca.shutdown();
        }
    } catch (IOException iox) {
        //@@@ log as warning? let pass?
        if (log.isDebugEnabled())
            log.debug("Exception shutting down released connection.", iox);
    } finally {
        sca.detach();
        managedConn = null;
        lastReleaseTime = System.currentTimeMillis();
        if (validDuration > 0)
            connectionExpiresTime = timeUnit.toMillis(validDuration) + lastReleaseTime;
        else
            connectionExpiresTime = Long.MAX_VALUE;
    }
}
Also used : IOException(java.io.IOException) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) Socket(java.net.Socket)

Example 92 with Socket

use of java.net.Socket in project XobotOS by xamarin.

the class ThreadSafeClientConnManager method releaseConnection.

// non-javadoc, see interface ClientConnectionManager
public void releaseConnection(ManagedClientConnection conn, long validDuration, TimeUnit timeUnit) {
    if (!(conn instanceof BasicPooledConnAdapter)) {
        throw new IllegalArgumentException("Connection class mismatch, " + "connection not obtained from this manager.");
    }
    BasicPooledConnAdapter hca = (BasicPooledConnAdapter) conn;
    if ((hca.getPoolEntry() != null) && (hca.getManager() != this)) {
        throw new IllegalArgumentException("Connection not obtained from this manager.");
    }
    try {
        // BEGIN android-changed
        // When recycling a Socket, we un-tag it to avoid collecting
        // statistics from future users.
        final BasicPoolEntry entry = (BasicPoolEntry) hca.getPoolEntry();
        final Socket socket = entry.getConnection().getSocket();
        if (socket != null) {
            SocketTagger.get().untag(socket);
        }
        // make sure that the response has been read completely
        if (hca.isOpen() && !hca.isMarkedReusable()) {
            if (log.isDebugEnabled()) {
                log.debug("Released connection open but not marked reusable.");
            }
            // In MTHCM, there would be a call to
            // SimpleHttpConnectionManager.finishLastResponse(conn);
            // Consuming the response is handled outside in 4.0.
            // make sure this connection will not be re-used
            // Shut down rather than close, we might have gotten here
            // because of a shutdown trigger.
            // Shutdown of the adapter also clears the tracked route.
            hca.shutdown();
        }
    } catch (IOException iox) {
        //@@@ log as warning? let pass?
        if (log.isDebugEnabled())
            log.debug("Exception shutting down released connection.", iox);
    } finally {
        BasicPoolEntry entry = (BasicPoolEntry) hca.getPoolEntry();
        boolean reusable = hca.isMarkedReusable();
        hca.detach();
        if (entry != null) {
            connectionPool.freeEntry(entry, reusable, validDuration, timeUnit);
        }
    }
}
Also used : IOException(java.io.IOException) Socket(java.net.Socket)

Example 93 with Socket

use of java.net.Socket in project XobotOS by xamarin.

the class AbstractClientConnAdapter method getSSLSession.

// non-javadoc, see interface ManagedClientConnection
public SSLSession getSSLSession() {
    OperatedClientConnection conn = getWrappedConnection();
    assertValid(conn);
    if (!isOpen())
        return null;
    SSLSession result = null;
    Socket sock = conn.getSocket();
    if (sock instanceof SSLSocket) {
        result = ((SSLSocket) sock).getSession();
    }
    return result;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) OperatedClientConnection(org.apache.http.conn.OperatedClientConnection) SSLSession(javax.net.ssl.SSLSession) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 94 with Socket

use of java.net.Socket in project jedis by xetorthio.

the class Connection method connect.

public void connect() {
    if (!isConnected()) {
        try {
            socket = new Socket();
            // ->@wjw_add
            socket.setReuseAddress(true);
            // Will monitor the TCP connection is
            socket.setKeepAlive(true);
            // valid
            // Socket buffer Whetherclosed, to
            socket.setTcpNoDelay(true);
            // ensure timely delivery of data
            // Control calls close () method,
            socket.setSoLinger(true, 0);
            // the underlying socket is closed
            // immediately
            // <-@wjw_add
            socket.connect(new InetSocketAddress(host, port), connectionTimeout);
            socket.setSoTimeout(soTimeout);
            if (ssl) {
                if (null == sslSocketFactory) {
                    sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
                }
                socket = (SSLSocket) sslSocketFactory.createSocket(socket, host, port, true);
                if (null != sslParameters) {
                    ((SSLSocket) socket).setSSLParameters(sslParameters);
                }
                if ((null != hostnameVerifier) && (!hostnameVerifier.verify(host, ((SSLSocket) socket).getSession()))) {
                    String message = String.format("The connection to '%s' failed ssl/tls hostname verification.", host);
                    throw new JedisConnectionException(message);
                }
            }
            outputStream = new RedisOutputStream(socket.getOutputStream());
            inputStream = new RedisInputStream(socket.getInputStream());
        } catch (IOException ex) {
            broken = true;
            throw new JedisConnectionException("Failed connecting to host " + host + ":" + port, ex);
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) RedisOutputStream(redis.clients.util.RedisOutputStream) RedisInputStream(redis.clients.util.RedisInputStream) IOException(java.io.IOException) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Example 95 with Socket

use of java.net.Socket in project platformlayer by platformlayer.

the class PlatformLayerTestContext method isPortOpen.

public boolean isPortOpen(InetSocketAddress socketAddress) throws IOException {
    Socket socket = new Socket();
    try {
        int timeout = 5000;
        socket.connect(socketAddress, timeout);
        return true;
    } catch (IOException e) {
        String message = e.getMessage();
        if (message.equalsIgnoreCase("connect timed out")) {
            return false;
        }
        throw new IllegalStateException("Unexpected IO exception checking port status", e);
    } finally {
        socket.close();
    }
}
Also used : IOException(java.io.IOException) 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