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
This commit is contained in:
committed by
Paul Jensen
parent
160638d7e4
commit
54a8d4c170
@@ -25,6 +25,7 @@ import java.net.InetAddress;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.SocketAddress;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@@ -106,27 +107,27 @@ public class Network implements Parcelable {
|
|||||||
mNetId = netId;
|
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.
|
// Lookup addresses only on this Network.
|
||||||
InetAddress[] hostAddresses = getAllByName(host);
|
InetAddress[] hostAddresses = getAllByName(host);
|
||||||
// Try all but last address ignoring exceptions.
|
// Try all addresses.
|
||||||
for (int i = 0; i < hostAddresses.length - 1; i++) {
|
for (int i = 0; i < hostAddresses.length; i++) {
|
||||||
try {
|
try {
|
||||||
|
Socket socket = createSocket();
|
||||||
|
if (localAddress != null) socket.bind(localAddress);
|
||||||
socket.connect(new InetSocketAddress(hostAddresses[i], port));
|
socket.connect(new InetSocketAddress(hostAddresses[i], port));
|
||||||
return;
|
return socket;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
if (i == (hostAddresses.length - 1)) throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Try last address. Do throw exceptions.
|
throw new UnknownHostException(host);
|
||||||
socket.connect(new InetSocketAddress(hostAddresses[hostAddresses.length - 1], port));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
|
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
|
||||||
Socket socket = createSocket();
|
return connectToHost(host, port, new InetSocketAddress(localHost, localPort));
|
||||||
socket.bind(new InetSocketAddress(localHost, localPort));
|
|
||||||
connectToHost(socket, host, port);
|
|
||||||
return socket;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -147,9 +148,7 @@ public class Network implements Parcelable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Socket createSocket(String host, int port) throws IOException {
|
public Socket createSocket(String host, int port) throws IOException {
|
||||||
Socket socket = createSocket();
|
return connectToHost(host, port, null);
|
||||||
connectToHost(socket, host, port);
|
|
||||||
return socket;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user