Chr*_*ris 8 java sockets android timeout
我的代码在这里:
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, getAllCerts(), new SecureRandom());
SSLSocketFactory factory = sslContext.getSocketFactory();
mSocket = (SSLSocket) factory.createSocket("myhost.com", socketPort[index]);
Run Code Online (Sandbox Code Playgroud)
我必须检查端口表并选择打开端口.一切正常,但在createSocket()上,我的应用程序会耗费大量时间.如果我有5个端口,最后一个打开连接大约需要3分钟.
如何在SSLSocketFactory上设置超时以加速连接?
如果您仍在疑惑,可以使用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);
Run Code Online (Sandbox Code Playgroud)
尝试包装现有的套接字:
Socket socketConn = new Socket();
socketConn.connect(addr, DEFAULT_CONNECT_TIMEOUT);
mSocket = socketConn.getSocketFactory().createSocket(socketConn, hostname, port, true);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10334 次 |
| 最近记录: |