网络编程

概述

  • Java是 Internet 上的语言,它从语言级上提供了对网络应用程序的支持,程序员能够很容易开发常见的网络应用程序。
  • Java提供的网络类库,可以实现无痛的网络连接,联网的底层细节被隐藏在 Java 的本机安装系统里,由 JVM 进行控制。并且 Java 实现了一个跨平台的网络库,程序员面对的是一个统一的网络编程环境

网络基础

  • 计算机网络:把分布在不同地理区域的计算机与专门的外部设备用通信线路互连成一个规模大、功能强的网络系统,从而使众多的计算机可以方便地互相传递信息、共享硬件、软件、数据信息等资源。

  • 网络编程的目的:直接或间接地通过网络协议与其它计算机进行通讯。

  • 网络编程中有两个主要的问题:

    • 如何准确地定位网络上一台或多台主机
    • 找到主机后如何可靠高效地进行数据传输。
  • 如何实现网络中的主机互相通信:

    • 通信双方地址
    • 一定的规则(有两套参考模型)
      • OSI参考模型:模型过于理想化,未能在因特网上进行广泛推广
      • **TCP/IP参考模型(或TCP/IP协议)**:事实上的国际标准。

网络通信协议

通讯要素

一:IP和端口号

  1. IP 地址:InetAddress

    • 唯一的标识 Internet 上的计算机
    • 本地回环地址(hostAddress):127.0.0.1 主机名(hostName):localhost
    • 不易记忆
  2. 端口号标识正在计算机上运行的进程(程序)

    • 不同的进程有不同的端口号
    • 被规定为一个 16 位的整数 065535。其中,01023被预先定义的服务通信占用(如MySql占用端口3306,http占用端口80等)。除非我们需要访问这些特定服务,否则,就应该使用 1024~65535 这些端口中的某一个进行通信,以免发生端口冲突
  3. 端口号与IP地址的组合得出一个网络套接字。

InetAddress类

主要内容

  • Internet上的主机有两种方式表示地址:
    • 域名(hostName):www.baidu.com
    • IP 地址(hostAddress):110.242.68.4
  • InetAddress类主要表示IP地址,两个子类:Inet4Address、Inet6Address。
  • InetAddress 类对象含有一个 Internet 主机地址的域名和IP地址:www.baidu.com 和 110.242.68.4。
  • 域名容易记忆,当在连接网络时输入一个主机的域名后,域名服务器(DNS)负责将域名转化成IP地址,这样才能和主机建立连接。 ——-域名解析

常用方法

  • InetAddress类没有提供公共的构造器,而是提供了如下两个静态方法来获取InetAddress实例
  • InetAddress提供了如下几个常用的方法

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestInetAddress {
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress);
//获取InetAddress对象所含域名
System.out.println(inetAddress.getHostName());
//获取InetAddress对象所含IP地址
System.out.println(inetAddress.getHostAddress());

System.out.println();

//获取本机的域名和IP地址
InetAddress inetAddress1 = InetAddress.getLocalHost();
System.out.println(inetAddress1);
System.out.println(inetAddress1.getHostName());
System.out.println(inetAddress1.getHostAddress());
}
}

二:网络通信协议

概述

  • 网络通信协议:计算机网络中实现通信必须有一些约定,即通信协议,对速率、传输代码、代码结构、传输控制步骤、出错控制等制定标准。

  • 通信协议分层的思想

    ​ 由于结点之间联系很复杂,在制定协议时,把复杂成份分解成一些简单的成份,再将它们复合起来。最常用的复合方式是层次方式,即同层间可以通信、上一层可以调用下一层,而与再下一层不发生关系。各层互不影响,利于系统的开发和扩展。

TCP/IP协议簇

  • 传输层协议中有两个非常重要的协议

    • 传输控制协议TCP(Transmission Control Protocol)
    • 用户数据报协议UDP(User Datagram Protocol)
  • TCP/IP 以其两个主要协议:传输控制协议(TCP)和网络互联协议(IP)而得名,实际上是一组协议,包括多个具有不同功能且互为关联的协议。

  • IP(Internet Protocol)协议是网络层的主要协议,支持网间互连的数据通信。

  • TCP/IP协议模型从更实用的角度出发,形成了高效的四层体系结构,即物理链路层、IP层、传输层和应用层

