简体   繁体   中英

Socket getOutputStream takes too long

Socket socket = sslSocketFactory.createSocket();
socket.connect(new InetSocketAddress(hostname, port), 5000);
OutputStream out = socket.getOutputStream(); // this takes too long

The connect method returned immediately, but getOutputStream stuck for too long.

It is not always too long just for some socket address
For HTTPS (port 443) getOutputStream() worked well but for HTTP (port 80) it stuck

For HTTPS (port 443) getOutputStream() worked well but for HTTP (port 80) it stuck

With an SSLSocket ? Be serious. Port 80 is plaintext. It won't respond to an SSL handshake, at least not as expected. I'm surprised you didn't get a handshake exception.

Use a plaintext socket for HTTP, and SSL for HTTPS. Actually you should use HttpURLConnection for both and stop trying to roll your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM