java.net
- 提供实现网络应用程序的类
常见类
InetSocketAddress
: 实现IP套接字地址(IP地址+端口号)
URL
: 表示统一资源定位符,指向万维网上的“资源”的指针
HttpURLConnection
: 支持HTTP特定功能的URLConnection
DatagramSocket
: 用于发送和接收数据报数据包的套接字(通信端口)
DatagramPacket
: 数据报包 最大60k (包裹)
1.0. 网络编程 : 关注TCP协议开发的,面向socket编程
网页开发主要关注HTTP 应用层
-
应用层协议
http,ftp,smtp,ws,file -
传输层协议
- TCP( Transfer Control Protocol )
- 面向连接的
- 基于字节流的
- HTTP、FTP、SMTP 类似打电话
- UDP( User Datagram Protocol )
- 无连接
- DNS、SNMP、发短信
- TCP( Transfer Control Protocol )
-
IP
定位网络节点,比如电脑,路由器 -
Port
定位电脑软件 -
URL
定位资源路径
1.1. 网络分层
- OSI 七层参考模型
-
TCP/IP的协议栈 四层事实模型
网络分层.png
1.2. 数据传输
- 数据封装 数据封装.png
- 数据解封装 数据拆封.png
1.3. 网络爬虫 + 模拟浏览器
- 获取URL
URL url = new URL("https://www.dianping.com/");
- 下载资源
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
String msg = null;
while ((msg = bufferedReader.readLine()) != null) {
System.out.println(msg);
}
bufferedReader.close();
- 分析:正则
- 处理
1.4. UPD通信基本流程
UdpClient 客户端
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UdpClient {
public static void main(String[] args) throws Exception {
System.out.println("发送方启动中...");
//Address already in use (Bind failed) 同一协议下端口不允许冲突
//1. 使用DatagramSocket 指定端口 创建发送端
DatagramSocket clientSocket = new DatagramSocket(8888);
//2. 准备数据 一定转成字节数组
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true) {
// String data = "编程语言只是工具";
String data = reader.readLine();
byte[] datas = data.getBytes();
//3. 封装成DatagramPacket 包裹,需要指定目的地 (打包)
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",9999));
//4. 发送包裹send(DatagramPacket p)
clientSocket.send(packet);
System.out.println("发送包裹...");
if (data.equals("bye")){
break;
}
}
//5. 释放资源
clientSocket.close();
}
}
UdpServer 服务器端
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServer {
public static void main(String[] args) throws IOException {
System.out.println("接受方启动中...");
//1. 使用DatagramSocket 指定端口 创建接收端
DatagramSocket serverSocket = new DatagramSocket(9999);
//2. 准备容器 封装成DatagramPacket 包裹 最多60k
byte[] container = new byte[1024*60];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
while (true) {
//3. 阻塞式接受包裹receive(DatagramPacket p)
serverSocket.receive(packet);
System.out.println("收到包裹...");
//4. 分析数据
byte[] datas = packet.getData();
int len = packet.getLength();
String data = new String(datas,0,len);
System.out.println(data);
if (data.equals("bye")){
break;
}
}
//5. 释放资源
serverSocket.close();
}
}
加入多线程,实现双向交流
//TalkReceive 对服务器端实现 run
new Thread(new TalkReceive(9999,"学生")).start(); //接收
//TalkSend 对客户端实现 run
new Thread(new TalkSend(5555,"localhost",8888)).start(); //发送
TalkSend
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
//实现 Runnable 接口
public class TalkSend implements Runnable {
private DatagramSocket client ;
private BufferedReader reader;
private String toIP ;
private int toPort ;
public TalkSend(int port,String toIP,int toPort) {
this.toIP = toIP;
this.toPort=toPort;
try {
client=new DatagramSocket(port);
reader =new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true) {
String data;
try {
data = reader.readLine();
byte[] datas = data.getBytes();
//3、 封装成DatagramPacket 包裹,需要指定目的地
DatagramPacket packet =new DatagramPacket(datas,0,datas.length,
new InetSocketAddress(this.toIP,this.toPort));
//4、发送包裹send(DatagramPacket p) *
client.send(packet);
if(data.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 5、释放资源
client.close();
}
}
TalkReceive
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
//实现 Runnable 接口
public class TalkReceive implements Runnable {
private DatagramSocket server ;
private String from ;
public TalkReceive(int port,String from) {
this.from = from ;
try {
server=new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true) {
// 2、准备容器 封装成DatagramPacket 包裹
byte[] container =new byte[1024*60];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
// 3、阻塞式接收包裹receive(DatagramPacket p)
try {
server.receive(packet);//阻塞式
// 4、分析数据
byte[] datas =packet.getData();
int len = packet.getLength();
String data=new String(datas,0,len);
System.out.println(from+":"+data);
if(data.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 5、释放资源
server.close();
}
}
网友评论