我的代码在这里:
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, getAllCerts(), new SecureRandom());
SSLSocketFactory factory = sslContext.getSocketFactory();
mSocket = (SSLSocket) factory.createSocket("myhost.com", socketPort[index]);
我必须检查端口表并选择打开端口。一切正常,但在createSocket()上,我的应用程序会耗费大量时间。如果我有5个端口,而最后一个打开连接大约需要3分钟。
如何在SSLSocketFactory上设置超时以加速连接?
答案 0 :(得分:9)
如果您仍然想知道,可以使用https://groups.google.com/forum/#!topic/android-developers/EYtMO5WoXXI
中给出的想法import javax.net.ssl.SSLSocketFactory;
// Create a socket without connecting
SSLSocketFactory socketFactory = SSLSocketFactory.getDefault();
Socket socket = socketFactory.createSocket();
// Connect, with an explicit timeout value
socket.connect(new InetSocketAddress(endpoint.mServer,
endpoint.mPort), CONNECT_TIMEOUT);
答案 1 :(得分:2)
尝试换行包装现有套接字:
Socket socketConn = new Socket();
socketConn.connect(addr, DEFAULT_CONNECT_TIMEOUT);
mSocket = socketConn.getSocketFactory().createSocket(socketConn, hostname, port, true);