W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
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);內(nèi)部調(diào)用了
* 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());
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: