当前位置:网站首页>网络基础 —— 报头、封装和解包
网络基础 —— 报头、封装和解包
2022-07-07 02:18:00 【abs(ln(1+NaN))】
目录
下面以快递的派发为例,来理解报头、封装和解包。
假设你在网上买了一瓶洗发水,卖家帮你装到了盒子里,盒子上注明了这个产品的质量(多少克)等信息,然后交给了快递站,快递站贴上快递单就把这个快递发出去了。
一、理解"报头"
1、什么是报头?
上面有提到两个东西,这里的快递单或者盒子上的产品信息、卖家地址就类似于报头所包含的信息。
站在网络的角度,这个产品就是数据,应用层把这个数据给传输层之前,需要加一个报头,报头包含了数据的有效长度、上层协议是什么等信息。
2、为什么需要报头?
可以举一个反例,如果没有报头,也就是说没有快递单和产品盒子,我们根本不知道这个东西从哪来,也不知道要发到哪里去,这样的话就无法进行数据传输。
3、报头包含了哪些信息?
从应用层到数据链路层,每一层都有自己的协议,而且几乎每一层都会添加一层报头,而这个报头包含的两个重要信息如下,这两个信息将会解包过程派上大用场!
- 当前层报头的长度
- 当前层使用的协议
因为不同主机要实现通信,对应层使用的协议必须相同,好比你顺丰发货,对方在圆通等快递,这显然不合理。
二、理解"封装"
那什么又是封装呢??我们把洗发水装进盒子,给盒子贴上快递单,这个过程就是封装!!

上图蓝色的方块就代表应用层添加的报头 —— 包含了有效载荷的长度(后面统一把数据称为载荷)
上图橙色的方块就代表传输层添加的"报头" —— 包含了上层协议是谁(即从哪来的)、当前层添加的"报头"长度。
网络传输中,每向下传一层就会在最前面添加一层内容。
三、理解"解包"
假设卖家收到快递了,外面的层层包装、快递单、还有产品盒子对于卖家而言,都是不需要的,卖家拆快递,把包装拆下来的过程就叫做解包!

每一层只解开与当前层对应的报头,现在报头里的两个重要内容就可以派上用场了
- 当前层报头的长度(用于解包)
- 上一层协议(要传给上一层的哪个协议)
1、如何解包
站在OS内核角度,我们只让一个指针指向数据包最开始的位置,现在已经知道了当前层报头的长度,我们只需要让指针向右移动相应的位置即可。

2、要传给上层的谁
现在解包解完了,那要继续向上传递,应该给谁呢?你可能会问,这不是直接就给上一层"传输层"吗?
准确来说是要给上层哪个协议,比如卖家是顺丰发货,而你却在圆通快递站等着,这肯定不合理。
站在网络角度,对方使用UDP协议,报头就包含了这个信息,你就应该传给传输层的UDP
边栏推荐
- Tkinter window selects PCD file and displays point cloud (open3d)
- How can I check the DOI number of a foreign document?
- Redis(一)——初识Redis
- mobx 知识点集合案例(快速入门)
- SVN version management in use replacement release and connection reset
- Audio distortion analysis of DSP and DAC based on adau1452
- Common problems of caching in high concurrency scenarios
- Programmers' daily | daily anecdotes
- Force deduction 62 different paths (the number of all paths from the upper left to the lower right of the matrix) (dynamic planning)
- How to set up in touch designer 2022 to solve the problem that leap motion is not recognized?
猜你喜欢

Programmers' daily | daily anecdotes

How to use wechat cloud hosting or cloud functions for cloud development of unapp development applet

软件测试到了35岁,真的就干不动了吗?

Test the foundation of development, and teach you to prepare for a fully functional web platform environment

How to install swoole under window

VMware安装后打开就蓝屏

HKUST & MsrA new research: on image to image conversion, fine tuning is all you need

Redis (I) -- getting to know redis for the first time

哈趣投影黑马之姿,仅用半年强势突围千元投影仪市场!

Developers don't miss it! Oar hacker marathon phase III chain oar track registration opens
随机推荐
怎样查找某个外文期刊的文献?
【从零开始】win10系统部署Yolov5详细过程(CPU,无GPU)
POI export to excel: set font, color, row height adaptation, column width adaptation, lock cells, merge cells
隐马尔科夫模型(HMM)学习笔记
c面试 加密程序:由键盘输入明文,通过加密程序转换成密文并输出到屏幕上。
LM small programmable controller software (based on CoDeSys) Note 23: conversion of relative coordinates of servo motor operation (stepping motor) to absolute coordinates
MySQL卸载文档-Windows版
Google Chrome browser released patch 103.0.5060.114 to fix the 0-day vulnerability
How to keep accounts of expenses in life
Performance comparison between Ceres solver and g2o
Unity C# 函数笔记
Etcd database source code analysis -- starting from the start function of raftnode
uniapp开发小程序如何使用微信云托管或云函数进行云开发
Postgresql源码(60)事务系统总结
Unable to debug screen program with serial port
[shell] summary of common shell commands and test judgment statements
dolphinscheduler3.x本地启动
The difference between string constants and string objects when allocating memory
Handling hardfault in RT thread
VIM mapping large K