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);內(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());

        
    }
}
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號