InetAddress类和InetSocketAddress类
InetAddress 类
封装计算机的 IP 地址,不包含端口号
InetAddress 类常用的方法
1 String getHostAddress() 获得 IP 地址
2 String getHostName() 获得主机名
3 static InetAddress getByName(String host) 根据主机名获得 IP 地址
import java.net.InetAddress;
import java.net.UnknownHostException; public class TestInetAddress { public static void main(String[] args) throws UnknownHostException { InetAddress localHost = InetAddress.getLocalHost(); //本机
System.out.println("本机IP地址:" + localHost.getHostAddress());
System.out.println("本机名称:" + localHost.getHostName()); //根据域名得到InetAddress对象
InetAddress bd = InetAddress.getByName("www.baidu.com");
System.out.println("百度服务器地址:" + bd.getHostAddress());
System.out.println("百度服务器名称:" + bd.getHostName()); //根据IP地址得到InetAddress对象
InetAddress ia = InetAddress.getByName("39.130.131.42");
System.out.println("服务器主机IP:" + ia.getHostAddress());
//如果39.130.131.42IP地址不存在或者DNS(域名解析系统)不允许进行IP地址和域名的映射,就会直接返回域名地址
System.out.println("主机名称" + ia.getHostName());
} }
运行结果:

-------------------------------------------------------------------------
InetSocketAddress 类
此类用于实现 IP 套接字地址 (IP 地址+端口号),用于socket 通信
InetSocketAddress 类常用的方法
1 InetAddress getAddress() 获取 InetAddress 对象
2 int getPort() 获取端口号
3 String getHostName() 获取主机名
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException; public class TestInetSocketAddress { public static void main(String[] args) throws UnknownHostException { //创建对象
InetSocketAddress is1 = new InetSocketAddress("localhost", 9999);
InetSocketAddress is2 = new InetSocketAddress("127.0.0.1", 9999);
InetSocketAddress is3 = new InetSocketAddress("192.168.136.1", 9999); InetAddress ia = InetAddress.getByName("192.168.136.1");
InetSocketAddress is4 = new InetSocketAddress(ia, 9999);
System.out.println("主机名称:" + is4.getHostName());
System.out.println("主机IP地址:" + is4.getAddress());
} }

-----------------------------------------------------------------------------------------------------------------------------
URL类
URL(Uniform Resource Locator)统一资源定位符,由 4 部分组成:协议 、存放资源的主机域名、端口号和资源文件名。
URL 是指向互联网“资源”的指针资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询
URL 类常用方法
1 String getProtocal() 获取此 URL 的协议名称
2 String getHost() 获取此 URL 的主机名(如果适用)
3 int getPort() 获取 URL 的端口号
4 String getFile() 获取此 URL 的文件名
5 getDefaultPort() 获取与此 URL 关联协议的默认端口号
6 getPath() 获取此 URL 的路径部分
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL; public class TestUrl {
public static void main(String[] args) throws MalformedURLException { URL url = new URL("https://www.baidu.com:80/index.html");
System.out.println("协议名称:" + url.getProtocol());
System.out.println("主机名称:" + url.getHost());
System.out.println("端口号:" + url.getPort()); //URL不指明端口号则getPort()返回-1
System.out.println("获取资源路径:" + url.getFile());
System.out.println("获取资源路径:" + url.getPath());
System.out.println("获取默认端口:" + url.getDefaultPort());
} }

-------------------------------------------------------------------------------------------------------
openStream()方法 打开到此 URL 的连接并返回一个用于从该连接读入的InputStream
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL; public class TestUrl2 {
public static void main(String[] args) throws IOException {
/**网络爬虫
* (1)从网络上获取资源
* (2)存储到本机
*/
//(1)创建URL对象
URL url = new URL("https://www.baidu.com"); //获取主页资源
//(2)获取字节输入流
InputStream is = url.openStream();
//(3)缓冲流
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
//(4)存储到本地
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("F://index.html"), "utf-8"));
//(5)边读边写
String line = null;
while( (line = br.readLine()) != null) {
bw.write(line); //写入
bw.newLine(); //换行
bw.flush(); //清空缓冲区
}
//(6)关闭流
bw.close();
br.close();
} }
InetAddress类和InetSocketAddress类的更多相关文章
- [19/04/14-星期日] 网络编程_java.net包(InetAddress类、InetSocketAddress类、URL类)
一.概念 Java为了可移植性,不允许直接调用操作系统,而是由java.net包来提供网络功能.Java虚拟机负责提供与操作系统的实际连接. InetAddress 作用:封装计算机的IP地址和 ...
- Socket类 以及 ServerSocket类 讲解
Socket类 套接字是网络连接的端点,套接字使应用可以从网络中读取数据,可以向网络中写入数据.不同计算机上的两个应用程序可以通过连接发送或接收字节流,以此达到相互通信的目的. 为了从一个应用程序向另 ...
- ipv4与ipv6 Inet4Address类和Inet6Address类
在设置本地IP地址的时候,一些人会疑惑IPv4与IPv6的区别是什么?下面由学习啦小编为你分享ipv4与ipv6的区别的相关内容,希望对大家有所帮助. ipv4与ipv6的区别 在windows 7以 ...
- JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式
相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...
- 【转】UML类图与类的关系详解
UML类图与类的关系详解 2011-04-21 来源:网络 在画类图的时候,理清类和类之间的关系是重点.类的关系有泛化(Generalization).实现(Realization).依赖(D ...
- Java如何解决脆弱基类(基类被冻结)问题
概述 大多数好的设计者象躲避瘟疫一样来避免使用实现继承(extends 关系).实际上80%的代码应该完全用interfaces写,而不是通过extends.“JAVA设计模式”一书详细阐述了怎样用 ...
- 类A have-a 类B,类B访问类A public 成员
需求是类A中包含类B,而类B又需要访问类A的public属性的成员. 首先类B中要访问类A的属性,那么对于类B而言,我们必须要知道有类A这个类,所以在类B的具体实现之前我们需要前向声明类A. 对于类A ...
- UML(一) 类图及类间关系
原创文章,同步发自作者个人博客,http://www.jasongj.com/uml/class_diagram/ UML类图 UML类图介绍 在UML 2.*的13种图形中,类图是使用频率最高的UM ...
- JAVA正则表达式:Pattern类与Matcher类详解(转)
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表 ...
随机推荐
- VS 2015 GIT操作使用说明
相比VS2013,VS2015在对GIT的支持上有了更强大的支持.本篇仅作抛砖引玉,不做过多介绍: 1. 打开VS 2015起始页 2. 打开团队资源管理器 打开[本地GIT存储库]选项卡,然后点击[ ...
- C#使用Http的Post方式请求webservice
webservice是以前比较流行的跨系统.跨语言.跨平台的数据交互技术.最近工作中调用Java作为服务端开放的webser,我是通过VS205生成webservice工具类的方式进行接口调用的.用这 ...
- JS的forEach和map方法的区别
一.前言 forEach()和map()两个方法都是ECMA5中Array引进的新方法,主要作用是对数组的每个元素执行一次提供的函数,但是它们之间还是有区别的.jQuery也有一个方法$.each() ...
- Java——final关键字
前言 Java中的关键字final的含义通常为"这是无法改变的".下面将介绍final用于修饰数据.方法和类的这三种情况. final数据 许多编程语言都有某种方法,来向告诉编译器 ...
- Django 系列博客(十三)
Django 系列博客(十三) 前言 本篇博客介绍 Django 中的常用字段和参数. ORM 字段 AutoField int 自增列,必须填入参数 primary_key=True.当 model ...
- Creating a ROS msg and srv
msg: msg files are simple text files that describe the fields of a ROS message. They are used to gen ...
- Could not load file or assembly 'System.ValueTuple'
项目目标框架:.Net Framework 4.6.2 报错:Could not load file or assembly 'System.ValueTuple' 在4.6.2项目中,想要使用C#7 ...
- Spring学习心得--------bean-Factory
在学习Spring框架的过程中发现,Spring中的bean不仅是对javabean的一种封装,让你可以通过beanfactoryAPI读取自己配置的beans.xml文件来实现javabean的设置 ...
- java开发环境配置——IntelliJ IDEA
关于开发工具,之前是用eclipse,后来用了一段时间idea后,发现idea比eclipse好用太多了,所以推荐大家用idea 官网下载地址:https://www.jetbrains.com/id ...
- ionic3 导航的应用(页面跳转与参数传递)
about.html(跳转页面) <ion-content padding> <ion-list> <ion-item *ngFor="let he of co ...