基于TCP/UDP的Socket编程

news2024/9/29 23:31:43

---- socket概述:

socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用已实现进程在网络中通信。

socket起源于UNIX,在Unix一切皆文件哲学的思想下,socket是一种"打开—读/写—关闭"模式的实现,服务器和客户端各自维护一个"文件",在建立连接打开后,可以向自己文件写入内容供对方读取或者读取对方内容,通讯结束时关闭文件。

---- 接口简介:

socket():创建socket

bind():绑定socket到本地地址和端口,通常由服务端调用

listen():TCP专用,开启监听模式

accept():TCP专用,服务器等待客户端连接,一般是阻塞态

connect():TCP专用,客户端主动连接服务器

send():TCP专用,发送数据

recv():TCP专用,接收数据

sendto():UDP专用,发送数据到指定的IP地址和端口

recvfrom():UDP专用,接收数据,返回数据远端的IP地址和端口

closesocket():关闭socket

---- 流程如下:

接口详解,常用的系统调用如下:

>> socket() : creating  a socket 

A socket is an abstraction of a communication endpoint. Just as they would use file descriptors to access files, applications use socket descriptors to access sockets. To create a socket, we call the socket() function.

原型:int socket(int domain, int type, int protocol);

返回值: returns file (socket) descriptor if OK, -1 on error.

domain:AF_INET, AF_INET6, AF_UNIX, AF_UNSPEC (address format)

type:SOCK_DGRAM, SOCK_RAW, SOCK_STREAM, SOCK_SEQPACKET

protocol:IPPROTO_IP,  IPPROTO_IPV6,  IPPROTO_TCP, IPPROTO_UDP

The protocol argument is usually zero, to select the default protocol for the given domain and socket type. The default protocol for a SOCK_STREAM socket in the AF_INET communication domain is TCP(Transmission Control Protocol). The default protocol for a SOCK_DGRAM socket in the AF_INET communication domain is UDP(User Datagram Protocol).

NOTE: UDP -- 数据报(datagram),无连接的,no logical connection exist between peers for them to communicate. A datagram is a self-contained(独立的) message. 类似于(analogous)发邮件,你可以发送多封邮件,但是不能保证邮件是否到达和邮件到达的顺序。每一封邮件都包含了接收者的地址。

TCP -- 字节流 A byte stream(SOCK_STREAM), in contrast, 在传输数据之前,需要建立连接,面向连接的通信类似于打电话。点到点的连接里包含了source and destination。

Communication on a socket is bidirectional. We can disable I/O on a socket with the shutdown function.

>> shutdown()   

原型:int shutdown(int sockfd, int how);

返回值: returns 0 if OK, -1 on error.

The shutdown() system call closes one or both channels of the socket sockfd, depending on the value of how, which is specified as one of the following:

how:  SHUT_RD, then reading from the socket is disabled.  SHUT_WR, then we can't use the socket for transmitting data. We can use SHUT_RDWR to disable both data transmission and reception.

shutdown() differs from close() in another important respect: it closes the socket channels regardless of whether there are other file descriptors referring to the socket. For example, sockfd refers to a connected stream socket. If we make the following calls, then the connection remains open, and we can still perform I/O on the connection via the file descriptor fd2:

1.    fd = dup(sockfd);

2.    close(sockfd);

However, if we make the following sequence of calls, then both channels of the connection are closed, and I/O can no longer be performed via fd2:

1.    fd2 = dup(sockfd);

2.    shutdown(sockfd,SHUT_RDWR);

Note that shutdown() doesn't close the file descriptor, even if how is specified as SHUT_RDWR. To close the file descriptor, we must additionally call close().

>> bind() : binding a socket to an address    

The bind() system call binds a socket to an address.

原型:int bind(int sockfd, const struct sockaddr * addr, socklen_t addrlen);

返回值:returns 0 on success, or -1 on error.

The sockfd argument is a file descriptor obtained from a previous call to socket(). The addr argument is a pointer to a structure specifying the address to which this socket is to be bound. The type of structure passed in this argument depends on the socket domain. The addrlen argument specifies the size of the address structure.

Typically, we bind a server's socket to a well-known address - that is, a fixed address that is known in advance to client applications that need to communicate with that server.

>> listen() : listening for incoming connections    

原型:int listen(int sockfd, int backlog); // returns 0 on success, or -1 on error.

The listen() system call marks the stream socket referred to by the file descriptor sockfd as passive. The socket will subsequently be used to accept connections from other(active) sockets.

The client may call connect() before the server calls accept(). This could happen, for example, because the server is busy handling some other clients. This results in a pending connection, as illustrated in Figure 56-2.

The kernel must record some information about each pending connection request so that a subsequent accept() can be processed. The backlog argument allows us to limit the number of such pending connections. Further connection requests block until a pending connection is accepted(via accept()), and thus removed from the queue of pending connections.

>> accept() : accepting a connection   

The accept() system call accepts an incoming connection on the listening stream socket referred to by the file descriptor sockfd. If there are no pending connections when accept() is called, the call blocks until a connection request arrives when the sockfd in block mode. If sockfd is in nonblocking mode, accept() will return -1 and set errno to either EAGAIN or EWOULDBLOCK.

原型:int accept(int sockfd, struct sockaddr * restrict addr, socklen_t * restrict len);

返回值:return file(socket) descriptor if OK, -1 on error.

本函数从s的等待连接队列中抽取第一个连接,创建一个与s同类的新的套接口并返回句柄。如果队列中无等待连接,且套接口为阻塞方式,则accept()阻塞调用进程直至新的连接出现。如果套接口为非阻塞方式且队列中无等待连接,则accept()返回一错误代码WSAEWOULDBLOCK。已接受连接的套接口不能用于接受新的连接,原监听套接口仍保持开放。

The key point to understand about accept() is that it creates a new socket, and this new socket that is connected to the peer socket that performed the connect(). This new socket descriptor has the same socket type and address family as the  original socket(sockfd). A file descriptor for the connected socket is returned as the function result of the accept() call. The listening socket(sockfd) remains open, and can be used to accept further connections. A typical server application creates one listening socket, binds it to a well-known address, and then handles all client requests by accepting connections via that socket.

The remaining(剩余的) arguments to accept() return the address of the peer socket.(客户端)

If we don't care about the client's identity, we can set the addr and len parameters to NULL. Otherwise, before calling accept, we need to set the addr (指向一个buffer) parameter to a buffer large enough to hold the address and set the integer pointed to by len to the size of the buffer in bytes. On return, accept will fill in the client's address in the buffer and update the integer pointed to by len to reflect the size of the address.

>> connect() : connecting to a peer socket  

原型:int connect(int sockfd, const struct sockaddr * addr, socklen_t addrlen);

返回值: returns 0 on success, or -1 on error.

The connect() system call connects the active socket referred to by the file descriptor sockfd to the listening socket whose address is specified by addr and addrlen.

>> send() : TCP类型的数据发送 

原型:int send(int sockfd, const void * msg, int len, int flags);

每个TCP套接口都有一个发送缓冲区,它的大小可以用SO_SNDBUF这个选项来改变。调用send函数的过程实际是内核将用户数据(msg)拷贝至TCP套接口的发送缓冲区的过程。若len大于发送缓冲区的大小,则返回-1. 否则,查看缓冲区剩余空间是否容纳得下要发送的len长度,若不够,则拷贝一部分,并返回拷贝长度(指的是非阻塞send,若为阻塞send,则一定等待所有数据拷贝至缓冲区才返回,因此阻塞send返回值必定与len相等)。若缓冲区满,则等待发送,有剩余空间后拷贝至缓冲区,若在拷贝过程中出现错误,则返回-1.关于错误的原因,查看errno的值。

注意:send成功返回并不代表对方已接收到数据,如果后续的协议传输过程中出现网络错误,下一个send便会返回-1发送错误。TCP给对方的数据必须在对方给予确认时,方可删除发送缓冲区的数据。否则,会一直缓存到缓冲区直至发送成功。

参数解释:

sockfd -- 发送端套接字描述符 (非监听描述符)

msg -- 待发送数据的缓冲区 (将其内容的len长度拷贝到socket的发送缓冲区)

len -- 待发送数据的字节长度。

flags -- 一般情况下置为0.

>> recv() : TCP类型的数据接收 

原型:int recv(int sockfd, void *buf, int len, unsigned int flags);

recv()从接收缓冲区拷贝数据。成功时,返回拷贝的字节数,失败时,返回-1。阻塞模式下,recv()将会阻塞直到缓冲区中至少有一个字节才返回,没有数据时处于休眠状态。若非阻塞,则立即返回,有数据则返回拷贝的数据大小,否则返回错误-1.

参数解释:

sockfd -- 接收端套接字描述符(非监听描述符)

buf -- 接收数据的基地址(将socket的接收缓冲区中的内容拷贝至buf中)

len -- 接收到数据的字节数

flags -- 一般情况下置为0.

>> sendto() : UDP类型的数据发送 

原型:int sendto(int sockfd, const void * msg, int len, unsigned int flags, const struct sockaddr * dst_addr, int addrlen);

用于非可靠连接(UDP)的数据发送,因为UDP方式未建立连接socket,因此需要指定目的socket的address。

可使用同一个UDP套接口描述符sockfd和不同的目的地址通信。而TCP要预先建立连接,每个连接都会产生不同的套接口描述符,体现在:客户端要使用不同的fd进行connect,服务端每次accept产生不同的fd。

