Simplify ImapConnection#connect

This commit is contained in:
Jan Berkel 2015-01-14 04:22:04 +01:00
parent 9523a589fc
commit ecd316d0af
2 changed files with 8 additions and 12 deletions

View File

@ -555,14 +555,14 @@ class ImapConnection {
private static Socket connect(ImapSettings settings, TrustedSocketFactory socketFactory)
throws GeneralSecurityException, MessagingException, IOException {
// Try all IPv4 and IPv6 addresses of the host
InetAddress[] addresses = InetAddress.getAllByName(settings.getHost());
for (int i = 0; i < addresses.length; i++) {
Exception connectException = null;
for (InetAddress address : InetAddress.getAllByName(settings.getHost())) {
try {
if (K9MailLib.isDebug() && DEBUG_PROTOCOL_IMAP) {
Log.d(LOG_TAG, "Connecting to " + settings.getHost() + " as " + addresses[i]);
Log.d(LOG_TAG, "Connecting to " + settings.getHost() + " as " + address);
}
SocketAddress socketAddress = new InetSocketAddress(addresses[i], settings.getPort());
SocketAddress socketAddress = new InetSocketAddress(address, settings.getPort());
Socket socket;
if (settings.getConnectionSecurity() == ConnectionSecurity.SSL_TLS_REQUIRED) {
socket = socketFactory.createSocket(
@ -576,15 +576,12 @@ class ImapConnection {
socket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
// Successfully connected to the server; don't try any other addresses
return socket;
} catch (SocketException e) {
if (i < (addresses.length - 1)) {
// There are still other addresses for that host to try
continue;
}
throw new MessagingException("Cannot connect to host", e);
} catch (IOException e) {
Log.w(LOG_TAG, "could not connect to "+address, e);
connectException = e;
}
}
throw new MessagingException("Cannot connect to host");
throw new MessagingException("Cannot connect to host", connectException);
}
private void adjustDNSCacheTTL() {

View File

@ -15,7 +15,6 @@ import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.MessagingException;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;