网络编程梳理001

网络编程001

先学习三个基本对象,然后学习udp和tcp两种网络编程内容【详情在入门java—>网络编程梳理002、003】
UDP网络协议
TCP网络协议

IP对象

InetAddress : 表示IP地址
static InetAddress getLocalHost() 返回本地主机的地址。
String getHostAddress() 返回文本表示中的IP地址字符串。
String getHostName() 获取此IP地址的主机名。
static InetAddress getByName(String host) 根据主机名【/域名】称确定主机的IP地址。

 public static void main(String[] args) throws UnknownHostException {
        //static InetAddress getLocalHost() 返回本地主机的地址。
        InetAddress ip1 = InetAddress.getLocalHost();
        System.out.println(ip1); //主机名/IP地址
        //String getHostAddress() 返回文本表示中的IP地址字符串。
        //String getHostName() 获取此IP地址的主机名。
        System.out.println(ip1.getHostAddress());
        System.out.println(ip1.getHostName());

        //static InetAddress getByName(String host) 根据主机名称确定主机的IP地址。
        InetAddress ip2 = InetAddress.getByName("www.baidu.com");
        System.out.println(ip2);  //www.baidu.com/180.101.49.11
        System.out.println(ip2.getHostAddress());
        System.out.println(ip2.getHostName());
    }

套接字InetSocketAddress

此类实现IP套接字地址(IP地址+端口号)它也可以是一对(主机名+端口号),在这种情况下,将尝试解析主机名
InetSocketAddress(String hostname, int port) —根据主机名和端口号创建套接字地址
InetSocketAddress(InetAddress addr, int port) --根据IP地址和端口号创建套接字地址
InetAddress getAddress() ----------------------------获取 InetAddress 。
String getHostName()--------------------------------- 获取 hostname 。
int getPort() -----------------------------------------------获取端口号。

public static void main(String[] args) {
        //InetSocketAddress(String hostname,int port) 根据主机名和端口号创建套接字地址
        //InetSocketAddress(InetAddress addr,int port) 根据IP地址和端口号创建套接字
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 9999);
        System.out.println(inetSocketAddress);
        //InenntAddress getAddress() 获取InetAddress
        //String getHostName() 获取hostName
        //int getPort() 获取端口
        System.out.println(inetSocketAddress.getAddress().getHostName());
        System.out.println(inetSocketAddress.getPort());
    }

URL对象

类URL表示统一资源定位符,指向万维网上的“资源”的指针。

    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("http://www.baidu.com:80/index.html?username=zhangsan&password=123#a");
        System.out.println(url);                     //  http://www.baidu.com:80/index.html?username=zhangsan&password=123#a
        System.out.println(url.getProtocol());       //  http
        System.out.println(url.getHost());           //  www.baidu.com
        System.out.println(url.getPort());           //  80
        System.out.println(url.getPath());           //  /index.html
        System.out.println(url.getFile());           //  /index.html?username=zhangsan&password=123
        System.out.println(url.getQuery());          //  username=zhangsan&password=123
        System.out.println(url.getRef());            //  a
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值