ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

JavaSE学习笔记 2023-12-25 --网络编程

2026/9/20 17:31:25 拓冰建站 浏览量
JavaSE学习笔记 2023-12-25 --网络编程

二十、网络编程

« 上一篇
个人整理非商业用途,欢迎探讨与指正!!


文章目录

    • 二十、网络编程
      • 20.1网络
      • 20.2网络模型
        • 20.2.1 OSI参考模型
        • 20.2.2 TCP/IP参考模型
      • 20.3IP协议
        • 20.3.1什么是IP
        • 20.3.2IPV4和IPV6
        • 20.3.3IP的分类
        • 20.3.4回环地址/回路地址
        • 20.3.5ip相关指令
      • 20.4InetAddress类
      • 20.5端口号
      • 20.6通讯协议
      • 20.7TCP案例


20.1网络

计算机网络:为了实现资源的共享和信息的传递,通过通讯线路将若干台主机连接在一起
常见的计算机网络:
 互联网(Internet):点与点相连
 万维网(WWW World Wide Web):端与端相连
 物连网(IoT Internet of things):物与物相连
 网络编程:让计算机与计算机之间搭建连接,进行通讯

20.2网络模型

20.2.1 OSI参考模型

该模型定义了网络的七层架构:
 物理层 进行数据传输
 数据链路层 保证准确性
 网络层 分配IP地址
 传输层 通过网络协议传输数据(TCP协议,UPD协议)
 会话层 保持主机和主机之间的联系
 表示层 将数据加密或者解密
 应用层 指程序

20.2.2 TCP/IP参考模型

该模型使用OSI参数模型,分为四层:
 网络接口层:物理层 数据链路层
 网络层:
 传输层:
 应用层:会话层 表示层 层

20.3IP协议

20.3.1什么是IP

分配给每台计算机的唯一表示

20.3.2IPV4和IPV6

IPV4:4个字节,32位组成,每位0~255的
IPV6:16个字节,128组成,每位0~65535的

20.3.3IP的分类

A类:大型网络 网络地址.主机地址.主机地址.主机地址
B类:中型网络 网络地址.网络地址.主机地址.主机地址
C类:个人网络 网络地址.网络地址.网络地址.主机地址

20.3.4回环地址/回路地址

本机地址:127.0.0.1 域名:localhost

20.3.5ip相关指令

windows:ipconfig
liunx:ifconfig

20.4InetAddress类

Java中获取IP的类

