Java Code Examples for java.net.Socket#isConnected()

The following examples show how to use java.net.Socket#isConnected() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StratumServer.java    From bitcoin-verde with MIT License 7 votes vote down vote up
protected NodeJsonRpcConnection _getNodeJsonRpcConnection() {
    final String bitcoinRpcUrl = _stratumProperties.getBitcoinRpcUrl();
    final Integer bitcoinRpcPort = _stratumProperties.getBitcoinRpcPort();

    try {
        final Socket socket = new Socket(bitcoinRpcUrl, bitcoinRpcPort);
        if (socket.isConnected()) {
            return new NodeJsonRpcConnection(socket, _threadPool, _masterInflater);
        }
    }
    catch (final Exception exception) {
        Logger.warn(exception);
    }

    return null;
}
 
Example 2
Source File: Lib485Connection.java    From openhab1-addons with Eclipse Public License 2.0 7 votes vote down vote up
@Override
public void open(String connectionString) throws Exception {
    String host = DEFAULT_HOST;
    int port = DEFAULT_PORT;

    if (StringUtils.isNotBlank(connectionString)) {
        String[] connectionStringElements = connectionString.split(":");
        if (connectionStringElements.length == 1) {
            host = connectionStringElements[0];
        } else if (connectionStringElements.length == 2) {
            host = connectionStringElements[0];
            port = Integer.valueOf(connectionStringElements[1]).intValue();
        }
    }

    connection = new Socket(host, port);
    if (connection.isConnected()) {
        logger.debug("Connected to Lib485 DMX service");
    }
}
 
Example 3
Source File: Environment.java    From bitcoin-verde with MIT License 6 votes vote down vote up
public StratumJsonRpcConnection getStratumJsonRpcConnection() {
    final String stratumRpcUrl = _explorerProperties.getStratumRpcUrl();
    final Integer stratumRpcPort = _explorerProperties.getStratumRpcPort();

    try {
        final Socket socket = new Socket(stratumRpcUrl, stratumRpcPort);
        if (socket.isConnected()) {
            return new StratumJsonRpcConnection(socket, _threadPool);
        }
    }
    catch (final Exception exception) {
        Logger.warn(exception);
    }

    return null;
}
 
Example 4
Source File: X509KeyManagerImpl.java    From openjsse with GNU General Public License v2.0 5 votes vote down vote up
private X500Principal[] getCertificateAuthorities(Socket socket) {
    if(socket != null && socket.isConnected() && socket instanceof SSLSocket){
        final SSLSocket sslSocket = (SSLSocket)socket;
        return getCertificateAuthorities(sslSocket.getHandshakeSession());
    }
    return null;
}
 
Example 5
Source File: X509TrustManagerImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static List<SNIServerName> getRequestedServerNames(Socket socket) {
    if (socket != null && socket.isConnected() &&
                                    socket instanceof SSLSocket) {

        SSLSocket sslSocket = (SSLSocket)socket;
        SSLSession session = sslSocket.getHandshakeSession();

        if (session != null && (session instanceof ExtendedSSLSession)) {
            ExtendedSSLSession extSession = (ExtendedSSLSession)session;
            return extSession.getRequestedServerNames();
        }
    }

    return Collections.<SNIServerName>emptyList();
}
 
Example 6
Source File: ChatTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void testPortOpen() throws Exception {
    ChatServer server = startServer();
    try {
        Socket socket = new Socket("localhost", listeningPort);
        if (!socket.isConnected()) {
            throw new RuntimeException("Failed to connect to server: port not open");
        }
    } finally {
        server.shutdown();
    }
}
 
Example 7
Source File: X509KeyManagerImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private AlgorithmConstraints getAlgorithmConstraints(Socket socket) {
    if (socket != null && socket.isConnected() &&
                                    socket instanceof SSLSocket) {

        SSLSocket sslSocket = (SSLSocket)socket;
        SSLSession session = sslSocket.getHandshakeSession();

        if (session != null) {
            ProtocolVersion protocolVersion =
                ProtocolVersion.valueOf(session.getProtocol());
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
                String[] peerSupportedSignAlgs = null;

                if (session instanceof ExtendedSSLSession) {
                    ExtendedSSLSession extSession =
                        (ExtendedSSLSession)session;
                    peerSupportedSignAlgs =
                        extSession.getPeerSupportedSignatureAlgorithms();
                }

                return new SSLAlgorithmConstraints(
                    sslSocket, peerSupportedSignAlgs, true);
            }
        }

        return new SSLAlgorithmConstraints(sslSocket, true);
    }

    return new SSLAlgorithmConstraints((SSLSocket)null, true);
}
 
Example 8
Source File: ClientX509ExtendedTrustManager.java    From light-4j with Apache License 2.0 5 votes vote down vote up
private void checkIdentity(Socket socket, X509Certificate cert) throws CertificateException {
	if (socket != null && socket.isConnected() && socket instanceof SSLSocket) {
		SSLSocket sslSocket = (SSLSocket) socket;
		SSLSession session = sslSocket.getHandshakeSession();

		checkIdentity(session, cert);
	}
}
 
Example 9
Source File: ChatTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void testPortOpen() throws Exception {
    ChatServer server = startServer();
    try {
        Socket socket = new Socket("localhost", listeningPort);
        if (!socket.isConnected()) {
            throw new RuntimeException("Failed to connect to server: port not open");
        }
    } finally {
        server.shutdown();
    }
}
 
Example 10
Source File: ChatTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void testPortOpen() throws Exception {
    ChatServer server = startServer();
    try {
        Socket socket = new Socket("localhost", listeningPort);
        if (!socket.isConnected()) {
            throw new RuntimeException("Failed to connect to server: port not open");
        }
    } finally {
        server.shutdown();
    }
}
 
Example 11
Source File: RpcSocketPool.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
private boolean isAlive(Socket socket) {
	return socket != null && socket.isBound() && !socket.isClosed()
			&& socket.isConnected() && !socket.isInputShutdown()
			&& !socket.isOutputShutdown();
}
 
Example 12
Source File: ListenActivity.java    From protect-baby-monitor with GNU General Public License v3.0 4 votes vote down vote up
private void streamAudio(final Socket socket) throws IllegalArgumentException, IllegalStateException, IOException
{
    Log.i(TAG, "Setting up stream");

    final int frequency = 11025;
    final int channelConfiguration = AudioFormat.CHANNEL_OUT_MONO;
    final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
    final int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
    final int byteBufferSize = bufferSize*2;

    final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            frequency,
            channelConfiguration,
            audioEncoding,
            bufferSize,
            AudioTrack.MODE_STREAM);

    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    final InputStream is = socket.getInputStream();
    int read = 0;

    audioTrack.play();

    try
    {
        final byte [] buffer = new byte[byteBufferSize];

        while(socket.isConnected() && read != -1 && Thread.currentThread().isInterrupted() == false)
        {
            read = is.read(buffer);

            if(read > 0)
            {
                audioTrack.write(buffer, 0, read);
            }
        }
    }
    finally
    {
        audioTrack.stop();
        socket.close();
    }
}
 
Example 13
Source File: SSLContextImpl.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private void checkAdditionalTrust(X509Certificate[] chain, String authType,
            Socket socket, boolean isClient) throws CertificateException {
    if (socket != null && socket.isConnected() &&
                                socket instanceof SSLSocket) {

        SSLSocket sslSocket = (SSLSocket)socket;
        SSLSession session = sslSocket.getHandshakeSession();
        if (session == null) {
            throw new CertificateException("No handshake session");
        }

        // check endpoint identity
        String identityAlg = sslSocket.getSSLParameters().
                                    getEndpointIdentificationAlgorithm();
        if (identityAlg != null && identityAlg.length() != 0) {
            String hostname = session.getPeerHost();
            X509TrustManagerImpl.checkIdentity(
                                hostname, chain[0], identityAlg);
        }

        // try the best to check the algorithm constraints
        ProtocolVersion protocolVersion =
            ProtocolVersion.valueOf(session.getProtocol());
        AlgorithmConstraints constraints = null;
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
            if (session instanceof ExtendedSSLSession) {
                ExtendedSSLSession extSession =
                                (ExtendedSSLSession)session;
                String[] peerSupportedSignAlgs =
                        extSession.getLocalSupportedSignatureAlgorithms();

                constraints = new SSLAlgorithmConstraints(
                                sslSocket, peerSupportedSignAlgs, true);
            } else {
                constraints =
                        new SSLAlgorithmConstraints(sslSocket, true);
            }
        } else {
            constraints = new SSLAlgorithmConstraints(sslSocket, true);
        }

        checkAlgorithmConstraints(chain, constraints);
    }
}
 
