Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Subnet iteration #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/main/net/sourceforge/jtds/jdbc/SharedSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -37,7 +39,7 @@

import javax.net.SocketFactory;

import net.sourceforge.jtds.ssl.*;
import net.sourceforge.jtds.ssl.SocketFactories;
import net.sourceforge.jtds.util.Logger;

/**
Expand Down Expand Up @@ -275,18 +277,37 @@ private Socket createSocketForJDBC3( JtdsConnection connection ) throws IOExcept
final String bindAddress = connection.getBindAddress();
final int loginTimeout = connection.getLoginTimeout();

Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress( host, port );
InetSocketAddress bindSocketAddress = null;

// call Socket.bind(SocketAddress) if bindAddress parameter is set
if( bindAddress != null && ! bindAddress.isEmpty() )
{
socket.bind( new InetSocketAddress( bindAddress, 0 ) );
bindSocketAddress = new InetSocketAddress( bindAddress, 0 );
}

// establish connection
socket.connect( address, loginTimeout * 1000 );
return socket;
InetAddress[] addresses = InetAddress.getAllByName(host);
String exception = "";

int timeout = loginTimeout * 100 / addresses.length;

for (int i = 0, length = addresses.length; i < length; i++) {
InetAddress address = addresses[i];

Socket socket = new Socket();
if (bindAddress != null) {
socket.bind(bindSocketAddress);
}

InetSocketAddress socketAddress = new InetSocketAddress(address, port);
try {
socket.connect(socketAddress, timeout);
return socket;
} catch (SocketTimeoutException e) {
exception = "Unable to connect to " + address.getHostAddress() + ", " + e.getMessage() + ";";
}
}

throw new IOException(exception);
}

String getMAC()
Expand Down