当前位置: 首页>>代码示例>>Java>>正文


Java SSLSocket.setSoTimeout方法代码示例

本文整理汇总了Java中javax.net.ssl.SSLSocket.setSoTimeout方法的典型用法代码示例。如果您正苦于以下问题:Java SSLSocket.setSoTimeout方法的具体用法?Java SSLSocket.setSoTimeout怎么用?Java SSLSocket.setSoTimeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.net.ssl.SSLSocket的用法示例。


在下文中一共展示了SSLSocket.setSoTimeout方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createSSLSocket

import javax.net.ssl.SSLSocket; //导入方法依赖的package包/类
private static TSocket createSSLSocket(SSLSocketFactory factory, String host,
                                       int port, int timeout, List<String> excludeProtocols)
    throws FlumeException {
  try {
    SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
    socket.setSoTimeout(timeout);

    List<String> enabledProtocols = new ArrayList<String>();
    for (String protocol : socket.getEnabledProtocols()) {
      if (!excludeProtocols.contains(protocol)) {
        enabledProtocols.add(protocol);
      }
    }
    socket.setEnabledProtocols(enabledProtocols.toArray(new String[0]));
    return new TSocket(socket);
  } catch (Exception e) {
    throw new FlumeException("Could not connect to " + host + " on port " + port, e);
  }
}
 
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:20,代码来源:ThriftRpcClient.java

示例2: get

import javax.net.ssl.SSLSocket; //导入方法依赖的package包/类
public static void get(String host, int port, KeyStore keyStore) throws Exception {
  TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tmf.init(keyStore);
  
  X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0];
  SavingTrustManager tm = new SavingTrustManager(defaultTrustManager);
  
  SSLContext sslContext = SSLContext.getInstance("TLS");
  sslContext.init(null, new TrustManager[] {tm}, null);
  
  LOGGER.info("Iniciando conexão com: " + host + ":" + port + "...");
  SSLSocket socket = (SSLSocket) sslContext.getSocketFactory().createSocket(host, port);
  
  try {
    socket.setSoTimeout(30 * 1000);
    socket.startHandshake();
    socket.close();
  } catch (Exception e) {
    LOGGER.info(e.toString());
  } 

  X509Certificate[] chain = tm.chain;
  if (chain == null) {
    LOGGER.info("Não foi possivel obter a cadeia de certificados");
  }

  LOGGER.info("O servidor enviou " + chain.length + " certificado(s):");
  MessageDigest sha1 = MessageDigest.getInstance("SHA1");
  MessageDigest md5 = MessageDigest.getInstance("MD5");
  for (int i = 0; i < chain.length; i++) {
    X509Certificate cert = chain[i];
    sha1.update(cert.getEncoded());
    md5.update(cert.getEncoded());

    String alias = host + "-" + (i);
    keyStore.setCertificateEntry(alias, cert);
    LOGGER.info("Certificado adicionado usando alias: '" + alias + "'");
  }
}
 
开发者ID:pablopdomingos,项目名称:nfse,代码行数:40,代码来源:NFSeGeraCadeiaCertificados.java

示例3: createSSLSocket

import javax.net.ssl.SSLSocket; //导入方法依赖的package包/类
/**
 * Create an SSL client socket using the IOR-encoded
 * security characteristics.
 * Setting want/need client auth on a client socket has no effect so all we can do is use the right host, port, ciphers
 *
 * @param host     The target host name.
 * @param port     The target connection port.
 *
 * @return An appropriately configured client SSLSocket.
 * @exception IOException if ssl socket can't be obtained and configured.
 */
private Socket createSSLSocket(String host, int port, int requires, int supports) throws IOException {
    SSLSocketFactory factory = getSocketFactory();
    SSLSocket socket = (SSLSocket) factory.createSocket(host, port);

    socket.setSoTimeout(SOCKET_TIMEOUT_MS);

    // get a set of cipher suites appropriate for this connections requirements.
    // We request this for each connection, since the outgoing IOR's requirements may be different from
    // our server listener requirements.
    String[] iorSuites = SSLCipherSuiteDatabase.getCipherSuites(requires, supports, factory.getSupportedCipherSuites());
    socket.setEnabledCipherSuites(iorSuites);
    if (log.isDebugEnabled()) {
        log.debug("Created SSL socket to " + host + ":" + port);
        log.debug("    cipher suites:");

        for (int i = 0; i < iorSuites.length; i++) {
            log.debug("    " + iorSuites[i]);
        }
        socket.addHandshakeCompletedListener(new HandshakeCompletedListener() {

            public void handshakeCompleted(HandshakeCompletedEvent handshakeCompletedEvent) {
                Certificate[] certs = handshakeCompletedEvent.getLocalCertificates();
                if (certs != null) {
                    log.debug("handshake returned local certs count: " + certs.length);
                    for (int i = 0; i < certs.length; i++) {
                        Certificate cert = certs[i];
                        log.debug("cert: " + cert.toString());
                    }
                } else {
                    log.debug("handshake returned no local certs");
                }
            }
        });
    }
    return socket;
}
 
