bind()的概念和使用案例

news/2025/2/23 5:55:11
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="markdown_views prism-atom-one-light"> cap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">

在计算机class="tags" href="/tags/WangLuo.html" title=网络>网络编程中࿰c;<code>bind()code> 是一个用于将一个套接字(socket)与一个特定的class="tags" href="/tags/WangLuo.html" title=网络>网络地址和端口号关联起来的系统调用。这个函数通常在服务器端编程中使用࿰c;用于指定服务器将监听哪个class="tags" href="/tags/WangLuo.html" title=网络>网络接口和端口号上的连接请求。

bind() 的概念

  • 套接字:在计算机class="tags" href="/tags/WangLuo.html" title=网络>网络中࿰c;套接字是通信链路的一个端点࿰c;可以看作是不同计算机进程间通信的一个虚拟端点。
  • class="tags" href="/tags/WangLuo.html" title=网络>网络地址和端口号class="tags" href="/tags/WangLuo.html" title=网络>网络地址用于标识class="tags" href="/tags/WangLuo.html" title=网络>网络中的设备࿰c;端口号用于标识设备上的特定服务或进程。
    <code>bind()code> 函数的原型在 C 语言中定义如下:
<code class="prism language-c">class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><sys/socket.h>
class="token keyword">int class="token function">bindclass="token punctuation">(class="token keyword">int sockfdclass="token punctuation">, class="token keyword">const class="token keyword">struct class="token class-name">sockaddr class="token operator">*addrclass="token punctuation">, class="token class-name">socklen_t addrlenclass="token punctuation">)class="token punctuation">;
code>
  • sockfd:是系统调用 <code>socket()code> 返回的套接字文件描述符。
  • addr:是一个指向 <code>sockaddrcode> 结构体的指针࿰c;该结构体包含了要绑定到套接字的地址信息。
  • addrlen:是 <code>addrcode> 结构体的大小。
    <code>bind()code> 调用成功时返回 0࿰c;失败时返回 -1࿰c;并设置 errno 来指示错误。

使用案例

以下是一个简单的 TCP 服务器端使用 <code>bind()code> 的例子:

<code class="prism language-c">class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><stdio.h>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><stdlib.h>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><string.h>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><sys/socket.h>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><netinet/in.h>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><arpa/inet.h>
class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{
    class="token keyword">int sockfdclass="token punctuation">;
    class="token keyword">struct class="token class-name">sockaddr_in servaddrclass="token punctuation">;
    class="token comment">// 创建套接字
    sockfd class="token function">socketclass="token punctuation">(AF_INETclass="token punctuation">, SOCK_STREAMclass="token punctuation">, class="token number">0class="token punctuation">)class="token punctuation">;
    class="token keyword">if class="token punctuation">(sockfd class="token operator">== class="token operator">-class="token number">1class="token punctuation">) class="token punctuation">{
        class="token function">perrorclass="token punctuation">(class="token string">"socket creation failed"class="token punctuation">)class="token punctuation">;
        class="token function">exitclass="token punctuation">(EXIT_FAILUREclass="token punctuation">)class="token punctuation">;
    class="token punctuation">}
    class="token comment">// 初始化服务器地址结构
    class="token function">memsetclass="token punctuation">(class="token operator">&servaddrclass="token punctuation">, class="token number">0class="token punctuation">, class="token keyword">sizeofclass="token punctuation">(servaddrclass="token punctuation">)class="token punctuation">)class="token punctuation">;
    servaddrclass="token punctuation">.sin_family class="token operator">= AF_INETclass="token punctuation">; class="token comment">// IPv4
    servaddrclass="token punctuation">.sin_addrclass="token punctuation">.s_addr class="token operator">= INADDR_ANYclass="token punctuation">; class="token comment">// 自动获取本地IP地址
    servaddrclass="token punctuation">.sin_port class="token operator">= class="token function">htonsclass="token punctuation">(class="token number">8080class="token punctuation">)class="token punctuation">; class="token comment">// 服务器将监听8080端口
    class="token comment">// 将套接字与服务器地址绑定
    class="token keyword">if class="token punctuation">(class="token function">bindclass="token punctuation">(sockfdclass="token punctuation">, class="token punctuation">(class="token keyword">const class="token keyword">struct class="token class-name">sockaddr class="token operator">*class="token punctuation">)class="token operator">&servaddrclass="token punctuation">, class="token keyword">sizeofclass="token punctuation">(servaddrclass="token punctuation">)class="token punctuation">) class="token operator">< class="token number">0class="token punctuation">) class="token punctuation">{
        class="token function">perrorclass="token punctuation">(class="token string">"bind failed"class="token punctuation">)class="token punctuation">;
        class="token function">exitclass="token punctuation">(EXIT_FAILUREclass="token punctuation">)class="token punctuation">;
    class="token punctuation">}
    class="token comment">// 其他代码࿰c;例如监听、接受连接等...
    class="token comment">// 关闭套接字
    class="token function">closeclass="token punctuation">(sockfdclass="token punctuation">)class="token punctuation">;
    class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

在这个例子中:

  1. 使用 <code>socket()code> 创建了一个 TCP 套接字。
  2. 使用 <code>memset()code> 初始化 <code>sockaddr_incode> 结构体。
  3. 设置 <code>sockaddr_incode> 结构体的各个字段࿰c;包括地址族、IP 地址和端口号。
  4. 调用 <code>bind()code> 将套接字绑定到指定地址和端口。
  5. 如果 <code>bind()code> 调用失败࿰c;程序将打印错误信息并退出。

注意事项

  • 在调用 <code>bind()code> 之前࿰c;必须先创建一个套接字。
  • 如果不调用 <code>bind()code>࿰c;系统会随机分配一个可用的端口号。
  • 对于面向连接的协议(如 TCP)࿰c;<code>bind()code> 是必须的步骤之一。
  • 对于无连接的协议(如 UDP)࿰c;<code>bind()code> 也是可选的࿰c;但通常用于指定服务器监听的端口。

http://www.niftyadmin.cn/n/5863051.html

相关文章

如何设计app测试用例

功能测试 测试方法&#xff1a;等价类划分法、边界值法、场景法、因果图法。优先级设定&#xff1a;核心业务功能设为高优先级。需求覆盖 正向场景、反向场景、关联接口串场景 与后端开发确认测试用例是否全面覆盖后端逻辑。和产品确认用例是否覆盖本次需求&#xff0c;以及是否…

Redisson分布式锁java语法, 可重入性实现原理 ,(还有可重试性,超时不释放,主从一致性)

Redisson在java的使用方法 Redisson分布式锁不可重入的实现原理 设置一个HSET key为锁的名字&#xff0c;field为当前获取锁的线程名字&#xff0c;value为可重入锁的当前已经重入次数 追踪源码发现RedissonClient类的tryLock就是用lua脚本和上图逻辑实现的加锁解锁&#xf…

【JavaWeb12】数据交换与异步请求:JSON与Ajax的绝妙搭配是否塑造了Web的交互革命?

文章目录 &#x1f30d;一. 数据交换--JSON❄️1. JSON介绍❄️2. JSON 快速入门❄️3. JSON 对象和字符串对象转换❄️4. JSON 在 java 中使用❄️5. 代码演示 &#x1f30d;二. 异步请求--Ajax❄️1. 基本介绍❄️2. JavaScript 原生 Ajax 请求❄️3. JQuery 的 Ajax 请求 &a…

日志管理利器:基于 ELK 的日志收集、存储与可视化实战

使用 Logstash、Elasticsearch 和 Kibana&#xff08;通常称为 ELK Stack&#xff09;可以快速搭建一个强大的日志收集、存储和可视化平台。以下是使用蓝易云搭建日志平台的步骤&#xff1a; 1. 环境准备 确保你有一台云服务器&#xff08;如蓝易云提供的服务器&#xff09;&a…

蓝桥杯——PWM波输出与捕获

pwm输出 要求如下&#xff0c;按要求去配置引脚。 ccr输出比较寄存器&#xff0c;占空比&#xff0c;一段时间内高电平所占比列 引脚功能设置为TIM2-CH2,ch2就是定时器的通道二&#xff0c;根据上面的公式计算给出f1000&#xff0c;时ARR与PSC的值&#xff0c;系统频率f为80…

基于 DeepSeek LLM 本地知识库搭建开源方案(AnythingLLM、Cherry、Ragflow、Dify)认知

写在前面 博文内容涉及 基于 Deepseek LLM 的本地知识库搭建使用 ollama 部署 Deepseek-R1 LLM知识库能力通过 Ragflow、Dify 、AnythingLLM、Cherry 提供理解不足小伙伴帮忙指正 &#x1f603;,生活加油 我站在人潮中央&#xff0c;思考这日日重复的生活。我突然想&#xff0c…

区块链相关方法-SWOT分析

1.SWOT 一、定义:一种基于内外部竞争环境和竞争条件下的态势分析&#xff0c;通过对企业的内外环境所形成的优势&#xff08;Strengths&#xff09;、劣势&#xff08;Weaknesses&#xff09;、机会&#xff08;Opportunities&#xff09;和威胁&#xff08;Threats&#xff0…

蓝禾,oppo,游卡,汤臣倍健,康冠科技,作业帮,高途教育25届春招内推

蓝禾&#xff0c;oppo&#xff0c;游卡&#xff0c;汤臣倍健&#xff0c;康冠科技&#xff0c;作业帮&#xff0c;高途教育25届春招内推 ①康冠科技 【职位】算法、软件、硬件、技术&#xff0c;结构设计&#xff0c;供应链&#xff0c;产品&#xff0c;职能&#xff0c;商务 【…