2.深入了解bind函数

article/2025/10/19 4:13:53

bind函数

  • 1.查看方法
  • 2.详细解说(中文)
    • bind函数:
  • 3.bind文档

1.查看方法

使用指令:man bind

bind

bind

2.详细解说(中文)

bind函数:

1.功能:
bind函数把一个本地协议地址赋予一个套接字
对于网际网协议,协议地址是32位的IPv4地址或者128位的IPv6地址与16位的TCP和UDP端口号的联合。

2.函数原型:

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);

3.参数说明:

1.sockfd: socket文件描述符
2.*addr: 一个指向特定协议的地址结构的指针
3.addrlen: *addr的长度

4.函数返回值:

成功:  0
失败:-1

5.bind()使用:针对TCP/IP

1.调用bind()时,可以指定一个端口号
2.调用bind()时,可以指定一个IP地址
3.调用bind()时,可以指定端口号和IP地址
4.调用bind()时,可以都不指定

下表汇总了如何根据预期的结果,选择设置sin_addrsin_port或者sin6_addrsin6_port

IP地址端口结果
通配地址0内核选择IP地址和端口
通配地址非0内核选择IP地址,进程指定端口
本地IP地址0进程指定IP地址,内核指定端口
本地IP地址非0进程指定IP地址和端口

对于IPv4: 通配地址由常值INADDR_ANY来指定,其值一般为0,它告知内核去选择IP地址。如下复制语句:

struct sockaddr_in servaddr;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);  /*wildcard*/

如此复制语句对IPv4是可行的,因为其IP地址是32位的值,可以用一个简单的数字常值表示;对于IPv6,我们就不能这样做,因为128位的IPv6地址是存放在一个结构中,在C语言总赋值语句的右边不能是常值结构,所以我们改写为:

struct sockaddr_in6 serv;
serv.sin6_addr = in6addr_any;  /*wildcard*/

系统预先分配in6addr_any变量并将其初始化为常值IN6ADDR_ANY_INIT。头文<netinet/in.h>中包含in6addr_any的extern声明。

无论是网络字节序还是主机字节序,INADDR_ ANY的值(为0)都一样,因此使用htonl并非必需。不过既然头文件<net inet/in. h>中定义的所有INADDR_常值都是按照主机字节序定义的,我们应该对任何这些常值都使用htonl.

如果让内核来为套接字选择一个临时端口号,那么必须注意,函数bind并不返回所选择的值。实际上,由于bind函数的第二个参数有const限定词,它无法返回所选之值。为了得到内核所选择的这个临时端口值,必须调用函数getsockname来返回协议地址。

进程捆绑非通配IP地址到套接字上的常见例子是在为多个组织提供Web服务器的主机上。首先,每个组织都得有各自的域名,譬如这样的形式: www.organization.com.其次,每个组织的域名都映射到不同的IP地址,不过通常仍在同一个子网上。

举例来说:
如果子网是198.69.10
那么第一个组织的IP地址可以是198.69.10.128;
第二个组织的可以198.69.10.129等等。

然后,把所有这些IP地址都定义成单个网络接口的别名( 譬如在4.4BSD系统上就使用ifconfig命令的alias选项来定义),这么一来,IP层将接收所有目的地为任何一个别名地址的外来数据报。最后,为每个组织启动一个HTTP服务器的副本,每个副本仅仅捆绑相应组织的IP地址。

3.bind文档