public class Demo01 {public static void main(String[] args) throws UnknownHostException {
//		获取本机ipInetAddress localHost = InetAddress.getLocalHost();System.out.println(localHost);System.out.println(localHost.getHostName());System.out.println(localHost.getHostAddress());//		获取指定的主机ip信息localHost = InetAddress.getByName("www.baidu.com");System.out.println(localHost);System.out.println(localHost.getHostName());System.out.println(localHost.getHostAddress());System.out.println("======================");
//		获取指定ip地址的信息localHost = InetAddress.getByName("192.168.40.228");System.out.println(localHost);System.out.println(localHost.getHostName());System.out.println(localHost.getHostAddress());localHost = InetAddress.getByName("10.2.10.52");System.out.println(localHost);System.out.println(localHost.getHostName());System.out.println(localHost.getHostAddress());}
}

20.5端口号

每个设备程序在计算机上的唯一数字标识
端口号范围:0~65535
 公认端口号:0~1023
 注册端口号:1024~49151
 动态端口号:49151~65535
常见的端口号:
 mysql:3306
 oracle:1521
 redis:6379
 tomcat:8080
 web服务器:80
 ftp服务器:21
 自定义端口号:1024~65535

20.6通讯协议

TCP:
 面向连接,安全可靠,效率低,数据大小无限制
 打电话
UDP:
 面向无连接,不安全,效率高,数据大小64kb
 广播/对讲机

20.7TCP案例

案例一:

//	客户端:只有启动状态的服务器可以接收我们的请求
public class Client {public static void main(String[] args) throws Exception {
//		创建客户端Socket对象Socket socket = new Socket("192.168.40.172",6666);
//		创建Socket输出流OutputStream out = socket.getOutputStream();
//		写内容out.write("6666你好,我是客户端wxx".getBytes());System.out.println("我写完了...");
//		关闭资源out.close();socket.close();}
}
//	服务器端:启动状态
//	服务器端 ip:192.168.40.172 端口:6666
public class Server {public static void main(String[] args) throws Exception {
//		创建服务器端SocketServerSocket serverSocket = new ServerSocket(6666);
//		通过serverSocket创建Socket对象
//		接收到的信息,进行处理Socket accept = serverSocket.accept();
//		获取客户端发送的内容InputStream in = accept.getInputStream();byte[] bs = new byte[1024];int len;while( (len = in.read(bs)) != -1 ) {System.out.println(new String(bs,0,len));}in.close();accept.close();serverSocket.close();}
}

案例二:使用多线程完成

//	客户端:只有启动状态的服务器可以接收我们的请求
public class Client {public static void main(String[] args) throws Exception {
//		创建ScannerScanner scanner = new Scanner(System.in);while(true) {
//			创建客户端Socket对象Socket socket = new Socket("localhost",6666);
//			创建Socket输出流OutputStream out = socket.getOutputStream();System.out.println("你想说啥");String words = scanner.next();if("886".equals(words) || "拜拜".equals(words)) {
//				关闭资源out.close();socket.close();break;}//			写内容out.write(words.getBytes());System.out.println("我写完了...");
//			关闭资源out.close();socket.close();}scanner.close();}
}
//	服务器端:启动状态
//	服务器端 ip:192.168.40.172 端口:6666
public class Server {public static void main(String[] args) throws Exception {
//		创建服务器端SocketServerSocket serverSocket = new ServerSocket(6666);System.out.println("服务器已启动,等待您的输入...");while(true) {
//			通过serverSocket创建Socket对象
//			接收到的信息,进行处理Socket accept = serverSocket.accept();
//			获取ip信息String ip = accept.getInetAddress().getHostAddress();String hostName = accept.getInetAddress().getHostName();new Thread(new Runnable() {@Overridepublic void run() {try {InputStream in = accept.getInputStream();
//						转换流InputStreamReader reader = new InputStreamReader(in);
//						字符缓冲流BufferedReader br = new BufferedReader(reader);
//						读取内容String readLine = br.readLine();System.out.println(ip+":"+hostName+":"+readLine);br.close();in.close();accept.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }}).start();}}
}

案例三:文件上传

//	客户端:只有启动状态的服务器可以接收我们的请求
public class Client {public static void main(String[] args) throws Exception {Socket socket = new Socket("192.168.40.172",6666);
//			创建Socket输出流OutputStream out = socket.getOutputStream();//		从当前电脑的本地上传一个文件FileInputStream in = new FileInputStream("file/1.jpg");//		边读边写byte[] bs = new byte[1024];int len;while( (len = in.read(bs)) != -1) {
//			写向服务器端out.write(bs,0,len);}//		告知服务器,我们写完了socket.shutdownOutput();//		等待服务器的返回信息InputStream input = socket.getInputStream();InputStreamReader reader = new InputStreamReader(input);BufferedReader br = new BufferedReader(reader);String str;while( (str = br.readLine()) != null) {System.out.println(str);}//			关闭资源br.close();reader.close();input.close();in.close();out.close();socket.close();}
}
//	服务器端:启动状态
//	服务器端 ip:192.168.40.172 端口:6666
public class Server {public static void main(String[] args) throws Exception {
//		创建服务器端SocketServerSocket serverSocket = new ServerSocket(6666);System.out.println("服务器已启动,等待您的输入...");while(true) {Socket accept = serverSocket.accept();new UploadThread(accept).start();}}
}class UploadThread extends Thread {private Socket socket;public UploadThread(Socket socket) {this.socket = socket;}@Overridepublic void run() {try {
//			读取流InputStream in = socket.getInputStream();//			写入到指定的位置FileOutputStream out = new FileOutputStream("upload/"+UUID.randomUUID().toString()+".jpg");byte[] bs = new byte[1024];int len;while( (len = in.read(bs)) != -1) {out.write(bs, 0, len);}
//			向客户端返回信息OutputStream outputStream = socket.getOutputStream();outputStream.write("success!您的文件以上传成功".getBytes());outputStream.close();out.close();in.close();socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}