集册 Java实例教程 表示套接字地址

表示套接字地址

欢马劈雪     最近更新时间:2020-01-02 10:19:05

530
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
创建一个InetSocketAddress对象

import java.net.InetSocketAddress;

/** from 
n o w j a   v  a . c o m - 时  代  Java**/

public class Main {

  public static void main(String[] args) {

    InetSocketAddress addr1 = new InetSocketAddress("::1", 12889);

    printSocketAddress(addr1);


    InetSocketAddress addr2 = InetSocketAddress.createUnresolved("::1", 12881);

    printSocketAddress(addr2);

  }


  public static void printSocketAddress(InetSocketAddress sAddr) {

    System.out.println("Socket Address: " + sAddr.getAddress());

    System.out.println("Socket Host Name: " + sAddr.getHostName());

    System.out.println(
展开阅读全文