Boost 网络库

2024-06-19 02:52
文章标签 网络 boost

本文主要是介绍Boost 网络库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

asio

  • 网络编程的基本流程
  • 创建 socket
  • 绑定acceptor
  • 连接指定的端点
  • 服务器接受连接

网络编程的基本流程

  • 服务端

1)socket----创建socket对象。

2)bind----绑定本机ip+port。

3)listen----监听来电,若在监听到来电,则建立起连接。

4)accept----再创建一个socket对象给其收发消息。原因是现实中服务端都是面对多个客户端,那么为了区分各个客户端,则每个客户端都需再分配一个socket对象进行收发消息。

5)read、write----收发消息

  • 客户端

1)socket----创建socket对象。

2)connect----根据服务端ip+port,发起连接请求。

3)write、read----建立连接后,就可发收消息了

创建 socket

创建socket分为4步,创建上下文iocontext,选择协议,生成socket,打开socket。

int create_tcp_socket() {// Step 1. An instance of 'io_service' class is required by// socket constructor. asio::io_context  ios;// Step 2. Creating an object of 'tcp' class representing// a TCP protocol with IPv4 as underlying protocol.asio::ip::tcp protocol = asio::ip::tcp::v4();// Step 3. Instantiating an active TCP socket object.asio::ip::tcp::socket sock(ios);// Used to store information about error that happens// while opening the socket.boost::system::error_code ec;// Step 4. Opening the socket.sock.open(protocol, ec);if (ec.value() != 0) {// Failed to open the socket.std::cout<< "Failed to open the socket! Error code = "<< ec.value() << ". Message: " << ec.message();return ec.value();}return 0;
}

上述socket只是通信的socket,如果是服务端,我们还需要生成一个acceptor的socket,用来接收新的连接。

int  create_acceptor_socket() {// Step 1. An instance of 'io_service' class is required by// socket constructor. asio::io_context ios;// Step 2. Creating an object of 'tcp' class representing// a TCP protocol with IPv6 as underlying protocol.asio::ip::tcp protocol = asio::ip::tcp::v6();// Step 3. Instantiating an acceptor socket object.asio::ip::tcp::acceptor acceptor(ios);// Used to store information about error that happens// while opening the acceptor socket.boost::system::error_code ec;// Step 4. Opening the acceptor socket.acceptor.open(protocol, ec);if (ec.value() != 0) {// Failed to open the socket.std::cout<< "Failed to open the acceptor socket!"<< "Error code = "<< ec.value() << ". Message: " << ec.message();return ec.value();}return 0;
}

绑定acceptor

对于acceptor类型的socket,服务器要将其绑定到指定的端口,所有连接这个端口的连接都可以被接收到。

int  bind_acceptor_socket() {// Step 1. Here we assume that the server application has// already obtained the protocol port number.unsigned short port_num = 3333;// Step 2. Creating an endpoint.asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(),port_num);// Used by 'acceptor' class constructor.asio::io_context  ios;// Step 3. Creating and opening an acceptor socket.asio::ip::tcp::acceptor acceptor(ios, ep.protocol());boost::system::error_code ec;// Step 4. Binding the acceptor socket.acceptor.bind(ep, ec);// Handling errors if any.if (ec.value() != 0) {// Failed to bind the acceptor socket. Breaking// execution.std::cout << "Failed to bind the acceptor socket."<< "Error code = " << ec.value() << ". Message: "<< ec.message();return ec.value();}return 0;
}

连接指定的端点

作为客户端可以连接服务器指定的端点进行连接

int  connect_to_end() {// Step 1. Assume that the client application has already// obtained the IP address and protocol port number of the// target server.std::string raw_ip_address = "127.0.0.1";unsigned short port_num = 3333;try {// Step 2. Creating an endpoint designating // a target server application.asio::ip::tcp::endpointep(asio::ip::address::from_string(raw_ip_address),port_num);asio::io_context ios;// Step 3. Creating and opening a socket.asio::ip::tcp::socket sock(ios, ep.protocol());// Step 4. Connecting a socket.sock.connect(ep);// At this point socket 'sock' is connected to // the server application and can be used// to send data to or receive data from it.}// Overloads of asio::ip::address::from_string() and // asio::ip::tcp::socket::connect() used here throw// exceptions in case of error condition.catch (system::system_error& e) {std::cout << "Error occured! Error code = " << e.code()<< ". Message: " << e.what();return e.code().value();}
}

服务器接受连接

