


boost::asio Programming - Synchronous TCP
boost.asio library is a cross-platform network and underlying IO C++ programming library. It uses modern C++ techniques to implement a unified asynchronous calling model.
boost.asio library supports TCP, UDP, and ICMP communication protocols.
The following introduces the synchronous TCP mode:
Hello everyone! I am in sync mode!
My main characteristic is persistence! All operations must be completed or make an error before returning. However, my persistence is called blocking by everyone, which is really depressing~~ (The audience booed), in fact, this is
There are also benefits, such as clear logic and easier programming.
On the server side, I will make a socket and give it to the acceptor object, and let it wait for the client to connect. After connecting, I will communicate with the client through this socket.
All communications are performed in a blocking manner and will not return until reading or writing is completed.
The same goes for the client. At this time, I will use the socket to connect to the server. Of course, I will only return when the connection is connected or an error occurs. In the end, I will communicate with the server in a blocking manner.
Some people think that the synchronous method is not as efficient as the asynchronous method. In fact, this is a one-sided understanding. This may be true in a single-threaded case, where I can't use the time spent on network operations to do anything else
Emotion is not a good coordination method. However, this problem can be avoided through multi-threading. For example, on the server side, one of the threads is responsible for waiting for the client to connect, and after the connection comes in, the socket is handed over to another thread.
Communicate with the client, so that while communicating with one client, you can also accept connections from other clients, and the main thread is completely freed.
That’s my introduction, thank you everyone!
Synchronization mode sample code:
Server side
// BoostTcpServer.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "boost/asio.hpp" #include "boost/thread.hpp" using namespace std; using namespace boost::asio; #ifdef _MSC_VER #define _WIN32_WINNT 0X0501 //避免VC下编译警告 #endif #define PORT 1000 #define IPV6 //#define IPV4 int _tmain(int argc, _TCHAR* argv[]) { // 所有asio类都需要io_service对象 io_service iosev; //创建用于接收客户端连接的acceptor对象 #ifdef IPV4 ip::tcp::acceptor acceptor(iosev,ip::tcp::endpoint(ip::tcp::v4(), PORT)); #endif #ifdef IPV6 ip::tcp::acceptor acceptor(iosev,ip::tcp::endpoint(ip::tcp::v6(), PORT)); #endif while (true) { // socket对象 ip::tcp::socket socket(iosev); // 等待直到客户端连接进来 acceptor.accept(socket); // 显示连接进来的客户端 std::cout <<"remote ip:"<<socket.remote_endpoint().address()<<endl; std::cout <<"remote port:"<<socket.remote_endpoint().port() << std::endl; char buf[2048]; boost::system::error_code ec; while(1) { socket.read_some(buffer(buf),ec); if (ec) { std::cout <<boost::system::system_error(ec).what() << std::endl; break ; } std::cout<<"recv msg:"<<buf<<endl; if(strcmp(buf,"bye")==0)//收到结束消息结束客户端连接 { break; } socket.write_some(buffer("I heared you!\n"),ec); if (ec) { std::cout <<boost::system::system_error(ec).what() << std::endl; break ; } } socket.close(); // 与当前客户交互完成后循环继续等待下一客户连接 } return 0; }
Client side
// BoostTcpClient.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "boost/asio.hpp" using namespace boost::asio; #ifdef _MSC_VER #define _WIN32_WINNT 0X0501 //避免VC下编译警告 #endif #define PORT 1000 #define IPV6 //#define IPV4 int _tmain(int argc, _TCHAR* argv[]) { // 所有asio类都需要io_service对象 io_service iosev; // socket对象 ip::tcp::socket socket(iosev); // 连接端点,这里使用了本机连接,可以修改IP地址测试远程连接 #ifdef IPV4 ip::address_v4 address=ip::address_v4::from_string("127.0.0.1"); #endif #ifdef IPV6 //"0:0:0:0:0:0:0:1"为IPV6的本机回环地址,类似于"127.0.0.1" ip::address_v6 address=ip::address_v6::from_string("0:0:0:0:0:0:0:1"); #endif ip::tcp::endpoint ep(address, PORT); // 连接服务器 boost::system::error_code ec; socket.connect(ep,ec); // 如果出错,打印出错信息 if (ec) { std::cout << boost::system::system_error(ec).what() << std::endl; return -1; } //循环发送和接收数据 for(int i=0;i<5;++i) { //发送数据 socket.write_some(buffer("hello"), ec); // 接收数据 char buf[100]; size_t len=socket.read_some(buffer(buf), ec); std::cout.write(buf, len); Sleep(500); } //发送与服务端约定好的结束语,由服务端断链 socket.write_some(buffer("bye"), ec); getchar(); return 0; }
The code is compatible with both IPV4 and IPV6 IP protocols. Use macro definition to choose which IP protocol to use. Of course, the client The protocol with the server must be consistent for normal communication.
Thanks for reading, I hope it can help everyone. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!

