当前位置 : 主页 > 编程语言 > java >

java165-inetaddress和inetsocketaddress

来源:互联网 收集:自由互联 发布时间:2022-07-13
import java . net . InetAddress ; import java . net . InetSocketAddress ; import java . net . UnknownHostException ; //inetaddress public class test112 { public static void main ( String [] args ){ try { InetAddress add0 = InetAddress . get

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

//inetaddress
public class test112 {
public static void main(String[] args){
try {
InetAddress add0 = InetAddress.getLocalHost();//获取本机ip地址实例
System.out.println( "本机服务器名称"+add0.getHostName() );
System.out.println( "本地地址"+ add0.getHostAddress() );
InetAddress baiduIp=InetAddress.getByName( "www.baidu.com" );
System.out.println( "百度服务器名称"+baiduIp.getHostName() );
System.out.println( "百度地址"+ baiduIp.getHostAddress() );
}catch (UnknownHostException e){
e.printStackTrace();
}
//创建基于给定的端口号的套接字地址实列
InetSocketAddress isa=new InetSocketAddress( "localhost",9090 );
System.out.println( isa.getHostName() );
System.out.println( isa.getHostString() );
System.out.println( isa.getAddress().getHostAddress() );
}
}

运行结果

java165-inetaddress和inetsocketaddress_.net

 



上一篇:java166-socket服务端和客户端连接
下一篇:没有了
网友评论