From 54a8d4c17036d7308d53dff2a0e30ece9d6159a3 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Wed, 30 Jul 2014 17:17:13 +0900 Subject: [PATCH] Use a new socket for each of the host's IP addresses. If Socket.connect() times out, the socket cannot be used any more - any attempt to do so fails with EBADF. Use a new socket for each IP address. Bug: 16664129 Change-Id: If3616df86f7c2da0eabd30dca5db65d0da85cb17 --- core/java/android/net/Network.java | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/core/java/android/net/Network.java b/core/java/android/net/Network.java index 97238f19e1..5e3decda77 100644 --- a/core/java/android/net/Network.java +++ b/core/java/android/net/Network.java @@ -25,6 +25,7 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.Socket; +import java.net.SocketAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.net.URL; @@ -106,27 +107,27 @@ public class Network implements Parcelable { mNetId = netId; } - private void connectToHost(Socket socket, String host, int port) throws IOException { + private Socket connectToHost(String host, int port, SocketAddress localAddress) + throws IOException { // Lookup addresses only on this Network. InetAddress[] hostAddresses = getAllByName(host); - // Try all but last address ignoring exceptions. - for (int i = 0; i < hostAddresses.length - 1; i++) { + // Try all addresses. + for (int i = 0; i < hostAddresses.length; i++) { try { + Socket socket = createSocket(); + if (localAddress != null) socket.bind(localAddress); socket.connect(new InetSocketAddress(hostAddresses[i], port)); - return; + return socket; } catch (IOException e) { + if (i == (hostAddresses.length - 1)) throw e; } } - // Try last address. Do throw exceptions. - socket.connect(new InetSocketAddress(hostAddresses[hostAddresses.length - 1], port)); + throw new UnknownHostException(host); } @Override public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException { - Socket socket = createSocket(); - socket.bind(new InetSocketAddress(localHost, localPort)); - connectToHost(socket, host, port); - return socket; + return connectToHost(host, port, new InetSocketAddress(localHost, localPort)); } @Override @@ -147,9 +148,7 @@ public class Network implements Parcelable { @Override public Socket createSocket(String host, int port) throws IOException { - Socket socket = createSocket(); - connectToHost(socket, host, port); - return socket; + return connectToHost(host, port, null); } @Override