BIND(2)                                                             Linux Programmer's Manual                                                            BIND(2)NAMEbind - bind a name to a socketSYNOPSIS#include <sys/types.h>          /* See NOTES */#include <sys/socket.h>int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);DESCRIPTION  描述When a socket is created with socket(2), it exists in a name space (address family) but has no address assigned to it.  bind() assigns the address speci‐fied by addr to the socket referred to by the file descriptor sockfd.  addrlen specifies the size, in bytes, of the address structure pointed to by addr.Traditionally, this operation is called “assigning a name to a socket”.当使用socket(2)创建套接字时,它存在于一个名称空间(地址族)中,但没有分配给它地址。Bind()将由addr指定的地址分配给由文件描述符sockfd引用的套接字。Addrlen以字节为单位指定addr所指向的地址结构的大小。传统上,这个操作被称为“给套接字赋一个名称”。It is normally necessary to assign a local address using bind() before a SOCK_STREAM socket may receive connections (see accept(2)).在SOCK_STREAM套接字接收连接之前,通常需要使用bind()分配一个本地地址(accept(2))。The rules used in name binding vary between address families.  Consult the manual entries in Section 7 for detailed information.  For AF_INET, see ip(7);for AF_INET6, see ipv6(7); for AF_UNIX, see unix(7); for AF_APPLETALK, see ddp(7); for  AF_PACKET,  see  packet(7);  for  AF_X25,  see  x25(7);  and  forAF_NETLINK, see netlink(7).名称绑定中使用的规则因地址族而异。详细信息请参阅第7节中的手册条目。AF_INET,见ip(7); AF_INET6,见ipv6(7);对于AF_UNIX,参见unix(7);AF_APPLETALK,见ddp(7);AF_PACKET,见packet(7);对于AF_X25,见x25(7);AF_NETLINK参见netlink(7)。The actual structure passed for the addr argument will depend on the address family.  The sockaddr structure is defined as something like:为addr参数传递的实际结构将取决于地址族。sockaddr结构的定义如下:struct sockaddr {sa_family_t sa_family;char        sa_data[14];}The only purpose of this structure is to cast the structure pointer passed in addr in order to avoid compiler warnings.  See EXAMPLE below.这个结构的唯一目的是对addr中传递的结构指针进行强制转换,以避免编译器警告。请参见下面的例子。RETURN VALUE 返回值On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.如果成功,则返回0。在出错时,返回-1,并适当地设置errno。ERRORSEACCES The address is protected, and the user is not the superuser.EACCES 地址受保护,用户不是超级用户。EADDRINUSEThe given address is already in use.给定地址已被使用。
EADDRINUSE(Internet  domain  sockets)  The  port  number was specified as zero in the socket address structure, but, upon attempting to bind to an ephemeralport, it was determined that all port numbers in the ephemeral port range are currently in use.  See the discussion  of  /proc/sys/net/ipv4/ip_lo‐cal_port_range ip(7).在套接字地址结构中,端口号被指定为0,但是,当尝试绑定一个临时端口时,它被确定在临时端口范围内的所有端口号目前都被使用。
cal_port_range ip(7)。EBADF  sockfd is not a valid file descriptor.Sockfd不是一个有效的文件描述符。EINVAL The socket is already bound to an address.EINVAL addrlen is wrong, or addr is not a valid address for this socket's domain.ENOTSOCKThe file descriptor sockfd does not refer to a socket.The following errors are specific to UNIX domain (AF_UNIX) sockets:EACCES Search permission is denied on a component of the path prefix.  (See also path_resolution(7).)EADDRNOTAVAILA nonexistent interface was requested or the requested address was not local.EFAULT addr points outside the user's accessible address space.ELOOP  Too many symbolic links were encountered in resolving addr.ENAMETOOLONGaddr is too long.ENOENT A component in the directory prefix of the socket pathname does not exist.ENOMEM Insufficient kernel memory was available.ENOTDIRA component of the path prefix is not a directory.EROFS  The socket inode would reside on a read-only filesystem.CONFORMING TOPOSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD (bind() first appeared in 4.2BSD).NOTESPOSIX.1  does  not require the inclusion of <sys/types.h>, and this header file is not required on Linux.  However, some historical (BSD) implementationsrequired this header file, and portable applications are probably wise to include it.For background on the socklen_t type, see accept(2).BUGSThe transparent proxy options are not described.EXAMPLEAn example of the use of bind() with Internet domain sockets can be found in getaddrinfo(3).The following example shows how to bind a stream socket in the UNIX (AF_UNIX) domain, and accept connections:#include <sys/socket.h>#include <sys/un.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#define MY_SOCK_PATH "/somepath"#define LISTEN_BACKLOG 50#define handle_error(msg) \do { perror(msg); exit(EXIT_FAILURE); } while (0)intmain(int argc, char *argv[]){int sfd, cfd;struct sockaddr_un my_addr, peer_addr;socklen_t peer_addr_size;sfd = socket(AF_UNIX, SOCK_STREAM, 0);if (sfd == -1)handle_error("socket");memset(&my_addr, 0, sizeof(struct sockaddr_un));/* Clear structure */my_addr.sun_family = AF_UNIX;strncpy(my_addr.sun_path, MY_SOCK_PATH,sizeof(my_addr.sun_path) - 1);if (bind(sfd, (struct sockaddr *) &my_addr,sizeof(struct sockaddr_un)) == -1)handle_error("bind");if (listen(sfd, LISTEN_BACKLOG) == -1)handle_error("listen");/* Now we can accept incoming connections oneat a time using accept(2) */peer_addr_size = sizeof(struct sockaddr_un);cfd = accept(sfd, (struct sockaddr *) &peer_addr,&peer_addr_size);if (cfd == -1)handle_error("accept");/* Code to deal with incoming connection(s)... *//* When no longer required, the socket pathname, MY_SOCK_PATHshould be deleted using unlink(2) or remove(3) */}SEE ALSOaccept(2), connect(2), getsockname(2), listen(2), socket(2), getaddrinfo(3), getifaddrs(3), ip(7), ipv6(7), path_resolution(7), socket(7), unix(7)COLOPHONThis page is part of release 5.05 of the Linux man-pages project.  A description of the project, information about reporting bugs, and the latest versionof this page, can be found at https://www.kernel.org/doc/man-pages/.Linux                                                                      2019-03-06