UDP没有真正的发送缓冲区,因为是不可靠连接,不必保存应用进程的数据拷贝,应用进程的数据在沿协议栈向下传递时,以某种形式拷贝到内核缓冲区,当数据链路层把数据传出后就把内核缓冲区中数据拷贝删除。因此它不需要一个发送缓冲区。

For sendto(), the dest_addr and addrlen arguments specify the socket to which the datagram is to be sent. These arguments are employed in the same manner as the corresponding arguments to connect(). The dest_addr argument is an address structure suitable for this communication domain. It is initialized with the address of the destination socket. The addrlen argument specifies the size of addr.

>> recvfrom() : UDP类型的数据接收 

原型:int recvfrom(int sockfd, void * buf, size_t len, int flags, struct sockaddr * src_addr, int * addrlen);

参数解释:

sockfd -- 接收端套接字描述;

buf -- 用于接收数据的应用缓冲区地址;

len -- 指明缓冲区大小;

flags -- 通常为0;

src_addr -- 数据来源端的地址(IP address,Port number).

fromlen -- 作为输入时,fromlen常常设置为sizeof(struct sockaddr).

For recvfrom(), the src_addr and addrlen arguments return the address of the remote socket used to send the datagram. (These arguments are analogous to the addr and addrlen arguments of accept(), which return the address of a connecting peer socket.) Prior to the call(在调用之前), addrlen should be initialized to the size of the structure pointed to by src_addr(结构的大小); upon return(在返回时), it contains the number of bytes actually written to this structure.

对嵌入式物联网感兴趣的小伙伴,可以多了解一下相关信息。(看过来)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/670679.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

springboot启动流程 (3) 自动装配

在SpringBoot中,EnableAutoConfiguration注解用于开启自动装配功能。 本文将详细分析该注解的工作流程。 EnableAutoConfiguration注解 启用SpringBoot自动装配功能,尝试猜测和配置可能需要的组件Bean。 自动装配类通常是根据类路径和定义的Bean来应…

005 Settings可以直接通过AndroidStudio安装并调试(二)——Settings 打release包遇到的问题

一.背景 Settings迁移到AndroidStudio中直接打release包是有各种问题的,打不出来包,这里我们详细来描述下Settings打包出现的问题及解决方案。 二.Type com.android.settingslib.widget.BuildConfig is defined multiple times 首先遇到的拦路虎,也是最繁琐的包名冲突,之…

为什么配电室总出故障?这一点你做对了吗

配电室是供电系统中非常关键的组成部分,负责对电能进行分配和控制。然而,传统的配电室监控方式存在一些局限性,如人工巡检的局限性、监测数据获取困难、安全隐患无法及时发现等。 因此,为了提高配电室的管理水平、确保供电系统的安…

剑指offer03.数组中重复的数字

看到这道题的第一眼想到的是先给它排序,然后双指针从左往右遍历,写了一个冒泡排序,但是我想到了应该会超时,因为冒泡时间复杂度是n的平方,输入大小时10000,肯定会超时,然后右又看了一下题目看到…

SpringCloud Alibaba入门之用户子模块开发

在上一章的基础上进行子模块的开发SpringCloud Alibaba入门之创建多模块工程_qinxun2008081的博客-CSDN博客 一、引入SpringBoot 我们在父项目统一管理引入的jar包的版本。我们采用父项目中以depencyMangement方式引入spring-boot,子项目依赖parent父配置即可。 &…

YGG 公会发展计划(GAP)第三季总结

2023年5月6日,Yield Guild Games(YGG)结束了其代币分配系统——公会发展计划(GAP)的第三季,向社区成员提供了更多奖励,以表彰他们对公会和参与游戏的宝贵贡献。 第三季对 GAP 进行了一些升级&am…

uniapp和springboot微信小程序开发实战:前端架构之微信小程序开发表单提交功能

文章目录 前言前端代码后端代码controller层service层总结前言 基本上很多项目都有类似于意见反馈、留言等形式的表单提交功能,今天给大家介绍的是使用uniapp和vue组件实现的表单提交功能。其效果如下: 前端代码 <template><view class="body"><…

Jmeter接口测试-MD5加密-请求验签

目录 前言&#xff1a; 第一部分&#xff1a;先准备好Jmeter 第二部分&#xff1a;编写MD5加密-请求验签的脚本 第三部分&#xff1a;执行脚本 前言&#xff1a; JMeter是一款常用的接口测试工具&#xff0c;对于需要进行加密验证的接口&#xff0c;我们可以使用MD5加密算…

HOOPS Exchange SDK 2023 Crack

