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

The following examples show how to use java.net.Socket#isConnected() .
Example 1
Project: bitcoin-verde   File: StratumServer.java    License: MIT License 6 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
Project: openhab1-addons   File: Lib485Connection.java    License: Eclipse Public License 2.0 6 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
Project: bitcoin-verde   File: Environment.java    License: 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
Project: openjsse   File: X509KeyManagerImpl.java    License: 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
Project: hottub   File: X509TrustManagerImpl.java    License: 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
Project: jdk8u_jdk   File: X509KeyManagerImpl.java    License: 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 7
Project: jdk8u-dev-jdk   File: ChatTest.java    License: 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 8
Project: dragonwell8_jdk   File: ChatTest.java    License: 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 9
Project: light-4j   File: ClientX509ExtendedTrustManager.java    License: 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 10
Project: TencentKona-8   File: ChatTest.java    License: 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
Project: protect-baby-monitor   File: ListenActivity.java    License: 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 12
Project: p4ic4idea   File: RpcSocketPool.java    License: 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 13
Project: openjdk-8   File: SSLContextImpl.java    License: 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
Project: openjdk-jdk8u   File: SSLContextImpl.java    License: 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 15
Project: protect-baby-monitor   File: MonitorActivity.java    License: 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 16
Project: p4ic4idea   File: RpcSocketPool.java    License: 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
Project: Bytecoder   File: SSLContextImpl.java    License: 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 18
Project: TencentKona-8   File: X509TrustManagerImpl.java    License: 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 19
Project: hottub   File: SSLContextImpl.java    License: 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
Project: p4ic4idea   File: RpcSocketPool.java    License: 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();
}