两者区别:SocketAddress是一个抽象类,而InetSocketAddress 是SocketAddress的子类。
示例:
InetAddress address = InetAddress.getLocalHost();
String hostName = address.getHostName();
String hostAddr = address.getHostAddress();
System.out.println("主机名:" + hostNmae + ", ip地址:" + hostAddr;
InetAddress add = InetAddress.getByName("BOPZKQZ9SSY5ECY");
System.out.println(add.getHostAddress());
final Socket socket = new Socket();
SocketAddress address = new InetSocketAddress("www.fortify.net", 443);
try {
socket.connect(address);
} catch (IOException e) {}
// 连接远程主机
Thread reader = new Thread() {
@Override
public void run() {
try {
byte[] buffer = new byte[512];
InputStream stream = socket.getInputStream();
socket.getInputStream().read(buffer);
} catch (Exception ex) {
}
}
};
reader.start();