参考文档
[1] linux man手册
[2]《Linux高性能服务器》
[3]《Unix网络编程卷1》

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


http://chatgpt.dhexx.cn/article/xZCHGWPq.shtml

相关文章

C/C++ bind函数应用详解

文章目录 知识前导头文件bind介绍bind函数&#xff1a;bind简述&#xff1a; placeholders 用法探究单个参数多个参数成员函数 知识前导 头文件 #include <functional>bind介绍 bind函数&#xff1a; 使用规则&#xff1a; auto newCallable bind(callable, arg_li…

c++bind函数使用

总述 最近写代码的时候看到代码使用了bind&#xff0c;一个参数绑定的标准库函数。程序是这么写的&#xff0c; speaker_play_routine_ new boost::thread (boost::bind(&Speaker::playRoutine, this)); 这是我们一个语音播放的一行代码。 其中 boost::thread是新建一个线…

c++ bind 函数讲解

1.bind 函数的使用详解 可以将bind函数看作一个通用的函数适配器&#xff0c;它接受一个可调用对象&#xff0c;生成一个新的可调用对象来“适应”原对象的参数列表。 调用bind的一般形式&#xff1a;auto newCallable bind(callable,arg_list); 其中&#xff0c;newCallab…

Bind函数详解

自己开发了一个股票软件&#xff0c;功能很强大&#xff0c;需要的点击下面的链接获取&#xff1a; QStockView股票智能分析报警软件下载链接 - 一字千金 - 博客园 Bind函数详解 目录 1 简介... 1 2 使用实例... 1 2.1 bind函数常规使用... 1 2.2 …

网络编程之bind函数

基本TCP客户端/服务器程序的套接字函数 1、bind函数把一个本地协议地址赋予一个套接字。对于网际协议&#xff0c;协议地址是32位的IPv4地址或是128位的IPv6地址与16位的TCP或UDP端口号的组合。 #include<sys/socket.h> int bind(int sockfd, const struct sockaddr, …

一阶数字低通滤波器设计matlab

一阶数字低通滤波器传递函数为&#xff1a; ωc2πfc ωc​为滤波截止角频率 :截止频率&#xff0c;单位&#xff1a; HZ 对低通滤波器进行离散化 一 后向差分法 变换公式为&#xff1a; s(1−z−1)/Ts​ Ts&#xff1a;采样频率 将变化公式带入传递函数&#xff0c;可得…

一文读懂滤波器的线性相位,全通滤波器,群延迟

一文读懂滤波器的线性相位&#xff0c;全通滤波器&#xff0c;群延迟 1. 延迟2. 全通滤波器3.相位延迟和群延迟4. 实际生活中的例子总结&#xff1a; 数字信号处理最常见的面试题&#xff0c;请简述FIR和IIR的区别。其中的一个区别是FIR可以方便地实现线性相位。那这个线性相位…

一阶RC低通滤波器的数学模型及算法实现

一阶RC低通滤波器的数学模型及算法实现 1. 一阶RC低通滤波器的连续域数学模型1.1 数学模型的推导1.2 频率特性1.3 物理作用 2. 一阶RC低通滤波器的算法推导2.1 离散化2.2 滤波系数 3. 一阶RC低通滤波器的C语言实现4. 缺点及改善方法4.1 缺点4.2 改善方法——动态调整滤波系数4.…

数字 一阶低通滤波器 详细分析

事件的起因是下图1&#xff0c;朋友偶然说到一阶低通滤波器&#xff0c;借此来详细介绍一阶低通滤波器的原理&#xff0c;并附上matlab仿真程序代码。图1中的一阶低通数字滤波器的公式为Eq(1)&#xff1a; y(n) q*x(n) (1-q)*y(n-1) Eq(1) 其中&#xff0c;y(n)表示当前…

【滤波器学习笔记】一阶RC低通滤波

