网络基础知识:
两台主机进行通信三个最关键的要素:IP地址,协议,端口号(负责区分不同的应用程序)
TCP/IP协议是以TCP和IP为基础的不同层次上多个协议的集合。
TCP:传输控制协议
IP:互联网协议
物理层 – 数据链路层 – 网络层 – 传输层(TCP/IP)– 应用层(http、ftp、smtp、telnet)
端口号(0-65535),0-1023被系统所保留,使用时尽量挑选1023以后的使用
IP地址和端口号组成了Socket,Socket是网络上运行的程序之间双向通信链路的终结点,是TCP和UDP的基础。
常用端口号:http:80、ftp:21 、telnet:23
Java提供的网络功能四大类:
1、InetAddress:用于标识网络上的硬件资源,表示互联网IP地址
2、URL:统一资源定位符,通过URL可以直接读取或写入网络上的数据
3、Scokets:使用TCP协议实现网络通信的Socket相关的类
4、Datagram:使用UDP协议,将数据保存在数据报里,通过网络进行通信
1、InetAddress类
API文档:
可以看出InetAddress没有构造方法,因此不能通过new来构造对象,但是该类有很多静态方法,比如:static InetAddress getByName(String host),该方法可以通过指定的host返回一个InetAddress对象。
一个小例子示范如何使用InetAddress类:
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
public class test {
public static void main(String[] args) throws UnknownHostException {
/*
*获取本机的InetAddress实例
*/
System.out.println("获取本机的InetAddress实例");
InetAddress address = InetAddress.getLocalHost();
System.out.println("计算机名: " + address.getHostName());
System.out.println("IP地址: " + address.getHostAddress());
byte[] bytes = address.getAddress();
System.out.println("获取字节数组形式的IP地址: " + Arrays.toString(bytes));
System.out.println("InetAddress: " + address);
System.out.println();
/*
*根据主机名获取InetAddress实例
*/
System.out.println("根据主机名获取InetAddress实例");
InetAddress address2 = InetAddress.getByName("dorothy-PC");
System.out.println("IP地址: " + address2.getHostAddress());
System.out.println("计算机名: " + address2.getHostName());
System.out.println();
/*
*根据IP地址获取InetAddress实例
*/
System.out.println("根据IP地址获取InetAddress实例");
InetAddress address3 = InetAddress.getByName("192.168.2.11");
System.out.println("IP地址: " + address3.getHostAddress());
System.out.println("计算机名: " + address3.getHostName());
}
}
输出结果:
2、URL类
URL由两部分组成:协议名称和资源名称,用冒号隔开
如:http://www.baidu.com,http就是协议名称,www.baidu.com为资源名称
在java.net包中提供了URL类来表示URL
URL的部分API文档:
一个小例子示范如何使用URL类:
import java.net.URL;
public class test {
public static void main(String[] args) {
try {
URL url = new URL("http://www.baidu.com");
/*
* ?后面表示的是参数,#后面的是锚点
*/
URL newUrl = new URL(url, "/index.html?username=dorothy#yao");
System.out.println(newUrl);
System.out.println("协议: " + newUrl.getProtocol());
System.out.println("主机: " + newUrl.getHost());
/*
* 如果未指定端口号,则根据协议的不同,使用默认端口号(http端口号为80),getPort()返回值为-1
*/
System.out.println("端口: " + newUrl.getPort());
System.out.println("文件路径: " + newUrl.getPath());
System.out.println("文件名: " + newUrl.getFile());
System.out.println("相对路径: " + newUrl.getRef());
System.out.println("查询字符串: " + newUrl.getQuery());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出结果:
注解:
HTTP协议在当初制定时规定,其默认端口号为80,即未申明(省略)端口号的情况下,浏览器自动为其补充上URL中缺失的“:80”部分。关于HTTP协议的其它详情,可查阅RFC 2616。
java.net.URL.getPort()规定,若URL的实例未申明(省略)端口号,则返回值为-1。
通过URL读取网络内容:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class test {
public static void main(String[] args) {
URL url;
try {
url = new URL("http://www.baidu.com");
//通过URL的openStream方法获取URL对象所表示的资源的字节输入流
InputStream inputStream = url.openStream();
//将字节输入流转换为字符输入流
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
//为字符输入流添加缓冲
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String data = bufferedReader.readLine(); //一次读取一行
while (data != null) {
System.out.println(data);
data = bufferedReader.readLine(); //读取下一行
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行后,控制台输出的就是百度首页的html代码,将这些代码复制到一个扩展名为html的文件中,就可以在浏览器中打开,显示的就是百度的首页。