计算机编程中常见的if语句是条件判断语句。if语句是一种选择分支结构,它是依据明确的条件选择选择执行路径,而不是严格按照顺序执行,在编程实际运用中要根据程序流程选择适合的分支语句,它是依照条件的结果改变执行的程序;if语句的简单语法“if(条件表达式){// 要执行的代码;}”。

前言本文继续来介绍Python集合模块,这次主要简明扼要的介绍其内的命名元组,即namedtuple的使用。闲话少叙,我们开始——记得点赞、关注和转发哦~ ^_^创建命名元组Python集合中的命名元组类namedTuples为元组中的每个位置赋予意义,并增强代码的可读性和描述性。它们可以在任何使用常规元组的地方使用,且增加了通过名称而不是位置索引方式访问字段的能力。其来自Python内置模块collections。其使用的常规语法方式为:import collections XxNamedT

作为一门高效的编程语言,Go在图像处理领域也有着不错的表现。虽然Go本身的标准库中没有提供专门的图像处理相关的API,但是有一些优秀的第三方库可以供我们使用,比如GoCV、ImageMagick和GraphicsMagick等。本文将重点介绍使用GoCV进行图像处理的方法。GoCV是一个高度依赖于OpenCV的Go语言绑定库,其

最近,PHP8.0发布了一个新的邮件库,使得在PHP中发送和接收电子邮件变得更加容易。这个库具有强大的功能,包括构建电子邮件,发送电子邮件,解析电子邮件,获取附件和解决电子邮件获得卡住的问题。在很多项目中,我们都需要使用电子邮件来进行通信和一些必备的业务操作。而PHP8.0中的邮件库可以让我们轻松地实现这一点。接下来,我们将探索这个新的邮件库,并了解如何在我

随着PHP8.0的发布,DOMDocument作为PHP内置的XML解析库,也有了新的变化和增强。DOMDocument在PHP中的重要性不言而喻,尤其在处理XML文档方面,它的功能十分强大,而且使用起来也十分简单。本文将介绍PHP8.0中DOMDocument的新特性和应用。一、DOMDocument概述DOM(DocumentObjectModel)

Python 中的 main 函数充当程序的执行点,在 Python 编程中定义 main 函数是启动程序执行的必要条件,不过它仅在程序直接运行时才执行,而在作为模块导入时不会执行。要了解有关 Python main 函数的更多信息,我们将从如下几点逐步学习:什么是 Python 函数Python 中 main 函数的功能是什么一个基本的 Python main() 是怎样的Python 执行模式Let’s get started什么是 Python 函数相信很多小伙伴对函数都不陌生了,函数是可

PHP8.0是PHP语言的最新版本,自发布以来已经引发了广泛的关注和争议。其中,最引人瞩目的新特性之一就是Symbol类型。Symbol类型是PHP8.0中新增的一种数据类型,它类似于JavaScript中的Symbol类型,可用于表示独一无二的值。这意味着,两个Symbol类型的值即使完全相同,它们也是不相等的。Symbol类型的使用可以避免在不同的代码段

两年多前,Adobe 发布了一则引人关注的公告 —— 将在 2020 年 12 月 31 日终止支持 Flash,宣告了一个时代的结束。一晃两年过去了,Adobe 早已从官方网站中删除了 Flash Player 早期版本的所有存档,并阻止基于 Flash 的内容运行。微软也已经终止对 Adobe Flash Player 的支持,并禁止其在任何 Microsoft 浏览器上运行。Adobe Flash Player 组件于 2021 年 7 月通过 Windows 更新永久删除。当 Flash


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