int accept_new_connection(){// The size of the queue containing the pending connection// requests.const int BACKLOG_SIZE = 30;// Step 1. Here we assume that the server application has// already obtained the protocol port number.unsigned short port_num = 3333;// Step 2. Creating a server endpoint.asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(),port_num);asio::io_context  ios;try {// Step 3. Instantiating and opening an acceptor socket.asio::ip::tcp::acceptor acceptor(ios, ep.protocol());// Step 4. Binding the acceptor socket to the // server endpint.acceptor.bind(ep);// Step 5. Starting to listen for incoming connection// requests.acceptor.listen(BACKLOG_SIZE);// Step 6. Creating an active socket.asio::ip::tcp::socket sock(ios);// Step 7. Processing the next connection request and // connecting the active socket to the client.acceptor.accept(sock);// At this point 'sock' socket is connected to //the client application and can be used to send data to// or receive data from it.}catch (system::system_error& e) {std::cout << "Error occured! Error code = " << e.code()<< ". Message: " << e.what();return e.code().value();}
}

这篇关于Boost 网络库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073811

相关文章

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SSID究竟是什么? WiFi网络名称及工作方式解析

《SSID究竟是什么?WiFi网络名称及工作方式解析》SID可以看作是无线网络的名称,类似于有线网络中的网络名称或者路由器的名称,在无线网络中,设备通过SSID来识别和连接到特定的无线网络... 当提到 Wi-Fi 网络时,就避不开「SSID」这个术语。简单来说,SSID 就是 Wi-Fi 网络的名称。比如

Java实现任务管理器性能网络监控数据的方法详解

《Java实现任务管理器性能网络监控数据的方法详解》在现代操作系统中,任务管理器是一个非常重要的工具,用于监控和管理计算机的运行状态,包括CPU使用率、内存占用等,对于开发者和系统管理员来说,了解这些... 目录引言一、背景知识二、准备工作1. Maven依赖2. Gradle依赖三、代码实现四、代码详解五

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

ASIO网络调试助手之一:简介

多年前,写过几篇《Boost.Asio C++网络编程》的学习文章,一直没机会实践。最近项目中用到了Asio,于是抽空写了个网络调试助手。 开发环境: Win10 Qt5.12.6 + Asio(standalone) + spdlog 支持协议: UDP + TCP Client + TCP Server 独立的Asio(http://www.think-async.com)只包含了头文件,不依

poj 3181 网络流,建图。

题意: 农夫约翰为他的牛准备了F种食物和D种饮料。 每头牛都有各自喜欢的食物和饮料,而每种食物和饮料都只能分配给一头牛。 问最多能有多少头牛可以同时得到喜欢的食物和饮料。 解析: 由于要同时得到喜欢的食物和饮料,所以网络流建图的时候要把牛拆点了。 如下建图: s -> 食物 -> 牛1 -> 牛2 -> 饮料 -> t 所以分配一下点: s  =  0, 牛1= 1~

poj 3068 有流量限制的最小费用网络流

题意: m条有向边连接了n个仓库,每条边都有一定费用。 将两种危险品从0运到n-1,除了起点和终点外,危险品不能放在一起,也不能走相同的路径。 求最小的费用是多少。 解析: 抽象出一个源点s一个汇点t,源点与0相连,费用为0,容量为2。 汇点与n - 1相连,费用为0,容量为2。 每条边之间也相连,费用为每条边的费用,容量为1。 建图完毕之后,求一条流量为2的最小费用流就行了

poj 2112 网络流+二分

题意: k台挤奶机,c头牛,每台挤奶机可以挤m头牛。 现在给出每只牛到挤奶机的距离矩阵,求最小化牛的最大路程。 解析: 最大值最小化,最小值最大化,用二分来做。 先求出两点之间的最短距离。 然后二分匹配牛到挤奶机的最大路程,匹配中的判断是在这个最大路程下,是否牛的数量达到c只。 如何求牛的数量呢,用网络流来做。 从源点到牛引一条容量为1的边,然后挤奶机到汇点引一条容量为m的边

配置InfiniBand (IB) 和 RDMA over Converged Ethernet (RoCE) 网络

配置InfiniBand (IB) 和 RDMA over Converged Ethernet (RoCE) 网络 服务器端配置 在服务器端,你需要确保安装了必要的驱动程序和软件包,并且正确配置了网络接口。 安装 OFED 首先,安装 Open Fabrics Enterprise Distribution (OFED),它包含了 InfiniBand 所需的驱动程序和库。 sudo

【机器学习】高斯网络的基本概念和应用领域

引言 高斯网络(Gaussian Network)通常指的是一个概率图模型,其中所有的随机变量(或节点)都遵循高斯分布 文章目录 引言一、高斯网络(Gaussian Network)1.1 高斯过程(Gaussian Process)1.2 高斯混合模型(Gaussian Mixture Model)1.3 应用1.4 总结 二、高斯网络的应用2.1 机器学习2.2 统计学2.3