惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Security @ Cisco Blogs
小众软件
小众软件
D
Docker
博客园_首页
A
About on SuperTechFans
P
Privacy International News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
Arctic Wolf
Spread Privacy
Spread Privacy
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Latest news
Latest news
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
大猫的无限游戏
大猫的无限游戏
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
K
Kaspersky official blog
V2EX - 技术
V2EX - 技术
SecWiki News
SecWiki News
U
Unit 42
GbyAI
GbyAI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LINUX DO - 热门话题
S
Security Affairs
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
T
Tenable Blog
W
WeLiveSecurity
Cloudbric
Cloudbric
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
Google Developers Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
N
News and Events Feed by Topic
D
DataBreaches.Net
P
Proofpoint News Feed
B
Blog RSS Feed
B
Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园_首页

Linux实操--组管理、权限管理和定时任务 Java + EasyExcel 实现单个接口导出多个Excel Mem0 源码解析系列(二):提示词工程的深度剖析 Openclaw TaskFlow究竟是什么?和普通Skill技能有什么区别 博文阅读密码验证 - 博客园 嘉立创开源:应该是全网MicroPython教程最多的开发板 Hermes Agent 集成实践:从协议到生产 2026年AI编程工具横评:Cursor、Codex、Claude Code、Zed、Windsurf Java程序员必看的RAG入门教程 2026 AI效率神器:Superpowers + Claude Code 保姆级教程 本地大模型部署全攻略:从 0 到 1 玩转 Ollama 【从0到1构建一个ClaudeAgent】内存管理-上下文压缩 .NET 高级开发 | 设计、实现一个事件总线框架 电子小白入门之NE555 3. WorkBuddy:隐藏玩法,一键召唤专家,让 AI 以"专家身份"给你干活 和AI一起搞事情#3:Claude Teammate 游戏开发翻车实录 【OpenClaw】通过 Nanobot 源码学习架构---(7)Memory C# .NET 周刊|2026年3月3期 我在 Debian 11 上把 K8s 单机搭起来了,过程没你想的那么顺(/opt 目录版) 深度学习进阶(七)Data-efficient Image Transformer CLI+Skill搭建浏览器AI自动化框架,告别一切重复枯燥任务 告别Token账单无底洞:OpenClaw本地部署,重塑企业数据主权的唯一解 FastAPI+Vue:文件分片上传+秒传+断点续传,这坑我帮你踩平了! SBTI 爆火后,我做了个程序员版的 CBTI。。已开源 + 附开发过程 多模态检索开始进入工程期:用 Sentence Transformers 搭建可落地的 Multimodal RAG 100多行代码实现一个最简单的Agent(用ReAct) Claude Code 通关手册(八):推荐 5 个 Hooks,代码质量提升 3 倍 老板:“有人截图了!”。安全部门:“收到,马上查暗水印!” - why技术 技术之外,皆是人间 C#/.NET/.NET Core技术前沿周刊 | 第 69 期(2026年4.01-4.12) Snack JSONPath 项目架构分析 Claude Code Buddy 小析:一个非核心功能,如何体现产品的细节完成度 AI新时代下的图床管理方案-Cloudflare图床+MCP+Skills方案指南 化繁为简:顺丰速运App如何通过 HarmonyOS SDK实现专业级空间测量 从零实现富文本编辑器#13-React非编辑节点的内容渲染 AI开发-python-langchain框架(3-23-OpenAI Functions风格Tool Calling智能助手) .NET + AI 进阶实战:基于类的技能开发 - 打造可治理的 Agent 能力模块 【从0到1构建一个ClaudeAgent】规划与协调-技能 上周热点回顾(4.6-4.12) 电子小白的工具三件套:面包板、杜邦线、万能板 单表五亿数据的查询优化 | Mysql、StarRocks 2. WorkBuddy:从“我是谁”到“帮我干活” C# 如何减少代码运行时间:7 个实战技巧 基于HelixToolkit.SharpDX 渲染3D模型 - 笺上知微 从零开始的双臂具身VLA起源及现阶段发展综述 - SkyXZ 记对 xonsh shell 的使用, 脚本编写, 迁移及调优 - pluvium27 受够了Vibe Coding的失控?换个起点,让AI事半功倍 从开始配置漏洞环境到漏洞复现流程 - 難しい 关于10年工作经验的程序员对OpenClaw的实战经验分享以及看法 - 虚无境 Any metadata 的内存布局 C# .NET 周刊|2026年3月2期 - InCerry 我帮你测过了,测试圈排名第二的 Skill 依然很牛逼 Skill Discovery | 无监督技能发现的经典工作总结 - MoonOut PbootCMS 网站内容数量多导致访问慢?这些实用优化方案帮你提速! - 家兴网络技术工作室 上下文工程是什么?过时了么?一文讲明白! - 一枫说码 网站漏洞怎么发现并修复?一篇实用指南(附完整流程) - 家兴网络技术工作室 开了 TUN 模式还是直连?90% 的人都踩过这个坑 Github日报|2026年04月12日 - AI一族 AScript扩展多种脚本语言 - rockey627 AI 学习笔记:Agent 的记忆机制 你能被装进一个文件里吗?——7 万人把同事"蒸馏"成了 AI - 我没有三颗心脏 Claude Code 通关手册(七):给 AI 装上技能包——Skills 完全指南 - 暮色之狐 在浏览器中快速编辑代码:VSCode Web 集成实践 - Newbe36524 蒸馏自己 skill?基于 Deepseek 的蒸馏器,丐版蒸馏方式,简单便捷 - To_Carpe_Diem Spring AI Aliababa和AgentScope,哪个更好? - 苏三说技术 Etsy 把 1000 个 MySQL 分片迁进 Vitess:425TB 数据背后的真正问题不是性能,而是运维规模 MicroPython LVGL基础知识和概念:底层渲染与性能优化 - FreakStudio 数据库草图算法 Python 潮流周刊#146:CPython 引入 Rust 的进展 - 豌豆花下猫 最小生成树 - mofei1116 红日靶场七:从外网入口、容器逃逸到 AD 接管的完整利用链复盘 - YouDiscovered1t 分享四款开源且实用的 Kafka 管理工具 - 追逐时光者 vLLM 权重加载机制全解析:从挑战到理想架构 LCT 学习笔记 - ACehomoxue Avalonia UI 12.0.0 正式发布:架构演进和性能飞跃 - 张善友 当 AI Agent 把调用链拉长,延迟开始成为一门生意 conhost.exe 无法显示 U+2717 - 145a 太秀了,我把自己蒸馏成了 Skill!已开源 - 程序员鱼皮 ASP.NET Core 内存缓存实战:一篇搞懂该怎么配、怎么避坑 基于 Ghostty 带有分割标签页和为 Claude 编程设计的通知终端 - BugShare AI 焊死入口:教育的“操作系统级”重塑 - 郝hai 初级Java开发工程师使用sql脚本编写代码的过程是简单而且不糊涂 - CoderOilStation Claude Code通关手册(六):MCP协议完全指南 - 暮色之狐 边框灯光环绕动画特效实现指南 - Newbe36524 开源:子木蒸馏版的 SEO 审计工具 seo-audit-skill v1.0 我所理解的Python元模型 【从0到1构建一个ClaudeAgent】规划与协调-TodoWrite - 程序员Seven Claude 和 Codex 在审计 Skill 上性能差异探究 - ACai_sec AScript如何实现中文脚本引擎 - rockey627 【渗透测试】HTB Season10 Garfield 全过程wp - dynasty_chenzi Android 开发者为什么必须掌握 AI 能力?端侧视角下的技术变革 树状数组正确性证明 - AC-wyr 你的 AI 焦虑,可能比 AI 本身更危险——ATM 机没有消灭银行柜员,但恐慌消灭了你的判断力 - 我没有三颗心脏 一个拉胯的分库分表方案有多绝望?整个部门都在救火! - 冰河团队 动态规划入门必学之走方格问题 - Ofnoname PostgREST 与 PostgreSQL 角色权限配置全解析(生产级实践) - SheepDog1998 使用 UEFI 图形输出协议 GOP 在屏幕上显示图像的方法 - 阿源- Claude Code通关手册(五):组建你的AI专家团队,子代理系统 - 暮色之狐 一个程序员到架构师的催婚路之感悟(整整10年后的催婚相亲感悟) - MisterLip 用 Agent Skill 自动生成工作周报 - 赵康
05.JAVA的网络编程
莫霏 · 2026-06-27 · via 博客园_首页

