InetSocketAddress
2019-07-09 22:33 更新
InetSocketAddress测试: 解决端口问题。
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
public class Test {
public static void main(String[] args) throws UnknownHostException {
/**
* 封装端口:在InetAddress基础上+端口。
*/
//127.0.0.1代表本机,相当于localhost。
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 9999);
/**
* new InetSocketAddress("127.0.0.1", 9999);内部调用了
* new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999);
* 即进行了封装。
*/
// address = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999);
System.out.println(address.getHostName());
System.out.println(address.getPort());
InetAddress inetAddress = address.getAddress();
System.out.println(inetAddress.getHostAddress());
System.out.println(inetAddress.getHostName());
}
}
以上内容是否对您有帮助:
更多建议: