当前位置:网站首页>Network sockets (UDP and TCP programming)
Network sockets (UDP and TCP programming)
2022-08-10 11:52:00 【Dragon Roar @~】
一:port
本质:The port number is one byte16位的整数【0,65535】【0,2^16-1】
作用:端口号用来标识一个进程,告诉操作系统,Which process is the current data to be handed over for processing
一个端口号只能被一个进程所占用,一个进程可以占用多个端口号
知名端口号【0,1023】:Already used by some well-known protocols
mysql:3306
oracle:1521
二:网络数据的五元组信息
源IP,源端口,目的IP,目的端口,协议
源IP地址:Identifies the host from which network data originates
源端口:Identifies the network data from“源IP”对应的这台主机的哪个进程产生
目的IP:The host to which the bidding network data will go
目的端口:when passing the purposeIP,找到目的主机之后,Find the corresponding process through the destination port
协议:When the two parties transmit data,使用什么协议(一般指UDP/TCP)
三:网络字节序
小端字节序:低位放在低地址
大端字节序:低位放在高地址
主机字节序:The endianness of the host itself,如果是大端,则主机字节序为大端,如果是小端,则主机字节序为小端
网络字节序:规定网络传输数据的时候采用大端字节序进行传输
四:Host byte order and network byte order are converted to each other
主机字节序转换为网络字节序
ip:uint32_t
uint322_t htonl(uint32_t hostlong);
port:unit16_t
uint16_t htons(uint16_t hostshort);
网络字节序转换为主机字节序
ip:unit32_t
uint32_t ntohl(unit32_t netlong);
port:uint16_t
uint16_t ntohs(uint16_t netshort);
五:TCP和UDPProtocol Features and Differences
UDP
无连接:UDPBoth parties before sending data,是不需要进行沟通的,只需要知道对方的ip和端口就好i(The peer process may not be ready)就可以发送
不可靠:不保证UDP数据是可靠的,有序的到达对方
面向数据报:UDP和应用层/网络层递交数据的时候,都是整条数据进行交付的
TCP
面向连接:TCPBoth parties establish a connection before sending data(1.Make sure that the other party communicates normally 2.沟通双方发送后续数据的细节(例如序号))
可靠传输:TCPIt is guaranteed that the transmitted data arrives at the peer end in a reliable and orderly manner
面向字节流:1.对于传输的数据没有明显的边界 2.for the recipient,It can be received as arbitrary bytes
六:UDP-socket编程
编程流程
服务端:创建套接字,绑定地址信息
客户端:创建套接字,不推荐绑定地址信息(可以绑定 )

创建套接字的含义:将进程和网卡进行绑定,Processes can receive data from the network card or send data through the network card
绑定地址信息的含义:绑定ip,Binding a port is to identify a host and a process on the network
for the recipient:The person sending the data knows which process the recipient is on which machine
对于发送方而言:It can identify which machine and which process the network data is sent from
创建接口
#include<sys/socker.h>
int socket(int domain,int type,int protocol);
- domain:地址域-选择一个具体的协议族进行沟通(udp/tcp)可以认为在指定网络层使用什么协议
AF_UNIX:本地域套接字(Use file communication on the same machine,Do not cross domain machines)
AF_INET:Ipv4版本的ip协议
AF_INET6:ipv6版本的ip协议- type:套接字的类型
SOCK_DGRAM:用户数据报套接字-对应UDP
SOCK_STREAM:流式套接字-对应TCP- protocol:协议
0:表示按照套接字类型选择默认协议
SOCK_DGRAM:对应UDP协议
SOCK_STREAM:对应TCP协议
也可以执行具体的协议
IPPROTO_TCP(6):代表tcp协议
IPPROTO_UDP(17):代表udp协议- 返回值:返回套接字操作句柄,本质上是一个文件描述符
大于等于0:创建成功
小于0:创建失败
绑定接口
int bind(int sockfd,const struct sockaddr *addr,socklen_t addrlen);
- sockfd:套接字描述符
- addr:绑定的地址信息 ip+port
- addrlen: 绑定的地址信息长度
发送接口
ssize_t sendto(int sockfd,const void *buf,size_t len,int flags,
const sruct sockaddr *dest_addr,socklen_t addrlen);
- sockfd:套接字描述符
- buf:要发送的数据
- len:要发送的数据长度
- flags:0(阻塞发送)
- dest_addr:地址信息结构,包含了目的ip,目的端口
- daarlen:地址信息长度
- 返回值:成功:返回正常发送的数据,失败:返回-1
接收接口
sszie_t recvfrom(int sockfd, void *buf,size_t len,int flags,
struct sockaddr*scr_addr,socklen_t *addrlen);
- sockfd:套接字描述符
- buf:准备接收数据的缓冲区
- len :Maximum received data size
- flags:0(阻塞接收)
- src_addr:源ip+源端口
- addrlen :出参,Returns the length of the address information
sockaddr
struct scokaddr{
sa_family_t sa_family;//地址域:占用两字节
char sa_data[14];
};
struct sockaddr_in{
sa_family_t sin_family;//地址族 AF_INET
uint16_t sin_port;//端口号
struct int_addr sin_addr;//32位ip地址
char sin_zero[8];//预留未使用
};
struct in_addr{
In_addr_t s_addr;//32位IPv4地址
};

