当前位置:网站首页>network basic learning
network basic learning
2022-08-01 08:03:00 【Kindergarten mobile】
网络套接字编程
认识端口号
-端口号是一个2字节16位的整数;
- 端口号用来标识一个进程, 告诉操作系统, 当前的这个数据要交给哪一个进程来处理;
- IP地址 + 端口号能够标识网络上的某一台主机的某一个进程;
- 一个端口号只能被一个进程占用.
认识TCP协议
TCP(Transmission Control Protocol 传输控制协议)
- 传输层协议
- 有连接
- 可靠传输(On behalf of the relatively low efficiency)
- 面向字节流
- 源/目的端口号: 表示数据是从哪个进程来, 到哪个进程去
- 32位序号/32位确认号
- 4位TCP报头长度: 表示该TCP头部有多少个32位bit(有多少个4字节); 所以TCP头部最大长度是15 * 4 = 60
- 6位标志位
URG: 紧急指针是否有效
ACK: 确认号是否有效
PSH: 提示接收端应用程序立刻从TCP缓冲区把数据读走
RST: 对方要求重新建立连接; 我们把携带RST标识的称为复位报文段
SYN: 请求建立连接; 我们把携带SYN标识的称为同步报文段
FIN: 通知对方, 本端要关闭了, 我们称携带FIN标识的为结束报文段 - 16位窗口大小:填充的是自身的接受缓冲区中剩余空间的大小,支持流量控制
- 16位校验和: 发送端填充, CRC校验. 接收端校验不通过, 则认为数据有问题. 此处的检验和不光包含TCP首部, 也包含TCP数据部分.
- 16位紧急指针: 标识哪部分数据是紧急数据
- 40字节头部选项
TCPHow do you decide how the payload and headers separation?
先读取前20个字节,提取出其中4位首部长度,And then let the length of the first4,Is the size of the header,It can be separate header and payload.
TCPHow to decide to their payload delivery to the top of the agreement?
Purpose port can decide the payload to the who
TCP建立连接
tcpEstablish a connection is the three-way handshake
这就是三次握手.
Three-way handshake belongs to communication details!!The upper user does not need to care about,双方OS(TCP)自动完成.
TCP断开连接
TCP的可靠性
什么是可靠性?What is the reliability of the real?
一般而言,We believe that only I sent you a message received a response,Is my hair of reliable message received by the other party.
一般而言,To send data should have response,This is to guarantee the reliability of the underlying policy,这就是确认应答机制.
确认应答机制
TCP将每个字节的数据都进行了编号. 即为序列号(保证可靠性)
每一个ACK都带有对应的确认序列号, 意思是告诉发送者, 我已经收到了哪些数据; 下一次你从哪里开始发.
为什么TCPWant to use two sets of serial number mechanism——Confirm the serial number and serial number?
因为TCP是全双工的.May communicate with both sides at the same time.
超时重传机制
如果主机A在一个特定时间间隔内没有收到B发来的确认应答, 就会进行重发
但是, 主机A未收到B发来的确认应答, 也可能是因为ACK丢失了
因此主机B会收到很多重复数据. 那么TCP协议需要能够识别出那些包是重复的包, 并且把重复的丢弃掉. 这时候我们可以利用前面提到的序列号, 就可以很容易做到去重的效果.
认识UDP协议
UDP(User Datagram Protocol 用户数据报协议)
- 传输层协议
- 无连接
- 不可靠传输
- 面向数据报
UDP协议端格式
- 16位源端口和16Of destination port from where to where to go
- 16位UDPOn behalf of the entire length data submitted to the(UDP首部+UDP数据)的最大长度
UDP的特点
UDP传输的过程类似于寄信
- 无连接: 知道对端的IP和端口号就直接进行传输, 不需要建立连接
- 不可靠: 没有确认机制, 没有重传机制; 如果因为网络故障该段无法发到对方, UDP协议层也不会给应用层返回任何错误信息;
- 面向数据报: 不能够灵活的控制读写数据的次数和数量
UDPHow to ensure its own header and payload separation?
*****UDP是定长报头(8字节)
UDPHow to make the decision to own payload to the top of the agreement?
*****Because of the aim port
What is called the header?
UDPIt is to belong to the kernel stack(C语言),用CLanguage is how to identify the header?
The transport layer is the process of how to find the corresponding server through the port number
socket编程接口
socket 常见API
// 创建 socket 文件描述符 (TCP/UDP, 客户端 + 服务器)
int socket(int domain, int type, int protocol);
// 绑定端口号 (TCP/UDP, 服务器)
int bind(int socket, const struct sockaddr *address,
socklen_t address_len);
// 开始监听socket (TCP, 服务器)
int listen(int socket, int backlog);
// 接收请求 (TCP, 服务器)
int accept(int socket, struct sockaddr* address,
socklen_t* address_len);
// 建立连接 (TCP, 客户端)
int connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
sockaddr结构
套接字不仅支持跨网络的进程间通信,还支持本地的进程间通信(域间套接字).在进行跨网络通信时我们需要传递的端口号和IP地址,而本地通信则不需要,因此套接字提供了sockaddr_in结构体和sockaddr_un结构体,其中sockaddr_in结构体是用于跨网络通信的,而sockaddr_un结构体是用于本地通信的.
为了让套接字的网络通信和本地通信能够使用同一套函数接口,于是就出现了sockeaddr结构体,该结构体与sockaddr_in和sockaddr_un的结构都不相同,但这三个结构体头部的16个比特位都是一样的,这个字段叫做协议家族.
传输层
再谈端口号
端口号(Port)标识了一个主机上进行通信的不同的应用程序;
在TCP/IP协议中, 用 “源IP”, “源端口号”, “目的IP”, “目的端口号”, “协议号” 这样一个五元组来标识一个通信(可以通过netstat -n查看);
一个进程是否可以bind多个端口号
可以的
一个端口号是否可以被多个进程bind
这是不行的,Because a port can only identify only one process
边栏推荐
- HoloView -- Tabular Datasets
- Do I need to introduce any dependencies to write data to clickhouse using flinksql?
- 【STM32】入门(二):跑马灯-GPIO端口输出控制
- HoloView--Customization
- SAP ABAP ALV+SMARTFORS 表分页 报表打印程序
- 【HDLBits 刷题】Circuits(1)Combinational Logic
- pytest接口自动化测试框架 | parametrize中ids的用法
- GO error handling
- JVM:运行时数据区-PC寄存器(程序计数器)
- Data Analysis 6
猜你喜欢
LevelSequence源码分析
支付宝如何生成及配置公钥证书
[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
app 自动化 通过工具查看app 元素 (三)
【HDLBits 刷题】Circuits(1)Combinational Logic
Golang:go模版引擎的使用
如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
【Unity3D】相机
Golang: go static file processing
C语言中编译时出现警告C4013(C语言不加函数原型产生的潜在错误)
随机推荐
力扣每日一题-第44天-290. 单词规律
【一句话攻略】彻底理解JS中的回调(Callback)函数
Centos install php7.4, build hyperf, forward RDS
13 - JUC CountDownLatch concurrent programming
力扣周赛304 6135. 图中的最长环 内向基环树
pytest接口自动化测试框架 | parametrize中ids的用法
Golang: go static file processing
巧妙利用unbuffer实时写入
GO错误处理方式
Holoview--Introduction
特殊的日子,值得纪念
Monitor the width and height of the parent element, adapt to the size of the plug-in
静态Pod、Pod创建流程、容器资源限制
Data Analysis 5
VoLTE基础学习系列 | 企业语音网简述
【STM32】入门(二):跑马灯-GPIO端口输出控制
拳头游戏免版权音乐下载,英雄联盟无版权音乐,可用于视频创作、直播
Shell executes SQL to send emails
【STM32】入门(一):环境搭建、编译、下载、运行
app 自动化 打开app (二)