领先的 CAD 导入和导出库 使用 HOOPS Exchange SDK 将 30 多种 CAD 文件格式导入您的应用程序以进行 CAD 数据转换&#xff0c;通过单个 API 对 2D 和 3D CAD 文件格式&#xff08;包括 CATIA、SOLIDWORKS、 Inventor、Revit™™、Creo、NX™、Solid Edge 等&#xff09;提供快…

Nvidia官方解码性能

NVIDIA VIDEO CODEC SDK | NVIDIA Developer 1080P解码性能&#xff1a; 720P解码性能&#xff1a; 详细的参见官方的链接地址&#xff0c;对于GPU的解码fps能力&#xff0c;可以作为评估参照&#xff01;

【服务器远程工具】一款好用的xshell

这里写目录标题 背景Tabby简介安装使用SSHSFTPPowerShellGit 设置外观颜色快捷键窗口 插件支持总结 背景 作为一名后端开发&#xff0c;我们经常需要和Linux系统打交道&#xff0c;免不了要使用Xshell这类终端工具来进行远程管理。今天给大家推荐一款更炫酷的终端工具Tabby&…

C++核心编程——详解函数模板

纵有疾风起&#xff0c;人生不言弃。本文篇幅较长&#xff0c;如有错误请不吝赐教&#xff0c;感谢支持。 &#x1f4ac;文章目录 一.模板的基础知识①为什么有模板&#xff1f;②初识模板 二.函数模板①函数模板的定义②函数模板的使用③函数模板实例化1️⃣隐式实例化2️⃣显…

QAC用户使用手册

文章目录 1 QAC介绍1.1 QAC简介1.2 QAC dashboard简介 2 QAC使用&#xff08;基本操作&#xff09;2.1 创建QAC工程2.2 创建QAC工程2.3 添加代码到QAC工程2.4 添加代码到QAC工程2.5 上传分析报告及结果 1 QAC介绍 1.1 QAC简介 Helix QAC是Perforce公司(原PRQA公司)产品,主要用…

「Java核心技术大会 2023」——小解送书第三期

目录 共同深入探讨 Java 生态&#xff01;直播预约&#xff1a;视频号“IT阅读排行榜” 抽奖 大会简介 人工智能在22年、23年的再次爆发让Python成为编程语言里最大的赢家&#xff1b;云原生的持续普及令Go、Rust等新生的语言有了进一步叫板传统技术体系的资本与底气。我们必…

Android studio项目编译进安卓源码中

最近要做一个Android 8.1 的launcher &#xff0c;在Androidstudio上开发好基本功能后&#xff0c;移到Android源码中编译 1.在源码中创建代码目录 我开发基于展讯9820e平台&#xff0c;在如下目录创建好对应名字的文件夹 \vendor\sprd\platform\packages\apps\xxxLauncher创…

回收站清空了怎么恢复?3个妙招恢复数据

回收站被人为清空&#xff0c;被放入回收站的文件因时间过久而被电脑自动删除时&#xff0c;回收站里的数据清空了还能找到吗&#xff1f;是可以的这3个小妙招可以帮你还原回收站的数据&#xff01; 妙招一&#xff1a;借助注册表还原回收站清空的数据 可以尝试借助注册表还原…

Bootstrap 环境安装

文章目录 Bootstrap 环境安装下载 Bootstrap 文件结构预编译的 BootstrapBootstrap 源代码 HTML 模板实例Bootstrap CDN 推荐 Bootstrap 环境安装 Bootstrap 安装是非常容易的。本章将讲解如何下载并安装 Bootstrap&#xff0c;讨论 Bootstrap 文件结构&#xff0c;并通过一个实…

常见的Jmeter参数化方式总结

目录 前言&#xff1a; 参数化概念 参数化方式 二、用户变量 三、CSV数据文件 四、函数助手 前言&#xff1a; 在进行接口性能测试时&#xff0c;我们通常需要针对不同的场景进行参数化操作。JMeter是一款强大的性能测试工具&#xff0c;它提供了多种参数化方式&#xff0c;方便…

Idea在JavaSE项目中配置JavaEE

新建模块&#xff08;File --> new --> Module...&#xff09;javase项目 选择了这个webapp的支持之后&#xff0c;IDEA会自动给你生成一个符合Servlet规范的webpp目录结构。 如果说我们现在需要使用servlet的和JSP 那么需要servlet和JSP的jar包 也可以选择添加库,但是…

qt udp通信

udp不分客户端和服务器&#xff0c;只需要使用一个类 QUdpSocket 这里写目录标题 界面设计qudpsocketthis按钮 打开按钮 发送 关闭 界面设计 接收框设置为 只读 为ui界面各个模块改名字 本低端口和目标ip框对齐&#xff0c;可以对目标ip 宽度设置 为一样 水平策略 qudpsocke…