开发者ID:RuiChen08,项目名称:dacapobench,代码行数:48,代码来源:SocketFactory.java

示例4: testSSLSocket

import javax.net.ssl.SSLSocket; //导入方法依赖的package包/类
private boolean testSSLSocket(SSLSocket sslSocket) {
       try {
        if(DEBUG_ALL) System.out.println("Starting SSL handshake");
     sslSocket.setSoTimeout(0);
           sslSocket.startHandshake();
           return true;
       } catch (Exception e) {
        if(DEBUG_ALL) e.printStackTrace();
           return false;
       }
}
 
开发者ID:jeffreyshen19,项目名称:Virtual-IoT-Server,代码行数:12,代码来源:SSLClientSocket.java

示例5: doServerSide

import javax.net.ssl.SSLSocket; //导入方法依赖的package包/类
void doServerSide() throws Exception {
    SSLContext context = generateSSLContext(false);
    SSLServerSocketFactory sslssf = context.getServerSocketFactory();
    SSLServerSocket sslServerSocket =
        (SSLServerSocket)sslssf.createServerSocket(serverPort);
    serverPort = sslServerSocket.getLocalPort();

    /*
     * Signal Client, we're ready for his connect.
     */
    serverReady = true;

    SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept();
    try {
        sslSocket.setSoTimeout(5000);
        sslSocket.setSoLinger(true, 5);

        InputStream sslIS = sslSocket.getInputStream();
        OutputStream sslOS = sslSocket.getOutputStream();

        sslIS.read();
        sslOS.write('A');
        sslOS.flush();

        throw new Exception("EC curve secp224k1 should be disabled");
    } catch (SSLHandshakeException she) {
        // expected exception: no cipher suites in common
        System.out.println("Expected exception: " + she);
    } finally {
        sslSocket.close();
        sslServerSocket.close();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:ECCurvesconstraints.java

示例6: doClientSide

import javax.net.ssl.SSLSocket; //导入方法依赖的package包/类
void doClientSide() throws Exception {

        /*
         * Wait for server to get started.
         */
        while (!serverReady) {
            Thread.sleep(50);
        }

        SSLContext context = generateSSLContext(true);
        SSLSocketFactory sslsf = context.getSocketFactory();

        SSLSocket sslSocket =
            (SSLSocket)sslsf.createSocket("localhost", serverPort);

        try {
            sslSocket.setSoTimeout(5000);
            sslSocket.setSoLinger(true, 5);

            InputStream sslIS = sslSocket.getInputStream();
            OutputStream sslOS = sslSocket.getOutputStream();

            sslOS.write('B');
            sslOS.flush();
            sslIS.read();

            throw new Exception("EC curve secp224k1 should be disabled");
        } catch (SSLHandshakeException she) {
            // expected exception: Received fatal alert
            System.out.println("Expected exception: " + she);
        } finally {
            sslSocket.close();
        }
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:ECCurvesconstraints.java

示例7: connectSocket

import javax.net.ssl.SSLSocket; //导入方法依赖的package包/类
/**
 * @since 4.1
 */
public Socket connectSocket(final Socket sock, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpParams params) throws IOException
{
	if (remoteAddress == null)
	{
		throw new IllegalArgumentException("Remote address may not be null");
	}

	if (params == null)
	{
		throw new IllegalArgumentException("HTTP parameters may not be null");
	}

	SSLSocket sslSocket = (SSLSocket) (sock != null ? sock : createSocket());

	if (localAddress != null)
	{
		//            sslSocket.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
		sslSocket.bind(localAddress);
	}

	int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
	int soTimeout = HttpConnectionParams.getSoTimeout(params);

	try
	{
		sslSocket.connect(remoteAddress, connTimeout);
	}
	catch (SocketTimeoutException ex)
	{
		throw new ConnectTimeoutException(String.format("Connect to %s/%s timed out", remoteAddress.getHostName(), remoteAddress.getAddress()));
	}

	sslSocket.setSoTimeout(soTimeout);

	if (this.hostnameVerifier != null)
	{
		try
		{
			this.hostnameVerifier.verify(remoteAddress.getHostName(), sslSocket);
			// verifyHostName() didn't blowup - good!
		}
		catch (IOException iox)
		{
			// close the socket before re-throwing the exception
			try
			{
				sslSocket.close();
			}
			catch (Exception x)
			{ /*ignore*/ }
			throw iox;
		}
	}

	return sslSocket;
}
 
开发者ID:ultrasonic,项目名称:ultrasonic,代码行数:60,代码来源:SSLSocketFactory.java

示例8: upgradeToTls

import javax.net.ssl.SSLSocket; //导入方法依赖的package包/类
/**
 * Create an {@code SSLSocket} and perform the TLS handshake and certificate
 * validation.
 */
private void upgradeToTls(TunnelRequest tunnelRequest) throws IOException {
  Platform platform = Platform.get();

  // Make an SSL Tunnel on the first message pair of each SSL + proxy connection.
  if (requiresTunnel()) {
    makeTunnel(tunnelRequest);
  }

  // Create the wrapper over connected socket.
  socket = route.address.sslSocketFactory
      .createSocket(socket, route.address.uriHost, route.address.uriPort, true /* autoClose */);
  SSLSocket sslSocket = (SSLSocket) socket;
  if (route.modernTls) {
    platform.enableTlsExtensions(sslSocket, route.address.uriHost);
  } else {
    platform.supportTlsIntolerantServer(sslSocket);
  }

  boolean useNpn = route.modernTls && route.address.transports.contains("spdy/3");
  if (useNpn) {
    platform.setNpnProtocols(sslSocket, NPN_PROTOCOLS);
  }

  // Force handshake. This can throw!
  sslSocket.startHandshake();

  // Verify that the socket's certificates are acceptable for the target host.
  if (!route.address.hostnameVerifier.verify(route.address.uriHost, sslSocket.getSession())) {
    throw new IOException("Hostname '" + route.address.uriHost + "' was not verified");
  }

  out = sslSocket.getOutputStream();
  in = sslSocket.getInputStream();
  streamWrapper();

  byte[] selectedProtocol;
  if (useNpn && (selectedProtocol = platform.getNpnSelectedProtocol(sslSocket)) != null) {
    if (Arrays.equals(selectedProtocol, SPDY3)) {
      sslSocket.setSoTimeout(0); // SPDY timeouts are set per-stream.
      spdyConnection = new SpdyConnection.Builder(route.address.getUriHost(), true, in, out)
          .build();
      spdyConnection.sendConnectionHeader();
    } else if (!Arrays.equals(selectedProtocol, HTTP_11)) {
      throw new IOException(
          "Unexpected NPN transport " + new String(selectedProtocol, "ISO-8859-1"));
    }
  }
}
 
开发者ID:aabognah,项目名称:LoRaWAN-Smart-Parking,代码行数:53,代码来源:Connection.java

示例9: connectSocket

import javax.net.ssl.SSLSocket; //导入方法依赖的package包/类
@Override
public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress,
		int localPort, HttpParams params) throws IOException, UnknownHostException,
		ConnectTimeoutException {
	if (host == null) {
		throw new IllegalArgumentException("Target host may not be null.");
	}
	if (params == null) {
		throw new IllegalArgumentException("Parameters may not be null.");
	}

	SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());

	if ((localAddress != null) || (localPort > 0)) {
		if (localPort < 0)
			localPort = 0;

		InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
		sslsock.bind(isa);
	}

	int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
	int soTimeout = HttpConnectionParams.getSoTimeout(params);

	InetSocketAddress remoteAddress = new InetSocketAddress(host, port);

	sslsock.connect(remoteAddress, connTimeout);

	sslsock.setSoTimeout(soTimeout);
	try {
		hostnameVerifier.verify(host, sslsock);
	} catch (IOException iox) {
		try {
			sslsock.close();
		} catch (Exception x) {
		}

		throw iox;
	}

	return sslsock;
}
 
开发者ID:CactusSoft,项目名称:zabbkit-android,代码行数:43,代码来源:MySSLSocketFactory.java


注:本文中的javax.net.ssl.SSLSocket.setSoTimeout方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。