七:TCP-socket编程
编程流程
服务端:创建套接字,绑定地址信息, 监听,获取新连接 ,收发数据,关闭连接
客户端:创建套接字,不推荐绑定地址信息(可以绑定), 发起连接,收发数据,关闭连接
监听的含义:监听tcp客户端新的链接,同客户端建立tcp连接.注意:这个时候,tcpThe establishment of the connection is done in the kernel
获取新连接的含义:获取新连接的套接字描述符,每一个tcpA socket descriptor is always common
发起连接的含义:向服务端发起tcp连接

监听接口
int listen(int sockfd,int backlog);
- sockfd:套接字描述符
- backlog:TCP并发连接数(已完成连接的大小)
- 返回值:成功:0;失败:-1;
未完成连接队列:Connections that are still in the connection establishment process are emulated in this queue(A connection that is doing a three-way handshake)
已完成连接队列:连接已经建立,可以正常通信的连接放在这个队列(The connection for which the three-way handshake has been completed)
可以通过修改:/proc/sys/net/ipv4/tcp_max_syn_backlog当中的值,修改未完成连接队列的大小
Block the calling interface
int accept(int sockfd,struct sockaddr *addr,socklen_t *addrlen);
- sockfd :套接字描述符
- addr:地址信息结构体,描述客户端地址信息的结构体(客户端IP和端口)
- addrlen:地址信息长度
- 返回值:成功:Returns the newly connected socket;失败:-1
注意:This is a blocking calling function
1.如果已完成队列当中没有已经建立连接的连接,则阻塞
2.如果有获取新连接之后,就返回
3.Returns the newly connected socket,to communicate with the client,It's just that this socket does not have a listening function,At the same time, the client's address information
4.Multiple clients initiate connections,The server will create multiple newly connected sockets
连接接口
int connect( int sockfd,const struct sockaddr *addr,socklen_t addrlen);
- sockfd:套接字描述符
- addr :地址信息结构体,Describes the address information of the server(服务端的ip和端口)
- addrlen:地址信息长度
- 返回值:成功:0;失败:-1;
注意:该函数不仅可以完成连接功能,If the client is not bound,At the same time, it will also bind the address information of the client
发送接口
ssize_t send(int sockfd,const void *buf,size_t len,int flags);
- sockfd:套接字描述符(acceptReceive back a newly connected socket,Not a listening socket)
- buf:发送bufPoints to the content of the space
- len:数据长度
- flags:0(阻塞发送)
- 返回值:成功:返回发送的字节数量;失败:-1;
接收接口
ssize_t recv(int sockfd,void *buf,size_t len,.int flags);
- sockfd:套接字描述符
- buf:将接收到的数据存放在buf指定的空间,Space needs to be opened up in advance
- len :The expected number of bytes to receive
- flags:0(阻塞接收)
- 返回值:成功:the number of bytes received; 0:对端关闭连接;-1:接收错误
边栏推荐
- LAXCUS分布式操作系统安全管理
- POJ 2891 Strange Way to Express Integers (扩展欧几里得)
- HDU 6040 Hints of sd0061 (技巧)
- 振弦传感器及核心VM系列振弦采集模块
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- 【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
- 从脚本到剪辑,影像大师亲授的后期制作秘籍
- The brave rice rice, does not fear the brush list of 】 list has a ring
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
- 4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
猜你喜欢

【勇敢饭饭,不怕刷题之链表】有序链表的合并

From the product dimension, why can't we fully trust Layer2?

ENVI 5.3软件安装包和安装教程

AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)

再有人问你分布式事务,把这篇扔给他

建校仅11年就入选“双一流” ,这所高校是凭什么做到的?

Article take you understand interrupt the key driver of polling mechanism

mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded

常量及数据类型你还记得多少?

怎么加入自媒体,了解这5种变现模式,让账号快速变现
随机推荐
为什么Redis很快
std::move()
网络套接字(UDP和TCP编程)
Double.doubleToLongBits()方法使用
POJ 2891 Strange Way to Express Integers (Extended Euclidean)
态路小课堂丨如何为CXP光模块选择光纤跳线?
基于UiAutomator2+PageObject模式开展APP自动化测试实战
三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
接口定义与实现
老板加薪!看我做的WPF Loading!!!
力扣练习——63 找到字符串中所有字母异位词
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
学长告诉我,大厂MySQL都是通过SSH连接的
Programmers pursue technology to consolidate basic learning route suggestions
APP automation testing practice based on UiAutomator2+PageObject mode
Some tips for using Unsafe
快速上手,征服三种不同分布式架构调用方案
HDU 1520 Anniversary party (tree dp)
从产品维度来看 我们为什么不能完全信任Layer2?
微信小程序提交审核历史版本记录从哪里查看