Example 14
Source File: X509TrustManagerImpl.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private void checkTrusted(X509Certificate[] chain,
        String authType, Socket socket,
        boolean checkClientTrusted) throws CertificateException {
    Validator v = checkTrustedInit(chain, authType, checkClientTrusted);

    AlgorithmConstraints constraints = null;
    if ((socket != null) && socket.isConnected() &&
                                    (socket instanceof SSLSocket)) {

        SSLSocket sslSocket = (SSLSocket)socket;
        SSLSession session = sslSocket.getHandshakeSession();
        if (session == null) {
            throw new CertificateException("No handshake session");
        }

        // check endpoint identity
        String identityAlg = sslSocket.getSSLParameters().
                                    getEndpointIdentificationAlgorithm();
        if (identityAlg != null && identityAlg.length() != 0) {
            checkIdentity(session, chain, identityAlg, checkClientTrusted);
        }

        // create the algorithm constraints
        ProtocolVersion protocolVersion =
            ProtocolVersion.valueOf(session.getProtocol());
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
            if (session instanceof ExtendedSSLSession) {
                ExtendedSSLSession extSession =
                                (ExtendedSSLSession)session;
                String[] localSupportedSignAlgs =
                        extSession.getLocalSupportedSignatureAlgorithms();

                constraints = new SSLAlgorithmConstraints(
                                sslSocket, localSupportedSignAlgs, false);
            } else {
                constraints =
                        new SSLAlgorithmConstraints(sslSocket, false);
            }
        } else {
            constraints = new SSLAlgorithmConstraints(sslSocket, false);
        }
    }

    X509Certificate[] trustedChain = null;
    if (checkClientTrusted) {
        trustedChain = validate(v, chain, constraints, null);
    } else {
        trustedChain = validate(v, chain, constraints, authType);
    }
    if (debug != null && Debug.isOn("trustmanager")) {
        System.out.println("Found trusted certificate:");
        System.out.println(trustedChain[trustedChain.length - 1]);
    }
}
 
Example 15
Source File: SSLContextImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private void checkAdditionalTrust(X509Certificate[] chain,
        String authType, Socket socket,
        boolean checkClientTrusted) throws CertificateException {
    if (socket != null && socket.isConnected() &&
                                socket instanceof SSLSocket) {

        SSLSocket sslSocket = (SSLSocket)socket;
        SSLSession session = sslSocket.getHandshakeSession();
        if (session == null) {
            throw new CertificateException("No handshake session");
        }

        // check endpoint identity
        String identityAlg = sslSocket.getSSLParameters().
                                    getEndpointIdentificationAlgorithm();
        if (identityAlg != null && !identityAlg.isEmpty()) {
            X509TrustManagerImpl.checkIdentity(session, chain,
                                identityAlg, checkClientTrusted);
        }

        // try the best to check the algorithm constraints
        AlgorithmConstraints constraints;
        if (ProtocolVersion.useTLS12PlusSpec(session.getProtocol())) {
            if (session instanceof ExtendedSSLSession) {
                ExtendedSSLSession extSession =
                                (ExtendedSSLSession)session;
                String[] peerSupportedSignAlgs =
                        extSession.getLocalSupportedSignatureAlgorithms();

                constraints = new SSLAlgorithmConstraints(
                                sslSocket, peerSupportedSignAlgs, true);
            } else {
                constraints =
                        new SSLAlgorithmConstraints(sslSocket, true);
            }
        } else {
            constraints = new SSLAlgorithmConstraints(sslSocket, true);
        }

        checkAlgorithmConstraints(chain, constraints, checkClientTrusted);
    }
}
 
Example 16
Source File: RpcSocketPool.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
private boolean isAlive(Socket socket) {
	return socket != null && socket.isBound() && !socket.isClosed()
			&& socket.isConnected() && !socket.isInputShutdown()
			&& !socket.isOutputShutdown();
}
 