网络编程

1. TCP/IP的参考模型:

Java的网络编程方面比其他的WINDOWS桌面程序要强大太多了,很多复杂的协议接口,都已经封装得很完美,功能代码只需要很少就完美实现。

OSI七层网络模型 TCP/IP四层概念模型 对应网络协议

应用层 Application 应用层 HTTP,TFTP, FTP,NFS,WAIS,SMTP
表示层 Presentation Telnet, Rlogin, SNMP, Gopher
会话层 Session SMTP, DNS

传输层 Transport 传输层 TCP, UDP
网络层 Network 网络层 IP,ICMP,ARP, RARP, AKP, UUCP

数据链路层:DataLink 数据链路层 FDDI, Ethernet, Arpanet, PDN,SLIP,PPP
物理层 Physical IEEE 802.1A, IEEE802.2到IEEE.802.11

小结:

  1. 如何准确的定位到网络上的一台或多台主机。
  2. 找到主机后如何通信。
  3. 确定IP和端口号。 IP 类
  4. 网络通信协议。 TCP和UDP类
  5. 万物皆对象。 InetAddress 类

IP: 用来唯一定位一台网络上的计算机。
127.0.0.1 : 本机地址localhost,相当于this.
IP地址分类: ipv4/ ipv6. 4字节组成IP和6字节组成IP。 42亿4类地址,30亿在北美,4亿在亚洲。
6类地址,是未来的地址。 ifconfig可以用来查看地址。128位,无符号整数。

  • A类地址:00000001, 00000000, 00000000, 00000001
    01111110, 11111111,11111111,11111110
    1.0.0.1~ 126.255.255.254
  • B类地址: 10000000, 00000001, 00000000, 00000001
    10111111, 11111110, 11111111,11111110
    128.1.0.1~ 191.254.255.254
  • C类地址:11000000, 00000000, 00000001, 00000001
    ~11011111, - 11111111, 11111110, 11111110
    192.0.1.1~223.255.254.254
  • D类地址:224.0.0.1~239.255.255.254
  • E类地址: 240.0.0.0~255.255.255.254
    //参数也可以用localhost或者127.0.0.1
    InetAddress inetAddress = InetAddress.getByName("www.Apple.com");
    使用方法:inetAddress.getHostAddress(); inetAddress.getHostName(); 得到对应IP和域名

