Java学习笔记之网络编程
1 IP 地址
-
作用:唯一定位一台网络上的计算机
-
本机 ip 地址(localhost):127.0.0.1
-
ip 地址分类
- ipv4
- 由四个字节组成,例如本机 ip 地址 127.0.0.1
- 每个范围是 0 ~ 255
- 一共有 42 亿个,30 亿都在北美,亚洲只有 4 亿,2011年就已经用尽
- ipv6
- 128 位,由 8 个无符号整数构成
- 例如:
2022:08b3:abc4:0010:ade1:0000:1252:dd54
- 公网(互联网)、私网(局域网)
- ABCD 类地址:可用范围越来越短
- 192.168.xxx.xxx:专门给组织内部使用
- ipv4
-
域名:记忆 ip 问题
2 InetAddress
public class InetAddressInfo {
public static void main(String[] args) throws UnknownHostException {
//获取本机 ip
InetAddress localhost01 = InetAddress.getByName("localhost");
InetAddress localhost02 = InetAddress.getByName("127.0.0.1");
InetAddress localhost03 = InetAddress.getLocalHost();
//获取网络 ip
InetAddress baidu = InetAddress.getByName("www.baidu.com");
//常用方法
System.out.println(localhost01.getAddress()); //返回一个 ip 地址的字节数组
System.out.println(localhost01.getCanonicalHostName()); //返回规范化命名(ip地址)
System.out.println(localhost01.getHostAddress()); //返回域名
System.out.println(localhost01.getHostName()); //返回主机名称
}
}
3 端口
- 定义:表示计算机上的一个程序的进程
- 特点:不同进程有不同的端口号,用来区分软件
- 端口号范围: 0 ~ 65535
- 同一个协议下,端口号不能冲突;不同协议下,端口号可以相同
- 端口分类
- 公有端口:0 ~ 1023
- http:80
- https:443
- FTP:21
- Telent:23
- 程序注册端口:分配给用户或者程序,1024 ~ 49151
- Tomcat:8080
- MySQL:3306
- Oracle:1521
- 动态、私有端口:49153 ~ 65535
- 公有端口:0 ~ 1023
netstat -ano #查看所有端口
netstat -ano|findstr "8080" #查看固定端口
tasklist|findstr "8080" #查看指定端口的进程
public class InetSocketAddressInfo {
public static void main(String[] args) {
InetSocketAddress localhost = new InetSocketAddress("localhost", 8080);
System.out.println(localhost.getAddress());
System.out.println(localhost.getHostName());
System.out.println(localhost.getHostString());
System.out.println(localhost.getPort());
}
}
4 通信协议
4.1 TCP
- 类似打电话
- 连接、稳定
- 三次握手、四次挥手
最少三次,才可以建立连接
三次握手
A:你瞅啥?
B:瞅你咋地?
A:干一场!
四次挥手
A:我要走了!
B:你真的要走?
B:你确定要走吗?
A:我真的要走!
- 客户端、服务端
- 传输完成、释放连接、效率低
4.2 UDP
- 类似发短信
- 不连接、不稳定
- 客户端与服务端无明确界限
- 不管是否准备好,都可以发给你
5 TCP
5.1 消息发送
- 客户端
public class TcpClientTest {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
int port = 9999;
try {
//要知道服务器的地址、端口号
InetAddress serverIp = InetAddress.getByName("127.0.0.1");
//创建一个 socket 连接
socket = new Socket(serverIp, port);
//发送消息
os = socket.getOutputStream();
os.write("Hello Java!".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 服务端
public class TcpServerTest {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baoS = null;
try {
//得有一个地址
serverSocket = new ServerSocket(9999);
//等待客户端连接过来
socket = serverSocket.accept();
//读取客户端消息
is = socket.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 (Exception e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (baoS != null) {
try {
baoS.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
5.2 文件上传
- 客户端
public class FileClient {
public static void main(String[] args) throws Exception{
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
OutputStream os = socket.getOutputStream();
FileInputStream file = new FileInputStream(new File("src/tx.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = file.read()) != -1)
os.write(buffer, 0, len);
//通知服务器,我已经发送结束了
socket.shutdownOutput();
//确定服务器接收完毕,再断开
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baoS = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2 = inputStream.read(buffer)) != -1)
baoS.write(buffer2, 0, len2);
baoS.close();
inputStream.close();
file.close();
os.close();
socket.close();
}
}
- 客户端
public class FileServer {
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(9000);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fOs = new FileOutputStream(new File("receive.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();
is.close();
socket.close();
serverSocket.close();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!