首页
统计
关于
Search
1
Sealos3.0离线部署K8s集群
1,087 阅读
2
类的加载
742 阅读
3
Spring Cloud OAuth2.0
726 阅读
4
SpringBoot自动装配原理
691 阅读
5
集合不安全问题
589 阅读
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
登录
Search
标签搜索
Java
CSS
mysql
RabbitMQ
JavaScript
Redis
JVM
Mybatis-Plus
Camunda
多线程
CSS3
Python
Spring Cloud
注解和反射
Activiti
工作流
SpringBoot
Mybatis
Spring
html5
蘇阿細
累计撰写
390
篇文章
累计收到
4
条评论
首页
栏目
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
页面
统计
关于
搜索到
1
篇与
的结果
2020-11-22
通信协议
网络通信协议:速率,传输码率,代码结构,传输控制……TCP/IP协议簇(实际上是一组协议)TCP:用户传输协议UDP:用户数据报协议TCP与UDP对比:TCP:连接、稳定三次握手,四次挥手最少需要三次,保证稳定链接 A:咋地! B:干啥? A:干一架 A:我要走了 B:你要走了吗 B:你真的要走了吗 A:走了客户端、服务端传输完成,释放连接。效率低UDP:不连接,不稳定客户端、服务端:没有明确的界限不管有没有准备好,都可以发给你DDOS(饱和攻击)TCP客户端:1、连接服务器socket2、发送消息package com.network.lesson02; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; /** * @Author suaxi * @Date 2020/11/20 17:37 * 客户端 */ public class TcpClientDemo01 { public static void main(String[] args) { Socket socket = null; OutputStream os = null; //1、要知道服务器的地址、端口号 try { InetAddress serverIp = InetAddress.getByName("127.0.0.1"); int port = 8888; //2、创建一个socke连接 socket = new Socket(serverIp, port); //3、发送消息、IO流 os = socket.getOutputStream(); os.write("故乡的樱花开了".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(); } } } } } 服务器1、建立服务端口ServerSocket2、等待用户连接3、接收客户端发送的消息(管道流)package com.network.lesson02; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; /** * @Author suaxi * @Date 2020/11/20 17:38 * 服务端 */ public class TcpServerDemo01 { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1、建立服务器地址 serverSocket = new ServerSocket(8888); while (true){ //2、等待客户端连接 socket = serverSocket.accept(); //3、读取客户端消息 is = socket.getInputStream(); //管道流 baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length=is.read(buffer))!=-1){ baos.write(buffer,0,length); } System.out.println(baos); } } 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 com.network.lesson02; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /** * @Author suaxi * @Date 2020/11/22 11:26 * 文件上传服务端 */ public class TcpServerDemo02 { public static void main(String[] args) throws IOException { //1、创建服务 ServerSocket serverSocket = new ServerSocket(8889); //2、监听客户端的连接 Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接 //3、获取输入流 InputStream is = socket.getInputStream(); //4、文件输出 FileOutputStream fileOutputStream = new FileOutputStream("recive.jpg"); byte[] bytes = new byte[1024]; int length; while ((length=is.read(bytes))!=-1){ fileOutputStream.write(bytes,0,length); } //通知客户端文件接收完毕 OutputStream os = socket.getOutputStream(); os.write("文件上传成功!".getBytes()); //5、关闭资源 fileOutputStream.close(); is.close(); socket.close(); serverSocket.close(); } } 客户端package com.network.lesson02; import java.io.*; import java.net.InetAddress; import java.net.Socket; /** * @Author suaxi * @Date 2020/11/22 11:26 * 文件上传客户端 */ public class TcpClientDemo02 { public static void main(String[] args) throws Exception { //1、创建一个socket连接 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8889); //2、创建一个输出流 OutputStream os = socket.getOutputStream(); //3、读取文件 FileInputStream fileInputStream = new FileInputStream(new File("test.jpg")); //4、写出文件 byte[] bytes1 = new byte[1024]; int length1; while((length1=fileInputStream.read(bytes1))!=-1){ os.write(bytes1,0,length1); } //通知服务器已经上传完毕 socket.shutdownOutput(); //确定服务器接收完毕,才断开连接 InputStream inputStream = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] bytes2 = new byte[1024]; int length2; while ((length2=inputStream.read(bytes2))!=-1){ baos.write(bytes2,0,length2); } System.out.println(baos.toString()); //5、关闭资源 baos.close(); inputStream.close(); fileInputStream.close(); os.close(); socket.close(); } }
2020年11月22日
211 阅读
0 评论
0 点赞