2. TCP的java实现:

  • 客户端:
    1. 连接服务器Socket(serverIP, port); //port可以是约定,
      serverIP 用InetAddress.getByName("127.0.0.1"); 获得。
    2. 发送消息. 用os=socket.getOutputStream();得到一个发送流对象。
      然后再调用对象os的write("这是一个消息哦",getbytes());方法。
  • 服务器端:
    1. 建立服务器端口 ServerSocket ss= new ServerSock(port);
      再建立一个通信端口变量socket,这里不用实例化。
    2. 用通信端口指向服务器端口的accept: socket=ss.accept();
      等待用户的连接 accept
    3. 接收用户的消息 定义一个输入流对象 InputStream is=socket.getInputStream();
      用socket的对应方法得到输入流。
    4. 繁琐的流处理
      用ByteArrayOutputStream()获得流信息并输出。

3. UDP的java实现:

  1. 发送端:

用DatagramSocket建立套接字; 用DatagramPacket 建立UDP数据包。

DatagramPacket pkt = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length, 
inetAddress, port);
sck.send(pkt);  //send方法发送出去就可以了,不用管连接没连接。
sck.close();
  1. 接收端:

同样用DatagramSocket建立套接字; 用DatagramPacket 建立UDP数据包。

sct = new DatagramSocket(port);
//繁琐的缓冲数据处理
byte[] buff=new byte[1024];
DatagramPacket pckt = new DatagramPacket(buff, 0, buff.length);
sct.receive(pckt); //对应send 方法,用receive 形成一个阻塞接收。
//以下是需要的信息: pckt.getAddress().getHostName()或getHostAddress();
new String(pckt.getData(), 0, pckt.getLength()); //这个可以得到数据内容。
sct.close();