TCP和UDP

  • TCP协议:

    • 使用TCP协议前,须先建立TCP连接,形成传输数据通道
    • 传输前,采用“三次握手”方式,是可靠的
    • TCP协议进行通信的两个应用进程:客户端、服务端
    • 在连接中可进行大数据量的传输
    • 传输完毕,需释放已建立的连接,效率低
  • UDP协议:

    • 将数据、源、目的封装成数据包,不需要建立连接
    • 每个数据报的大小限制在64K内
    • 因无需连接,故是不可靠的
    • 发送数据结束时无需释放资源,速度快

Socket

概述

  1. 利用套接字(Socket)开发网络应用程序早已被广泛的采用,以至于成为事实上的标准。
  2. 通信的两端都要有Socket,是两台机器间通信的端点。
  3. 网络通信其实就是Socket间的通信。
  4. Socket允许程序把网络连接当成一个流,数据在两个Socket间通过IO传输。
  5. 一般主动发起通信的应用程序属客户端,等待通信请求的为服务端

Socket类常用方法

ServerSocket类常用方法

基于Socket的TCP编程

通信模型

Java语言的基于套接字编程分为服务端编程和客户端编程,其通信模型如图所示:

客户端

  1. 客户端Socket的工作过程包含以下四个基本的步骤:
  • 创建 Socket:根据指定服务端的 IP 地址或端口号构造 Socket 类对象。若服务器端响应,则建立客户端到服务器的通信线路。若连接失败,会出现异常。
  • 打开连接到 Socket 的输入/出流: 使用 getInputStream()方法获得输入流,使用 getOutputStream()方法获得输出流,进行数据传输。
  • 按照一定的协议对 Socket 进行读/写操作:通过输入流读取服务器放入线路的信息(但不能读取自己放入线路的信息),通过输出流将信息写入线程。
  • 关闭 Socket:断开客户端到服务器的连接,释放线路。
  1. 客户端创建Socket对象
  • 客户端程序可以使用Socket类创建对象,创建的同时会自动向服务器方发起连接。Socket的构造方法是:

    • Socket(String host,int port)throws UnknownHostException,IOException:向服务器(域名是host,端口号为port)发起TCP连接,若成功,则创建Socket对象,否则抛出异常。
    • Socket(InetAddress address,int port)throws IOException:根据InetAddress对象所表示的IP地址以及端口号port发起连接。
  • 客户端建立socketAtClient对象的过程就是向服务器发出套接字连接请求

服务端

  1. 服务器程序的工作过程包含以下四个基本的步骤:
  • 调用 ServerSocket(int port):创建一个服务器端套接字,并绑定到指定端口上。用于监听客户端的请求。
  • 调用 accept()监听连接请求,如果客户端请求连接,则接受连接,返回通信套接字对象。
  • **调用该Socket类对象的 getOutputStream() 和 getInputStream ()**:获取输出流和输入流,开始网络数据的发送和接收。
  • 关闭ServerSocket和Socket对象:客户端访问结束,关闭通信套接字。
  1. 服务器建立 ServerSocket 对象
  • ServerSocket 对象负责等待客户端请求建立套接字连接,类似邮局某个窗口中的业务员。也就是说,服务器必须事先建立一个等待客户请求建立套接字连接的ServerSocket对象
  • 所谓“接收”客户的套接字请求,就是accept()方法会返回一个 Socket 对象。

代码示例1

