zoukankan      html  css  js  c++  java
  • 网络编程

    import java.net.InetAddress;

    /**

     * 网络通信的一个要素:IP地址,通过IP地址,唯一的定位互联网上一台主机

     * 

     * InetAddress位于java.net包下

     *InetAddress用来代表IP地址,一个该对象就代表一个IP地址

     *1.如何创建InetAddress的对象,getByName(String host);

     *2.getHostName();获取了域名

     *3.getHostAddress();获取了IP地址

     * 

     */

    public class TestInetAddress {

    public static void main(String[] args) throws Exception {

    InetAddress inet = InetAddress.getByName("www.atguigu.com");

    System.out.println("inet==>"+inet);

    //两个方法

    System.out.println("inet.getHostName()==>"+inet.getHostName());

    System.out.println("inet.getHostAddress()==>"+inet.getHostAddress());

    //获取本机ip:getLocalhost();

    InetAddress inet1 = InetAddress.getLocalHost();

    System.out.println("inet1==>"+inet1);

    System.out.println("inet1.getHostName()==>"+inet1.getHostName());

    System.out.println("inet1.getHostAddress()==>"+inet1.getHostAddress());

    }

    }

    打印结果是:

    inet==>www.atguigu.com/42.121.6.2

    inet.getHostName()==>www.atguigu.com

    inet.getHostAddress()==>42.121.6.2

    inet1==>lixiumingdeMacBook-Air.local/192.168.2.102

    inet1.getHostName()==>lixiumingdeMacBook-Air.local

    inet1.getHostAddress()==>192.168.2.102

  • 相关阅读:
    (转)Apache与Tomcat 区别联系
    (转)JAVA排序汇总
    (转)Java线程:大总结
    (转)Java线程:新特征-原子量,障碍器
    (转)Java线程:新特征-条件变量
    oracle中的not in和not exists注意事项
    oracle字符乱码的解决方法
    线刷和卡刷的区别
    nexus5刷机
    linux上复制行到另一个文件
  • 原文地址:https://www.cnblogs.com/lixiuming521125/p/6428501.html
Copyright © 2011-2022 走看看