UDP的特点就是没有谁做服务器的概念,只要定义好相同的端口,谁都可以发送,谁都可以接收。

4. URL的类使用:

统一资源定位符: 可以定位网上的的某一个资源。
协议://ip地址: 端口号/项目名/资源位置。
new URL(地址String);
getProtocol()得到协议名。getHost();得到主机。getPort()得到端口。getPath()得到地址。
getFile()得到文件。getQuery()得到查询名。
利用URL下载文件:

HttpURLConnection urlCon = (HttpURLConnection) url.openConnection(); //返回URL的连接
InputStream strm =urlCon.getInputStream();                  //从连接里得到流资源
FileOutputStream fos =new FileOutputStream(str文件流的文件名); 
//繁琐的读缓冲区
byte[] buff =new byte[1024];
int len
while((len=inputStream.read(buff))!=1){
        fos.write(buffer, 0, len);  //往信件的文件流中写入连接流。
}
fos.close();
strm.close();
urlCon.disconnect();

几行代码就可以搞定网络资源的文件了。
Apache Tomcat的启动:
cd /Users/用户名/tomcat/bin,进入到tomcat的bin目录下
输入:./startup.sh + 回车,
如出现错误:“Permission denied”(操作失败,缺少权限)
输入命令,赋予超级管理员权限sudo chmod 755 *.sh + 回车
再次执行命令:./startup.sh + 回车
打开浏览器,输入网址 http://localhost:8080/ ,如果出现一只三角猫,表示tomcat安装成功
关闭的话:./shutdown.sh + 回车
核心配置文件:./conf/server.xml

5. 示例

实现一个标准的TCP/IP通信
下面是服务端,需要先启动,形成一个通信阻塞。核心方法: ServerSocket; Socket; ByteArrayOutputStream; InputStream; buffer的应用以及各种异常处理。

package UseInternet;

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) {
     ByteArrayOutputStream baos = null;
     ServerSocket serverSocket =null;
     Socket socket =null;
     InputStream is =null;
     try {
         serverSocket = new ServerSocket(9999);
         socket = serverSocket.accept(); //这里形成一个阻塞,等待接收客户端信息.
         is = socket.getInputStream();
         baos= new ByteArrayOutputStream();
         byte[] buffer2 = new byte[1024];
         int len2 = 0;
         while ((len2=is.read(buffer2))!=-1){
             baos.write(buffer2,0,len2);
         }
         System.out.println(baos.toString());
         System.out.println("Got msg");
     } 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();
             }
         }
     }
 }
}

下面是客户端代码。

