switch from gethostbyname to getaddrinfo on POSIX systems

gethostbyname may return any combination of IPv4 and IPv6 addresses,
and it's not safe to assume the first address is IPv4, which is all
our code is currently prepared to handle.  In contrast, getaddrinfo
allows us to specify whether we want IPv4, IPv6, or both.

We should eventually make this switch on Windows as well, but the
status of getaddrinfo in Windows 2000 is not clear, and MinGW's
ws2tcpip.h only declares it for XP and above.

This commit also adds InetAddress.getByName for explicit DNS lookups.
This commit is contained in:
Joel Dice
2010-06-14 16:09:56 -06:00
parent 686f1ba983
commit 47d9039b69
9 changed files with 194 additions and 20 deletions

View File

@ -18,7 +18,7 @@ public abstract class Selector {
protected final Set<SelectionKey> keys = new HashSet();
protected final Set<SelectionKey> selectedKeys = new HashSet();
public static Selector open() {
public static Selector open() throws IOException {
return new SocketSelector();
}

View File

@ -15,11 +15,16 @@ import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSocketChannel extends SelectableChannel {
private final SocketChannel channel = new SocketChannel();
private final SocketChannel channel;
public static ServerSocketChannel open() {
private ServerSocketChannel() throws IOException {
channel = new SocketChannel();
}
public static ServerSocketChannel open() throws IOException {
return new ServerSocketChannel();
}
@ -58,6 +63,8 @@ public class ServerSocketChannel extends SelectableChannel {
}
private int doListen(String host, int port) throws IOException {
Socket.init();
return natDoListen(host, port);
}

View File

@ -26,7 +26,9 @@ public class SocketChannel extends SelectableChannel
boolean connected = false;
boolean blocking = true;
public static SocketChannel open() {
public static SocketChannel open() throws IOException {
Socket.init();
return new SocketChannel();
}

View File

@ -12,13 +12,16 @@ package java.nio.channels;
import java.io.IOException;
import java.util.Iterator;
import java.net.Socket;
class SocketSelector extends Selector {
protected long state;
protected final Object lock = new Object();
protected boolean woken = false;
public SocketSelector() {
public SocketSelector() throws IOException {
Socket.init();
state = natInit();
}