网络编程
1.1网路通信的要素
如何实现网络通信?
通信双方地址:
- IP地址
- 端口号
规则:网络通信的协议
TCP、IP参考模型
OSI七层参考模型
网络编程主要地方
小结:
网络编程中主要存在的问题:
- 如何准确定位到网络中的一台或者多台主机
- 找到主机之后如何进行通信
网络编程中的要素:
- IP和端口号
- 网络通信协议(UDP、TCP)
1.2 IP
Java类(Class InetAddress)主要表示Internet协议(IP)地址。不存在构造方法
IP:InetAddress
- 唯一定位一台计算机
- 127.0.0.1 :本机localhost
- IP地址的分类
- IPv4/IPv6
- IPv4 :32位,4个字节
- IPv5:128位,8个无符号整数
- 公网(互联网)/私网(局域网)
- 192.168.xx.xx;局域网,专门为组织内部使用
- ABCD类地址,取半
- 域名:记忆IP地址
- IPv4/IPv6
InetAddress没有构造器,直接调用静态方法。
package Lesson;
import java.net.InetAddress;
import java.net.UnknownHostException;
//测试IP
public class Lesson_ip {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress byName = InetAddress.getByName("127.0.0.1");
InetAddress byName2 = InetAddress.getByName("localhost");
InetAddress byName3 = InetAddress.getLocalHost();
//查询网站IP地址
InetAddress byName1 = InetAddress.getByName("www.baidu.com");
System.out.println(byName);
System.out.println(byName2);
System.out.println(byName3);
System.out.println(byName1);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
1.3端口
端口表示计算机上一个程序的进程;
-
不同的进程有不同的端口号!用来区分软件!
-
被规定0~65535
-
TCP、UDP:65536*2 ;单个协议下端口号不能冲突
-
端口分类
-
公有端口 0~1023
- HTTP:80端口;
- HTTPS:443端口
- FTP:21
- Telent:23
-
程序注册端口:1024~49151,分配给用户或者程序
- Tomcat :8080
- MySQL:3306
- Oracle:1512
-
动态端口、私有端口:49152~65535
netstat -ano 查看所有的端口 netstat -ano|findstr "端口号" 查看指定的端口
-
1.4通信协议
协议:约定,
网络通信协议:速率,传输码率,代码结构,传输控制。。。。
TCP/IP协议簇:实际上是一组协议
重要:
- TCP:用户传输协议
- UDP:用户数据报协议
TCP、UDP对比
TCP:打电话
-
会建立连接,稳定
-
三次握手、四次挥手
最少需要三次,保证连接稳定 A:你愁啥? B:瞅你咋地? A:干一场 四次挥手 A;我要断开了! B:我知道你要断开了 B:你真的要断开吗 A:是的
-
客户端、服务端
-
传输完成,会释放连接,释放连接
UDP:发短信
- 不连接,不稳定
- 客户端、服务端:没有明确的界限
- 不管有没有准备,都可以发送
- DDOS:洪水攻击!饱和攻击
1.5 TCP
TCP字节传输
TCP服务器端:
- 建立服务器端口SeverSocket
- 等待用户的连接accept
- 接收用户的消息
package Lesson.Tcp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务器端
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1.我得有一个地址
serverSocket = new ServerSocket(9999);
while (true){
//2.等待客户端连接
socket = serverSocket.accept();
//3.读取客户端消息
is = socket.getInputStream();
/*
会出乱码
byte[] buffer = new byte[1024];
int len;
while ((len=is.read())!=-1){
String msg = new String(buffer,0,len);
System.out.println(msg);
}
*/
//管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer)) != -1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流,先开后关
if (baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客户端:
- 连接服务器Socket
- 发送消息
package Lesson.Tcp;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
//客户端
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream outputStream = null;
try {
//1.要知道服务器端的地址、端口号
InetAddress severIP = InetAddress.getByName("127.0.0.1");
int port=9999;
//2.创建一个Socket连接
socket = new Socket(severIP,port);
//3.发送消息 IO流
outputStream = socket.getOutputStream();
outputStream.write("你好啊!!!".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
TCP文件传输
服务器端:
package Lesson.Tcp;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
//服务器端
public class TcpServerDemo02 {
public static void main(String[] args) throws IOException {
//1.创建一个服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.服务器等待
Socket socket = serverSocket.accept();//阻塞式等待
//3.获取输入流
InputStream is = socket.getInputStream();
//4.写出文件
FileOutputStream fos = new FileOutputStream("1241.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = is. read(buffer))!= -1){
fos.write(buffer,0,len);
}
//通知客户端接收完毕
OutputStream os = socket.getOutputStream();
os.write("我已经输入完了".getBytes());
os.close();
fos.close();
serverSocket.close();
serverSocket.close();
}
}
客户端:
package Lesson.Tcp;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
//客户端
public class TcpClientDemo02 {
public static void main(String[] args) throws Exception {
//1.创建一个套接字
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
//2.创建输出流
OutputStream os = socket.getOutputStream();
//3.写入一个文件
FileInputStream fis = new FileInputStream("src/1141.jpg");
//4.发出文件
byte[] buffer = new byte[2048];
int len;
while ((len=fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//通知服务器我已经发送完了
socket.shutdownOutput();
//确定客户端接收完毕
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len2;
while ((len2=inputStream.read(bytes))!=-1){
baos.write(bytes,0,len2);
}
System.out.println(baos.toString());
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
}
}
1.6UDP
发短信:不需要连接,需要知道对方地址
Java类(Class DatagramPacket),该类表示数据报包。存在构造方法。
- 数据报包用于实现无连接分组传送服务。仅基于该数据包中包含的信息,每个消息从一台机器路由到另一台机器。从一台机器发送到另一台机器的多个分组可能会有不同的路由,并且可能以任何顺序到达。包传送不能保证。
Java类(Class DatagramSocket),该类表示用于发送和接收数据报数据包的套接字。
- 数据报套接字是分组传送服务的发送或接收点。在数据报套接字上发送或接收的每个数据包都被单独寻址和路由。从一个机器发送到另一个机器的多个分组可以不同地路由,并且可以以任何顺序到达。
1.6.1UDP字节传输
接收端
package Lesson.UDP;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServerDemo01 {
public static void main(String[] args) throws Exception {
//1.开放接 口
DatagramSocket socket = new DatagramSocket(9090);
//2.接收包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
//关闭链接
socket.close();
}
}
客户端
package Lesson.UDP;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//不需要连接服务器
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
//1.建立一个socket
DatagramSocket socket = new DatagramSocket();
//2.建一个包
String msg = "你好啊!!!";
//发给谁
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
//数据 、起始的长度起始位置 、数据长度 、目标地址 、 接口
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
//3.发送包
socket.send(packet);
//4.关闭流
socket.close();
}
}
1.6.2循环多次发送
发送方
package Lesson.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UdpSenderDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
String data = reader.readLine();
DatagramPacket packet = new DatagramPacket(data.getBytes(),0,data.getBytes().length,new InetSocketAddress("localhost",6666));
socket.send(packet);
if (data.equals("bye"))
break;
}
socket.close();
}
}
接收方
package Lesson.chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true){
//准备接收数据包
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);//阻塞式接收
//断开链接 bye
byte[] data = packet.getData();
String datas = new String(data, 0, packet.getLength());
System.out.println(datas);
if (datas.equals("bye")){
break;
}
}
socket.close();
}
}
1.6.3 UDP实现在线咨询
发送类
package Lesson.chat;
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;
import java.nio.charset.StandardCharsets;
public class TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
String data = reader.readLine();
byte[] datas = data.getBytes(StandardCharsets.UTF_8);
DatagramPacket packet = new DatagramPacket(datas,0,data.length(),new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if (data.equals("bye"))
break;
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
接受类
package Lesson.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String name;
public TalkReceive(int port,String name) {
this.port = port;
this.name = name;
try {
socket = new DatagramSocket(this.port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
try {
socket.receive(packet);
//输出
byte[] data = packet.getData();
String datas = new String(data, 0, packet.getLength());
System.out.println(name+":"+datas);
if (datas.equals("bye"))
break;
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
教师类继承
package Lesson.chat;
public class Teacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
学生类继承
package Lesson.chat;
public class Student {
public static void main(String[] args) {
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
总结:
发送方与接收方没有具体的界限,控制台接收键盘数据需要转换成byte字节流后发出,接收方需要将接收到的byte转换成String输出控制台。注意双方接收端口和发送端口。
控制台输出时,需要推出到项目界面,使用 java 命令执行.class文件即可。
1.7 URL
https://www.baidu.com
统一资源定位符:定位资源的,定位互联网上的某一项资源
DNS域名解析:将www.baidu.com转换为IP地址
协议://IP地址:端口/项目名/资源
URL简单使用
package Lesson.URL;
import java.net.MalformedURLException;
import java.net.URL;
public class URLDemo01 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://loaclhost:8080/helloword/index.jsp?username=xxx&password=111");
System.out.println(url.getProtocol());//获得协议名
System.out.println(url.getHost());//获得主机IP
System.out.println(url.getPort());//获得端口号
System.out.println(url.getPath());//获得文件的地址
System.out.println(url.getFile());//获得文件全路径
System.out.println(url.getQuery());//获得URL查询的能力
}
}
URL下载文件
注意:.txt乱码由于文件编码格式不匹配,另存为时,更改为ANSI编码格式。
package Lesson.URL;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLDown {
public static void main(String[] args) throws Exception {
//1.下载地址
URL url = new URL("http://localhost:8080/xxx/Security.txt");
//2.连接到这个资源 HTTP连接
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//3.开始下载
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("Security.txt");
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);//从缓冲区读取数据
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}
网易云音乐下载
注意:需要寻找文件地址,文件地址可以从F12网络中进行寻找。只需要将文件名称进行更改即可
package Lesson.URL;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLDown {
public static void main(String[] args) throws Exception {
//1.下载地址
URL url = new URL("https://m701.music.126.net/20220316155139/0105edb020ef5fdb78cf0cf3cdbd55af/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/13386255970/a8cb/e698/5086/a12e78677754518a1082ee7bdcb7e328.m4a");
//2.连接到这个资源 HTTP连接
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//3.开始下载
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("网易云.m4a");
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);//从缓冲区读取数据
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}
1.8、中文字符编码与解码
网络字符编码解码
主要解决中文乱码问题
URLEncoder.encode("贤","utf-8")//中文字符编码
URLDecoder.decode(cookie.getValue(),"utf-8")//解码