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

The following are Jave code examples for showing how to use isConnected() of the java.net.Socket class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: incubator-servicecomb-java-chassis   File: TrustManagerExt.java   Source Code and License Vote up 6 votes
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType,
    Socket socket) throws CertificateException {
  if (!option.isAuthPeer()) {
    return;
  }

  String ip = null;
  if (socket != null && socket.isConnected()
      && socket instanceof SSLSocket) {
    InetAddress inetAddress = socket.getInetAddress();
    if (inetAddress != null) {
      ip = inetAddress.getHostAddress();
    }
  }
  checkTrustedCustom(chain, ip);
  trustManager.checkClientTrusted(chain, authType, socket);
}
 
Example 2
Project: incubator-servicecomb-java-chassis   File: TrustManagerExt.java   Source Code and License Vote up 6 votes
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType,
    Socket socket) throws CertificateException {
  if (!option.isAuthPeer()) {
    return;
  }

  String ip = null;
  if (socket != null && socket.isConnected()
      && socket instanceof SSLSocket) {
    InetAddress inetAddress = socket.getInetAddress();
    if (inetAddress != null) {
      ip = inetAddress.getHostAddress();
    }
  }
  checkTrustedCustom(chain, ip);
  trustManager.checkServerTrusted(chain, authType, socket);
}
 
Example 3
Project: nCube-Thyme-Java   File: TasSender.java   Source Code and License Vote up 6 votes
/**
 * sendMessage Method
 * Send the message to TAS for control
 * @param containerName
 * @param content
 * @throws IOException
 */
public static void sendMessage(String containerName, String content) throws IOException {
	ArrayList<Container> containers = ResourceRepository.getContainersInfo();		
	Container tempContainer;
	
	for (int i = 0; i < containers.size(); i++) {
		tempContainer = containers.get(i);
		
		if (containerName.equals(tempContainer.ctname)) {
			Socket tasSocket = tempContainer.tasSocket;
			if (tasSocket.isConnected()) {
				PrintWriter writer = new PrintWriter(tasSocket.getOutputStream(), true);
				writer.println(content);
				System.out.println("[&CubeThyme] Send to TAS \"" + content + "\"\n");
			}
			else {
				System.out.println("[&CubeThyme] TAS connection is closed\n");
			}
		}
		else {
			System.out.println("[&CubeThyme] Could not found container\n");
		}
	}
}
 
Example 4
Project: hadoop   File: TestPortmap.java   Source Code and License Vote up 6 votes
@Test(timeout = 1000)
public void testIdle() throws InterruptedException, IOException {
  Socket s = new Socket();
  try {
    s.connect(pm.getTcpServerLocalAddress());

    int i = 0;
    while (!s.isConnected() && i < RETRY_TIMES) {
      ++i;
      Thread.sleep(SHORT_TIMEOUT_MILLISECONDS);
    }

    Assert.assertTrue("Failed to connect to the server", s.isConnected()
        && i < RETRY_TIMES);

    int b = s.getInputStream().read();
    Assert.assertTrue("The server failed to disconnect", b == -1);
  } finally {
    s.close();
  }
}
 
Example 5
Project: blackbird   File: NetworkConnection.java   Source Code and License Vote up 5 votes
/**
 * Creates a connection using a connected socket
 *
 * @param socket the socket this connection is based on
 * @throws IOException if the socket is either closed, unbound or not connected.
 */
public NetworkConnection(Socket socket) throws IOException {
    if (socket.isClosed() || !socket.isBound() || !socket.isConnected())
        throw new IOException("NetworkConnection expects a not closed, bound and connected socket");

    this.socket = socket;
}
 
Example 6
Project: KeYExperienceReport   File: Client.java   Source Code and License Vote up 5 votes
public Client(final String ip, int port) throws UnknownHostException,
		IOException, ClassNotFoundException {
	Socket socket = new Socket(ip, port);
	ObjectOutputStream outToClient = new ObjectOutputStream(
			socket.getOutputStream());
	ObjectInputStream inFromClient = new ObjectInputStream(
			socket.getInputStream());

	long startTime = System.currentTimeMillis();
	long currentTime = System.currentTimeMillis();
	while (socket.isConnected() && jobs < MAX_JOBS
			&& (currentTime - startTime) < MAX_RUNTIME) {
		jobs++;
		ResultRunnable job = (ResultRunnable) inFromClient.readObject();
		job.setIPAdress(ip);
		job.run();
		Object result = job.getResult();
		try {
			outToClient.writeObject(result);
		} catch (NotSerializableException e) {
			outToClient.writeObject(e);
		}
		currentTime = System.currentTimeMillis();
	}

	if (!socket.isClosed()) {
		restart = true;
	}

	socket.close();
}
 
