[Java基础] 网络编程
一:TCP/IP模型
OSI参考模型为七个层面:应用层,表示层,会话层,传输层,网络层,数据链路层,物理层;
TCP/IP模型将这七个层面简化成四个层面:前三个层面简化成了:应用层,最后两个层面简化成了:网络接口层;
二:TCP/UDP
TCP协议:是一种面向连接,可靠的,基于字节流的传输层通讯协议,数据大小无限制,建立连接和断开连接需要三次握手四次挥手,是一种长连接
UDP协议:是一种无连接的传输层协议,提供面向事务的简单不可靠信息传输服务,每个包的大小为64KB
三:IP/PORT
IP是一个分配给互联网设备的唯一标识,port是一部设备的端口,一般来讲8000以内的端口都被设备的硬件软件服务给占领了
而一组IP和port能确定一个唯一的连接,IP确定设备,port确定数据的传输端口
四:Socket/ServerSocket
Client类
/** * */ package SocketDemo; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @description: * @author Lzzy * @date 2020年8月12日 * */ public class Client { public static void main(String[] args) { try { Socket s = new Socket("127.0.0.1", 8808); // 写的run() 当写入exit时向对方写入一个exit告知要结束交流 Runnable write = () -> { while (true) { OutputStream os = null; try { os = s.getOutputStream(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Scanner input = new Scanner(System.in); String str = input.nextLine(); if (str.equals("exit")) { try { os.write("exit".getBytes()); os.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } input.close(); break; } else { try { os.write(str.getBytes()); os.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // TODO Auto-generated catch block } } }; // 读的run()方法,当读到了exit后结束此方法 Runnable read = () -> { while (true) { InputStream is = null; try { is = s.getInputStream(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } byte c[] = new byte[1024]; try { is.read(c); if (!new String(c).trim().equals("exit")) { System.out.println(new String(c)); } else { break; } } catch (IOException e) { // TODO Auto-generated catch block } } }; ExecutorService es = Executors.newFixedThreadPool(2); es.submit(read); es.submit(write); es.shutdown(); while (es.isTerminated()) { s.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Server类
/** * */ package SocketDemo; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @description: * @author Lzzy * @date 2020年8月12日 * */ public class Server { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8808); Socket s = ss.accept(); System.out.println("一个连接进入..."); // 写的run() 当写入exit时向对方写入一个exit告知要结束交流 Runnable write = () -> { while (true) { OutputStream os = null; try { os = s.getOutputStream(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Scanner input = new Scanner(System.in); String str = input.nextLine(); if (str.equals("exit")) { input.close(); try { os.write("exit".getBytes()); os.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } else { try { os.write(str.getBytes()); os.flush(); } catch (IOException e) { // TODO Auto-generated catch block } } } }; // 读的run()方法,当读到了exit后结束此方法 Runnable read = () -> { while (true) { byte c[] = new byte[1024]; try { InputStream is = s.getInputStream(); is.read(c); } catch (IOException e) { // TODO Auto-generated catch block } if (!new String(c).trim().equals("exit")) { System.out.println(new String(c)); } else { break; } } }; ExecutorService es = Executors.newFixedThreadPool(2); es.submit(read); es.submit(write); es.shutdown(); while (es.isTerminated()) { s.close(); ss.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署