For null hostnames, use the loopback InetAddress (for backward-compatibility).

This commit is contained in:
David Solin
2016-08-16 10:26:57 -05:00
parent 4183776adb
commit 1caa7ac722

View File

@@ -118,18 +118,26 @@ public abstract class SocketClient {
} }
public void connect(String hostname, int port) throws IOException { public void connect(String hostname, int port) throws IOException {
this.hostname = hostname; if (hostname == null) {
socket = socketFactory.createSocket(); connect(InetAddress.getByName(null), port);
socket.connect(new InetSocketAddress(hostname, port), connectTimeout); } else {
onConnect(); this.hostname = hostname;
socket = socketFactory.createSocket();
socket.connect(new InetSocketAddress(hostname, port), connectTimeout);
onConnect();
}
} }
public void connect(String hostname, int port, InetAddress localAddr, int localPort) throws IOException { public void connect(String hostname, int port, InetAddress localAddr, int localPort) throws IOException {
this.hostname = hostname; if (hostname == null) {
socket = socketFactory.createSocket(); connect(InetAddress.getByName(null), port, localAddr, localPort);
socket.bind(new InetSocketAddress(localAddr, localPort)); } else {
socket.connect(new InetSocketAddress(hostname, port), connectTimeout); this.hostname = hostname;
onConnect(); socket = socketFactory.createSocket();
socket.bind(new InetSocketAddress(localAddr, localPort));
socket.connect(new InetSocketAddress(hostname, port), connectTimeout);
onConnect();
}
} }
public void connect(InetAddress host) throws IOException { public void connect(InetAddress host) throws IOException {