Example 17
Source File: MonitorActivity.java    From protect-baby-monitor with GNU General Public License v3.0 4 votes vote down vote up
private void serviceConnection(Socket socket) throws IOException
{
    MonitorActivity.this.runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            final TextView statusText = (TextView) findViewById(R.id.textStatus);
            statusText.setText(R.string.streaming);
        }
    });

    final int frequency = 11025;
    final int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
    final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

    final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
    final AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
            frequency, channelConfiguration,
            audioEncoding, bufferSize);

    final int byteBufferSize = bufferSize*2;
    final byte[] buffer = new byte[byteBufferSize];

    try
    {
        audioRecord.startRecording();

        final OutputStream out = socket.getOutputStream();

        socket.setSendBufferSize(byteBufferSize);
        Log.d(TAG, "Socket send buffer size: " + socket.getSendBufferSize());

        while (socket.isConnected() && Thread.currentThread().isInterrupted() == false)
        {
            final int read = audioRecord.read(buffer, 0, bufferSize);
            out.write(buffer, 0, read);
        }
    }
    finally
    {
        audioRecord.stop();
    }
}
 
Example 18
Source File: SSLContextImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private void checkAdditionalTrust(X509Certificate[] chain,
        String authType, Socket socket,
        boolean checkClientTrusted) throws CertificateException {
    if (socket != null && socket.isConnected() &&
                                socket instanceof SSLSocket) {

        SSLSocket sslSocket = (SSLSocket)socket;
        SSLSession session = sslSocket.getHandshakeSession();
        if (session == null) {
            throw new CertificateException("No handshake session");
        }

        // check endpoint identity
        String identityAlg = sslSocket.getSSLParameters().
                                    getEndpointIdentificationAlgorithm();
        if (identityAlg != null && identityAlg.length() != 0) {
            X509TrustManagerImpl.checkIdentity(session, chain,
                                identityAlg, checkClientTrusted);
        }

        // try the best to check the algorithm constraints
        ProtocolVersion protocolVersion =
            ProtocolVersion.valueOf(session.getProtocol());
        AlgorithmConstraints constraints = null;
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
            if (session instanceof ExtendedSSLSession) {
                ExtendedSSLSession extSession =
                                (ExtendedSSLSession)session;
                String[] peerSupportedSignAlgs =
                        extSession.getLocalSupportedSignatureAlgorithms();

                constraints = new SSLAlgorithmConstraints(
                                sslSocket, peerSupportedSignAlgs, true);
            } else {
                constraints =
                        new SSLAlgorithmConstraints(sslSocket, true);
            }
        } else {
            constraints = new SSLAlgorithmConstraints(sslSocket, true);
        }

        checkAlgorithmConstraints(chain, constraints, checkClientTrusted);
    }
}
 
Example 19
Source File: SSLContextImpl.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private void checkAdditionalTrust(X509Certificate[] chain, String authType,
            Socket socket, boolean isClient) throws CertificateException {
    if (socket != null && socket.isConnected() &&
                                socket instanceof SSLSocket) {

        SSLSocket sslSocket = (SSLSocket)socket;
        SSLSession session = sslSocket.getHandshakeSession();
        if (session == null) {
            throw new CertificateException("No handshake session");
        }

        // check endpoint identity
        String identityAlg = sslSocket.getSSLParameters().
                                    getEndpointIdentificationAlgorithm();
        if (identityAlg != null && identityAlg.length() != 0) {
            String hostname = session.getPeerHost();
            X509TrustManagerImpl.checkIdentity(
                                hostname, chain[0], identityAlg);
        }

        // try the best to check the algorithm constraints
        ProtocolVersion protocolVersion =
            ProtocolVersion.valueOf(session.getProtocol());
        AlgorithmConstraints constraints = null;
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
            if (session instanceof ExtendedSSLSession) {
                ExtendedSSLSession extSession =
                                (ExtendedSSLSession)session;
                String[] peerSupportedSignAlgs =
                        extSession.getLocalSupportedSignatureAlgorithms();

                constraints = new SSLAlgorithmConstraints(
                                sslSocket, peerSupportedSignAlgs, true);
            } else {
                constraints =
                        new SSLAlgorithmConstraints(sslSocket, true);
            }
        } else {
            constraints = new SSLAlgorithmConstraints(sslSocket, true);
        }

        checkAlgorithmConstraints(chain, constraints);
    }
}
 
Example 20
Source File: RpcSocketPool.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
private boolean isAlive(Socket socket) {
	return socket != null && socket.isBound() && !socket.isClosed()
			&& socket.isConnected() && !socket.isInputShutdown()
			&& !socket.isOutputShutdown();
}