网络编程
网络编程
1.1、概述
打电话------>TCP
发短信------>UDP
计算机网络:
- 地理位置不同
- 网络通信协议
- 实现资源共享
网络编程的目的:
- 传播交流信息
- 数据交换,通信
到达的效果
- 如何定位网络上的一台主机
- 找到主机之后,如何传输数据呢?
1.2、网络通信的要素
如何实现网络的通信?
通信双方的地址:
- ip
- 端口号
规则:网络通信的协议
http, ftp,
小结
- 网络编程中的两个主要问题
- 如何准确的定位一台或者多台主机
- 找到主机之后如何通信呢?
- 网络编程中的要素
- IP和端口号
- 网络通信协议 TCP UDP
- 万物皆对象
1.3、IP
ip地址:InetAddress
- 唯一定位一台网络上计算机
- 127.0.0.1;本机localhost
- ip地址的分类
- IPV4 /IPV6
- 127.0.0.1 , 四个字节组成 0~255
- 公网(互联网)-私网(局域网)
- IPV4 /IPV6
- 域名:记忆ip问题
package com.kuang.lesson01;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author yt
* @create 2021-07-29 21:58
*/
//测试IP
public class TestInetAddress {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress3 = InetAddress.getByName("localhost");
System.out.println(inetAddress3);
InetAddress inetAddress4 = InetAddress.getLocalHost();
System.out.println(inetAddress4);
//查询网站ip地址
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
1.4、端口
端口表示计算机上的一个程序进程
-
不同的进程有不同的端口号!用来区分软件!
-
被规定0~65535
-
TCP,UDP,单个协议下,端口号不能冲突
-
端口号分类
-
公有端口0~1023
- HTTP:80
- HTTPS:443
- FTP:21
- Telent:23
-
程序注册端口:1024~49151 分配用户或者程序
- Tomcat: 8080
- MySQL: 3306
- Oracle: 1521
-
动态、私有端口:49152~65535
netstat-ano
-
1.5、通信协议
1.6、TCP
客户端
- 连接服务器 ServerSocket
- 发送消息
package com.kuang.lesson02;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author yt
* @create 2021-07-30 10:57
*/
//客户端
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1.要知道服务器的地址
final InetAddress serverIP = InetAddress.getByName("127.0.0.1");
//2.端口号
int port = 9999;
//3.创建一个socket连接
socket = new Socket(serverIP, port);
//4.发送消息 IO流
os = socket.getOutputStream();
os.write("你好啊!hao".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务器
- 建立服务的端口
- 等待用户的连接 accept
- 接收用户的消息
package com.kuang.lesson02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author yt
* @create 2021-07-30 10:57
*/
//服务端
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream is = null;
ByteArrayOutputStream baos =null;
try {
//1.我得有个地址
serverSocket = new ServerSocket(9999);
while (true){
//2.等待客户端的连接
accept = serverSocket.accept();
//3.读取客户端的消息
is = accept.getInputStream();
//管道流
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 (accept != null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件上传
服务器端
package com.kuang.lesson02;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author yt
* @create 2021-07-30 15:29
*/
public class TcpServerDemo02 {
public static void main(String[] args) throws Exception {
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9009);
//2.监听客户端的连接
Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
//3.获取输入流
InputStream is = socket.getInputStream();
//4.文件输出
FileOutputStream fos = new FileOutputStream(new File("receive"));
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());
//关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
客户端
package com.kuang.lesson02;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author yt
* @create 2021-07-30 15:15
*/
public class TcpClientDemo02 {
public static void main(String[] args) throws Exception {
//1.创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9009);
//2.创建一个输出流
OutputStream os = socket.getOutputStream();
//3.读取文件
FileInputStream fis = new FileInputStream(new File("idea.png"));
//4.写出文件
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer))!= -1){
os.write(buffer,0,len);
}
//通知服务器,我已经结束了
socket.shutdownInput();
//确定服务器接收完毕,才能够断开连接
InputStream inputStream = socket.getInputStream();
//String byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2=inputStream.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
//5.关闭资源
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
}
}
Tomcat
服务端
- 自定义
- Tomcat服务器
客户端
- 自定义
- 浏览器
1.7、UDP
发送端
//不需要连接服务器
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();
}
}
## 接收端
~~~ java
public class UdpServerDemo01 {
public static void main(String[] args) throws Exception {
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
socket.receive(packet);//阻塞接收
System.out.println(packet.getAddress().getHostAddress());
socket.close();
}
}
1.8、URL
统一资源定位符
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】