Example 7
Project: OpenJSharp   File: X509KeyManagerImpl.java   Source Code and License Vote up 5 votes
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
Project: OpenJSharp   File: X509TrustManagerImpl.java   Source Code and License Vote up 5 votes
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 9
Project: jdk8u-jdk   File: X509KeyManagerImpl.java   Source Code and License Vote up 5 votes
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 10
Project: jdk8u-jdk   File: X509TrustManagerImpl.java   Source Code and License Vote up 5 votes
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 11
Project: jdk8u-jdk   File: ChatTest.java   Source Code and License Vote up 5 votes
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 12
Project: openjdk-jdk10   File: X509KeyManagerImpl.java   Source Code and License Vote up 5 votes
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.useTLS12PlusSpec()) {
                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 13
Project: nativead   File: IOUtils.java   Source Code and License Vote up 5 votes
public static void closeSocket(Socket socket) {
	if (socket != null) {
		if (socket.isConnected()) {
			try {
				socket.close();
			} catch (IOException e) {
				DLog.e(e);
			}
		}
	}
}
 
Example 14
Project: openjdk-jdk10   File: X509TrustManagerImpl.java   Source Code and License Vote up 5 votes
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 15
Project: OpenJSharp   File: SSLContextImpl.java   Source Code and License Vote up 4 votes
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 16
Project: OpenJSharp   File: X509TrustManagerImpl.java   Source Code and License Vote up 4 votes
private void checkTrusted(X509Certificate[] chain, String authType,
            Socket socket, boolean isClient) throws CertificateException {
    Validator v = checkTrustedInit(chain, authType, isClient);

    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[0], identityAlg, isClient,
                    getRequestedServerNames(socket));
        }

        // 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 (isClient) {
        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 17
Project: jdk8u-jdk   File: SSLContextImpl.java   Source Code and License Vote up 4 votes
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 18
Project: jdk8u-jdk   File: X509TrustManagerImpl.java   Source Code and License Vote up 4 votes
private void checkTrusted(X509Certificate[] chain, String authType,
            Socket socket, boolean isClient) throws CertificateException {
    Validator v = checkTrustedInit(chain, authType, isClient);

    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[0], identityAlg, isClient,
                    getRequestedServerNames(socket));
        }

        // 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 (isClient) {
        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: openjdk-jdk10   File: X509TrustManagerImpl.java   Source Code and License Vote up 4 votes
private void checkTrusted(X509Certificate[] chain, String authType,
            Socket socket, boolean isClient) throws CertificateException {
    Validator v = checkTrustedInit(chain, authType, isClient);

    X509Certificate[] trustedChain = 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");
        }

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

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

        // Grab any stapled OCSP responses for use in validation
        List<byte[]> responseList = Collections.emptyList();
        if (!isClient && isExtSession) {
            responseList =
                    ((ExtendedSSLSession)session).getStatusResponses();
        }
        trustedChain = validate(v, chain, responseList,
                constraints, isClient ? null : authType);

        // check if EE certificate chains to a public root CA (as
        // pre-installed in cacerts)
        boolean chainsToPublicCA =
            AnchorCertificates.contains(trustedChain[trustedChain.length-1]);

        // check endpoint identity
        String identityAlg = sslSocket.getSSLParameters().
                getEndpointIdentificationAlgorithm();
        if (identityAlg != null && identityAlg.length() != 0) {
            checkIdentity(session, trustedChain[0], identityAlg, isClient,
                    getRequestedServerNames(socket), chainsToPublicCA);
        }
    } else {
        trustedChain = validate(v, chain, Collections.emptyList(),
                null, isClient ? null : authType);
    }
    if (debug != null && Debug.isOn("trustmanager")) {
        System.out.println("Found trusted certificate:");
        System.out.println(trustedChain[trustedChain.length - 1]);
    }
}
 
Example 20
Project: RxNetwork   File: SocketInternetObservingStrategy.java   Source Code and License Vote up 4 votes
private boolean isSocketConnected(Socket socket) {
  return socket.isConnected();
}