package UseInternet;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpClientDemo1 {
 public static void main(String[] args) {
     Socket socket =null;
     OutputStream os =null;
     try {
         //获得服务器的IP和端口号
         InetAddress serverIP= InetAddress.getByName("127.0.0.1");
         int port = 9999;
         //创建一个socket连接
         socket = new Socket(serverIP, port);
         os = socket.getOutputStream();
         os.write("你好,这是一个网络外发的消息OS".getBytes());
         // 关键修复:1.刷新输出流,确保数据全部发送 2.关闭输出流,发送流结束标记
         os.flush();
         socket.shutdownOutput();

     } catch (UnknownHostException e) {
         e.printStackTrace();
         //throw new RuntimeException(e);
     } catch (IOException e) {
         e.printStackTrace(); // 建议不要直接throw RuntimeException,方便排查问题
     }finally {
         if (os!=null){
             try {
                 os.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
         if(socket!=null){
             try {
                 socket.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 }
}

解析各种网址的IP信息

了解InetAddress.getByName; InetAddress.getLocalHost; getAddress; getCanonicalHostName; getHostAddress; getHostName等常用方法。

import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestInetAddress {
 public static void main(String[] args) {
     try{
         //查询本机地址
         InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
         System.out.println(inetAddress1);
         InetAddress inetAddress2 = InetAddress.getByName("localhost");
         System.out.println(inetAddress2);
         InetAddress inetAddress3 = InetAddress.getLocalHost();
         System.out.println(inetAddress3);

         InetAddress inetAddress4= InetAddress.getByName("www.baidu.com");
         System.out.println(inetAddress4);
         InetAddress inetAddress5= InetAddress.getByName("www.Apple.com");
         System.out.println(inetAddress5);
         System.out.println("====================================");
         System.out.println(inetAddress5.getAddress());      //拿IP地址的字节数据(底层二进制,很少用)
         System.out.println(inetAddress5.getCanonicalHostName());  //拿规范主机名,强制反向DNS解析
         System.out.println(inetAddress5.getHostAddress()); //只拿IP地址字符串(最常用)  
         System.out.println(inetAddress5.getHostName());    //拿主机名(优先用已知的,不强制反向解析

     }catch (UnknownHostException e){
         System.out.println(e);
         e.printStackTrace();
     }
 }
}

UDP发送与接收
服务端:需要了解类和方法:DatagramSocket; DatagramPacket; datagramSocket.receive; datagramSocket.send.

package UseInternet;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TestUDPserver {
 public static void main(String[] args) throws Exception {
     DatagramSocket sct;
     sct = new DatagramSocket(8888);
     byte[] buff=new byte[1024];
     DatagramPacket dgp= new DatagramPacket(buff,0,buff.length);
     sct.receive(dgp);  //阻塞接收
     System.out.println(dgp.getAddress().getHostName()+"|||"+dgp.getAddress().getHostAddress());
     System.out.println(new String(dgp.getData(),0,dgp.getLength()));
     sct.close();
 }
}

客户端:严格说,没有服务端和客户端的区别,都是使用同样的DatagramSocket类。都可以发送和接收。

package UseInternet;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class TestUDP {
 public static void main(String[] args) throws Exception {
     DatagramSocket skt = new DatagramSocket();
     String msg="这是一个UDP的发送测试";
     InetAddress idr =InetAddress.getByName("localhost");
     int port =8888;
     DatagramPacket dgp = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,idr,port);
     skt.send(dgp);  //这里不需要建立连接
     skt.close();
 }
}

网络资源下载

核心类:HttpURLConnection; FileOutputStream;

package UseInternet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class UseURLDownLoad {
 public static void main(String[] args) throws IOException {
     URL url = new URL("https://himg.bdimg.com/sys/portrait/item/pp.1.f0337f07.qLRrh6j5cr5u6SFhRwgS1A?_t=1764409787836");
     HttpURLConnection urlCon =(HttpURLConnection) url.openConnection();
     InputStream strm= urlCon.getInputStream();
     FileOutputStream fos = new FileOutputStream("阳光和青草.jpg");
     byte[] buff = new byte[1024];
     int len;
     while ((len=strm.read(buff))!=-1){
         fos.write(buff,0,len);
     }
     fos.close();
     strm.close();
     urlCon.disconnect();
 }
}

对,就上面这么短一点的代码,只需要把URL换成网页中的各种元素资源,就可以随意的在网页上荡取视频,音乐,图片,堪比歪瓜!