客户端发送内容给服务端,服务端将内容打印到控制台上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import org.testng.annotations.Test;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
* 客户端发送内容给服务端,服务端将内容打印到控制台上。
*/
public class TestTCP {

// 客户端
@Test
public static void client(){
Socket socket = null;
OutputStream outputStream = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
outputStream = socket.getOutputStream();
outputStream.write("我是客户端,请多关照~".getBytes());
}catch (Exception e){
e.printStackTrace();
}finally {
if (outputStream != null){
try {
outputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}

// 服务端
@Test
public static void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
try {
serverSocket = new ServerSocket(9090);
socket = serverSocket.accept();
inputStream = socket.getInputStream();
byte[] b = new byte[20];
int len;
while ((len = inputStream.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.println(str);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (inputStream != null){
try {
inputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}

代码示例2

客户端发送内容给服务端,服务端给予反馈。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import org.testng.annotations.Test;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
* 客户端发送内容给服务端,服务端给予反馈。
*/
public class TestTCP2 {

// 客户端
@Test
public static void client(){
Socket socket = null;
OutputStream outputStream = null;
InputStream inputStream = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
outputStream = socket.getOutputStream();
outputStream.write("我是客户端喔".getBytes());
// 显式地告诉服务端我已发送完毕
socket.shutdownOutput();
inputStream = socket.getInputStream();
byte[] b = new byte[20];
int len;
while ((len = inputStream.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.println(str);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (inputStream != null){
try {
inputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (outputStream != null){
try {
outputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}

// 服务端
@Test
public static void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
serverSocket = new ServerSocket(9090);
socket = serverSocket.accept();
inputStream = socket.getInputStream();
byte[] b = new byte[20];
int len;
while ((len = inputStream.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.println(str);
}
outputStream = socket.getOutputStream();
outputStream.write("我已收到!".getBytes());
}catch (Exception e){
e.printStackTrace();
}finally {
if (inputStream != null){
try {
inputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (outputStream != null){
try {
outputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}

代码示例3

从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import org.testng.annotations.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
* 从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。
*/
public class TestTCP3 {

@Test
public void client(){
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
InputStream inputStream = null;
try {
// 1、创建Socket对象
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9898);
// 2、从本地获取一个文件发送给服务端
outputStream = socket.getOutputStream();
fileInputStream = new FileInputStream(new File("1.jpg"));
byte[] b = new byte[1024];
int len;
while ((len = fileInputStream.read(b)) != -1) {
outputStream.write(b, 0, len);
}
socket.shutdownOutput();
// 3、接收来自服务端的信息
inputStream = socket.getInputStream();
byte[] b1 = new byte[1024];
int len1;
while ((len1 = inputStream.read(b)) != -1) {
String str = new String(b1, 0, len1);
System.out.println(str);
}
}catch (Exception e){
e.printStackTrace();
}finally {
// 4、关闭相应的流和socket对象
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

@Test
public void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
OutputStream outputStream = null;
try {
// 1、创建一个ServerSocket的对象
serverSocket = new ServerSocket(9898);
// 2、调用其accept()方法,返回一个Socket的对象
socket = serverSocket.accept();
// 3、将客户端发来的图片保存到本地
inputStream = socket.getInputStream();
fileOutputStream = new FileOutputStream("2.jpg");
byte[] b = new byte[1024];
int len;
while ((len = inputStream.read(b)) != -1) {
fileOutputStream.write(b, 0, len);
}
// 4、发送"接收成功"的信息给客户端
outputStream = socket.getOutputStream();
outputStream.write("你发送的图片我已经收到了".getBytes());
}catch (Exception e){
e.printStackTrace();
}finally {
// 5、关闭相应的流和Socket以及ServerSocket的对象
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

UDP网络通信

概述

  • 类 DatagramSocket 和 DatagramPacket 实现了基于 UDP 协议网络程序。
  • UDP数据报通过数据报套接字 DatagramSocket 发送和接收,系统不保证UDP数据报一定能够安全送到目的地,也不能确定什么时候可以抵达。
  • DatagramPacket 对象封装了UDP数据报,在数据报中包含了发送端的IP地址和端口号以及接收端的IP地址和端口号。
  • UDP协议中每个数据报都给出了完整的地址信息,因此无须建立发送方和接收方的连接

流程

  1. DatagramSocket 与 DatagramPacket
  2. 建立发送端,接收端
  3. 建立数据包
  4. 调用Socket的发送、接收方法
  5. 关闭Socket
  • 发送端与接收端是两个独立的运行程序

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import org.testng.annotations.Test;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class TestUDP {

/**
* 发送端
*/
@Test
public void send(){
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket();
byte[] b = "你好,我是要发送的信息".getBytes();
// 创建一个数据报,每一个数据报都不能大于64k,
DatagramPacket datagramPacket = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"), 9090);
datagramSocket.send(datagramPacket);
}catch (Exception e){
e.printStackTrace();
}finally {
if (datagramSocket != null){
datagramSocket.close();
}
}
}

/**
* 接收端
*/
@Test
public void receive(){
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket(9090);
byte[] b = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(b, 0, b.length);
datagramSocket.receive(datagramPacket);
String str = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
System.out.println(str);
}catch (Exception e){
e.printStackTrace();
}finally {
if (datagramSocket != null){
datagramSocket.close();
}
}
}
}

URL编程

概述

  • URL(Uniform Resource Locator)统一资源定位符,它表示 Internet 上某一资源的地址。通过 URL 我们可以访问 Internet 上的各种网络资源,比如最常见的 www,ftp 站点。浏览器通过解析给定的 URL 可以在网络上查找相应的文件或其他资源。

  • URL的基本结构由5部分组成:

    • <传输协议>://<主机名>:<端口号>/<文件名>
    • 例如: http://192.168.1.100/:8080/helloworld/index.jsp

初始化URL对象

为了表示URL,java.net 中实现了类 URL。我们可以通过下面的构造器来初始化一个 URL 对象:

  • public URL (String spec):通过一个表示URL地址的字符串可以构造一个URL对象。例如:URL url = new URL (“http://www.baidu.com/");
  • public URL(URL context, String spec):通过基 URL 和相对 URL 构造一个 URL 对象。例如:URL downloadUrl= new URL(url, “download.html”)
  • public URL(String protocol, String host, String file):例如:new URL(“http”, “www.baidu.com", “download.html”);
  • public URL(String protocol, String host, int port, String file):例如:URL gamelan = new URL(“http”, “www.baidu.com", 80, “download.html”);

常用方法

类URL的构造方法都声明抛出非运行时异常,必须要对这一异常进行处理,通常是用 try-catch 语句进行捕获。

一个URL对象生成后,其属性是不能被改变的,但可以通过它给定的方法来获取这些属性

  • public String getProtocol( ) 获取该URL的协议名
  • public String getHost() 获取该URL的主机名
  • public String getPort() 获取该URL的端口号
  • public String getPath() 获取该URL的文件路径
  • public String getFile() 获取该URL的文件名
  • public String getRef() 获取该URL在文件中的相对位置
  • public String getQuery() 获取该URL的查询名

针对HTTP协议的URLConnection类

  • URL的方法 openStream()能从网络上读取数据

  • 若希望输出数据,例如,向服务器端的 CGI (公共网关接口-Common Gateway Interface-的简称,是用户浏览器和服务器端的应用程序进行连接的接口)程序发送一些数据,则必须先与URL建立连接,然后才能对其进行读写,此时需要使用 URLConnection

  • URLConnection:表示到URL所引用的远程对象的连接。当与一个URL建立连接时,首先要在一个 URL 对象上通过方法 openConnection() 生成对应的 URLConnection 对象。如果连接过程失败,将产生IOException。

  • 通过URLConnection对象获取的输入流和输出流,即可以与现有的CGI程序进行交互

    • public Object getContent() throws IOException
    • public int getContentLength()
    • public String getContentType()
    • public long getDate()
    • public long getLastModified()
    • public InputStream getInputStream()throws IOException
    • public OutputSteram getOutputStream()throws IOException

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class TestURL {
public static void main(String[] args) throws IOException {
URL url = new URL("http://liuwqtech.zhongwangtx.com/2022/09/18/Java/Java%E5%9F%BA%E7%A1%80%E8%AF%A6%E8%BF%B0/L10-IO/");

// 如何读取服务端资源读取出来:openStream()
InputStream inputStream = url.openStream();
byte[] b = new byte[20];
int len;
while ((len = inputStream.read(b)) != -1){
String str = new String(b,0,len);
System.out.println(str);
}
inputStream.close();

// 如果既有数据的输入,又有数据的输出,则考虑使用URLConnection
URLConnection urlConnection = url.openConnection();
InputStream inputStream1 = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(new File("xiaoxin.txt"));
byte[] b1 = new byte[20];
int len1;
while ((len1 = inputStream1.read(b1)) != -1){
fileOutputStream.write(b1,0,len1);
}
fileOutputStream.close();
inputStream1.close();
}
}

总结

  1. 位于网络中的计算机具有唯一的IP地址,这样不同的主机可以互相区分。
  2. 客户端-服务器是一种最常见的网络应用程序模型。服务器是一个为其客户端提供某种特定服务的硬件或软件。客户机是一个用户应用程序,用于访问某台服务器提供的服务。端口号是对一个服务的访问场所,它用于区分同一物理计算机上的多个服务。套接字用于连接客户端和服务器,客户端和服务器之间的每个通信会话使用一个不同的套接字。TCP协议用于实现面向连接的会话。
  3. Java 中有关网络方面的功能都定义在 java.net 程序包中。Java 用 InetAddress 对象表示 IP 地址,该对象里有两个字段:主机名(String) 和 IP 地址(int)。
  4. 类 Socket 和 ServerSocket 实现了基于TCP协议的客户端-服务器程序。Socket是客户端和服务器之间的一个连接,连接创建的细节被隐藏了。这个连接提供了一个安全的数据传输通道,这是因为 TCP 协议可以解决数据在传送过程中的丢失、损坏、重复、乱序以及网络拥挤等问题,它保证数据可靠的传送
  5. 类 URL 和 URLConnection 提供了最高级网络应用。URL 的网络资源的位置来同一表示 Internet 上各种网络资源。通过URL对象可以创建当前应用程序和 URL 表示的网络资源之间的连接,这样当前程序就可以读取网络资源数据,或者把自己的数据传送到网络上去。