一阶RC低通滤波 从模拟到数字 本文整理自网络、《匠人手记》等书籍文章 模拟电路低通滤波时域、频域软件低通滤波 典型电路 图1 典型RC电路 直流、交流、脉冲信号都可以用它 时域 电容电流&#xff1a; Icdqdtd(C∙Uo)dtCdUodt 基尔霍夫电压定律得&#xff1a; UiRCdUo…

一阶低通数字滤波

一阶低通滤波算法&#xff1a; 3、 一阶低通滤波优缺点 一阶低通滤波数学意义&#xff1a;一阶低通滤波法采用本次采样值与上次滤波输出值进行加权&#xff0c;得到有效滤波值&#xff0c;使得输出对输入有反馈作用。 优缺点&#xff1a;滤波系数越小&#xff0c;滤波结果越平稳…

MATLAB—FIR数字滤波器设计

目录&#xff1a; 1 FIR滤波器的原理2 FIR滤波器的特点2.1 相位特性2.1.1 偶对称2.1.2 奇对称 2.2 幅度特性 3 几种滤波器函数3.1 fir1()3.2 fir2()3.3 kaiserord()3.4 firpm() 4 仿真实例4.1 代码4.2 仿真分析 本文是基于MATLAB的数字滤波器设计&#xff0c;所有数据基于计算机…

窗函数法设计FIR中,如何选择窗函数和阶数N

在用窗函数法设计FIR滤波器时&#xff0c;给出了滤波器要求的具体指标&#xff0c;包括通带频率fp、阻带频率fs、通带波纹Rp和阻带衰减As等&#xff0c;有了这些指标后&#xff0c;是否什么窗函数都可以选择呢&#xff1f;答案是否定的。那么怎么选择窗函数呢&#xff1f;在本小…

一阶RC低通滤波器(二)

这篇文章补充下前面讲的一阶低通滤波器。在母线电压采样或是在电机的三相端电压采样时&#xff0c;往往是先分压&#xff0c;再经过RC低通滤波器。电路图如下&#xff1a; 1&#xff1a;先求输出和输入的关系&#xff08;Uao/Ua&#xff09; 从上式可以看出系统相当于一个典…

二阶有源滤波器设计

1引入 为什么要用有源二阶滤波器&#xff1f; (1)从有源来说 对于无源二阶低通滤波器&#xff1a; 其幅頻方程为&#xff1a; 我们从中可以看出其通带截止频率为 有其品质因子为0.372。 我们根据上图得到二阶无源低通滤波器的品质因子只有0.372&#xff0c;如果希望Q大于0.5…

滤波算法——均值滤波,中值滤波,一阶(αβ)滤波,卡尔曼滤波

滤波算法——均值滤波&#xff0c;中值滤波&#xff0c;一阶(αβ)滤波&#xff0c;卡尔曼滤波 因工作涉及到数据滤波(滤噪)处理&#xff0c;汇总了一些网上简单的滤波算法&#xff0c;方便日后查看。 滤波算法包括&#xff1a;均值滤波&#xff0c;中值滤波&#xff0c;一阶…

一阶滤波器

1. 一阶滤波算法的原理 一阶滤波&#xff0c;又叫一阶惯性滤波&#xff0c;或一阶低通滤波。是使用软件编程实现普通硬件RC低通滤波器的功能。 一阶低通滤波的算法公式为&#xff1a; Y(n)αX(n) (1-α)Y(n-1) 式中&#xff1a;α滤波系数&#xff1b;X(n)本次采样值&…

FIR数字滤波器设计

今天给大侠带来FIR数字滤波器设计&#xff0c;由于篇幅较长&#xff0c;分三篇。今天带来第三篇&#xff0c;FIR数字滤波器设计&#xff0c;包括窗函数法设计FIR滤波器、频率采样法设计FIR滤波器以及基于firls函数和remez函数的最优化方法设计FIR滤波器。话不多说&#xff0c;上…

一阶二阶数字滤波器笔记

数字滤波器 一阶数字滤波器时域分析频域分析数字化代码示例 二阶巴特沃斯低通滤波器S域和Z域的频率关系分析巴特沃斯滤波器举例说明代码示例 声明&#xff1a;感谢知乎大佬的文章&#xff0c;原文链接 数字滤波器实现方法是把滤波器所要完成的运算编成程序并让计算机执行,也就…

简单二阶滤波器截止频率的计算

最近刚好学习到这了&#xff0c;而我在网上查资料的时候却非常难找&#xff0c;不少资料讲解不够详细&#xff0c;所以经过我努力也为了为大家做点贡献的想法&#xff0c;以自己的见解写下这篇文章。废话不多说&#xff0c;先从一阶滤波器讲起。 一阶低通滤波器&#xff1a; …