19.13 Boost Asio 发送TCP流数据
Boost框架中默认就提供了针对TCP流传输的支持,该功能可以用来进行基于文本协议的通信,也可以用来实现自定义的协议。一般tcp::iostream会阻塞当前线程,直到IO操作完成。
首先来看服务端代码,如下所示在代码中首先通过GetFileSize读取文件行数,当有了行数我们就可以使用循环的方式依次调用acceptor.accept(*tcp_stream.rdbuf())接收客户端的相应请求,并使用<<符号向建立了链接的文件内追加字符串数据。
#include <iostream>
#include <fstream>
#include <boost/asio.hpp>
using namespace std;
using namespace boost;
using namespace boost::asio;
// 利用流获取文件大小
long GetFileSize(std::string filename)
{
long ref_kb;
std::ifstream ptr(filename, std::ios::in | std::ios::binary);
if (ptr.is_open() == true)
{
ptr.seekg(0, std::ios::end); // 移动到末尾
ref_kb = ptr.tellg(); // 获取字节数
ptr.close();
return ref_kb;
}
return 0;
}
// 一次性读入,并循环输出
void ReadAllFile(std::string filename)
{
char *buffer;
long size;
std::ifstream ptr(filename, std::ios::in | std::ios::binary | std::ios::ate);
size = ptr.tellg();
std::cout << "总大小: " << size << std::endl;
ptr.seekg(0, std::ios::beg);
buffer = new char[size];
ptr.read(buffer, size);
ptr.close();
// 循环输出逐字节输出
for (int x = 0; x < size; x++)
{
if (buffer[x] != '\0')
{
std::cout << buffer[x];
}
}
delete[] buffer;
}
// 每次读入一行,并输出
void ReadLineFileA(std::string filename)
{
std::ifstream ptr(filename);
std::string string;
while (std::getline(ptr, string))
{
std::cout << string.c_str() << std::endl;
}
}
void ReadLineFileB(std::string filename)
{
char buffer[1024];
std::fstream ptr;
ptr.open(filename, std::ios::in | std::ios::binary);
if (ptr.is_open() == true)
{
while (!ptr.eof())
{
// 该行长度到达1024或者遇到\n则结束
ptr.getline(buffer, 1024, '\n');
std::cout << buffer << std::endl;
}
}
}
// 获取文本总行数
int GetFileLine(std::string filename)
{
char buffer[1024];
std::fstream ptr;
int line_count = 0;
ptr.open(filename, std::ios::in | std::ios::binary);
if (ptr.is_open() == true)
{
while (!ptr.eof())
{
ptr.getline(buffer, 1024, '\n');
line_count = line_count + 1;
}
}
return line_count;
}
int main(int argc, char *argv[])
{
std::string file_path = "d://lyshark.txt";
// 获取行号
int count = GetFileLine(file_path);
std::cout << "行数: " << count << std::endl;
// 发送数据流
io_service io;
ip::tcp::endpoint ep(ip::tcp::v4(), 6666);
ip::tcp::acceptor acceptor(io, ep);
std::ifstream ptr(file_path);
std::string get_string;
while (std::getline(ptr, get_string))
{
ip::tcp::iostream tcp_stream;
acceptor.accept(*tcp_stream.rdbuf());
tcp_stream << get_string.c_str();
}
std::system("pause");
return 0;
}
与服务端相比,客户端的代码则显得非常简单,在代码中我们只需要通过ip::tcp::iostream tcp_stream链接到服务端,并通过调用getline即可每次在流中获取一行数据,由于我们循环了3次,所有也就是只读取前三行。
#include <iostream>
#include <boost/asio.hpp>
using namespace std;
using namespace boost::asio;
using namespace boost::system;
int main(int argc, char *argv[])
{
// 循环从流中读入,前三行
for (int i = 0; i < 3; ++i)
{
ip::tcp::iostream tcp_stream("127.0.0.1", "6666");
string str;
getline(tcp_stream, str);
cout << str << endl;
}
std::system("pause");
return 0;
}
读者可自行编译并运行上述代码片段,则可看到如下图所示的输出信息;

19.13 Boost Asio 发送TCP流数据的更多相关文章
- boost::asio::ip::tcp实现网络通信的小例子
同步方式: Boost.Asio是一个跨平台的网络及底层IO的C++编程库,它使用现代C++手法实现了统一的异步调用模型. 头文件 #include <boost/asio.hpp> 名空 ...
- boost::asio::ip::tcp中几个重要类型
typedef basic_stream_socket socket; 流式套接字,提供同/异步发送接收数据,连接,绑定,设置套接字选项等功能 对于socket中的connect()方法,它只针对某一 ...
- boost::asio::socket tcp 连接 在程序结束时崩溃。
刚开始的时候一直不知道怎么回事,不过幸好我有在每个class 的析构时都打印一条信息. 这个时候发现我的一个tcp_connection (就是自定义的一个连接类) 在最后才被析构. 所以感觉这里可能 ...
- boost asio 学习(八) 网络基础 二进制写发送和接收
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=9 8. Net ...
- boost asio 异步实现tcp通讯
---恢复内容开始--- asioboost 目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化 一.前言 boost asio可算是一个简 ...
- boost asio tcp 多线程
common/pools.h // common/pools.h #pragma once #include <string> #include <boost/pool/pool.h ...
- boost asio tcp 多线程异步读写,服务器与客户端。
// server.cpp #if 0 多个线程对同一个io_service 对象处理 用到第三方库:log4cplus, google::protobuf 用到C++11的特性,Windows 需要 ...
- boost asio tcp server 拆分
从官方给出的示例中对于 boost::asio::ip::tcp::acceptor 类的使用,是直接使用构造函数进行构造对象,这一种方法用来学习是一个不错的方式. 但是要用它来做项目却是不能够满足我 ...
- boost asio 学习(七) 网络基础 连接器和接收器(TCP示例)
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=8 7. Net ...
- Boost.Asio基本原理(CSDN也有Markdown了,好开森)
Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情.我们也将深入研究比同步编程更复杂.更有乐趣的异步编程. 网络API 这一部分包含了当使用Boost.Asio编写 ...
随机推荐
- Tomcat--启动慢
本篇为转载文章 来自:https://www.cnblogs.com/user-sunli/articles/13917035.html 有时启动Tomcat,发现启动很慢,需要几分钟,这个问题值得重 ...
- 【HZERO】报表服务
报表服务 参考文档 https://open.hand-china.com/document-center/doc/component/181/12455?doc_id=10088&_back ...
- LaTex常用数学符号整理
在论文和博客的写作中,经常会用到Latex的语法来书写数学公式,一份详细的数学符号对照表必不可少,本文重写了部分 Markdown 公式指导手册 . 在线Latex公式编辑器 -1.求和积分的上下标位 ...
- CH6803 导弹防御塔 (二分 + 匈牙利 / 网络流)
链接:https://ac.nowcoder.com/acm/contest/1062/D 题目描述 Freda的城堡-- "Freda,城堡外发现了一些入侵者!" "喵 ...
- 探究Presto SQL引擎(2)-浅析Join
作者:vivo互联网技术-Shuai Guangying 在<探究Presto SQL引擎(1)-巧用Antlr>中,我们介绍了Antlr的基本用法以及如何使用Antlr4实现解析SQL查 ...
- springboot启动类源码探索一波
举个例子: 这是一个原始的Spring IOC容器启动方法,我们需要AnnotationConfigApplicationContext这个类有如下几个步骤 1. 创建构造方法,根据我们所传入的Ap ...
- 08.25北京站|阿里云Serverless 技术实践营( AI 专场)开放报名
活动简介 阿里云 Serverless 技术实践营(AI 专场)是一场以聚焦企业级 AIGC 应用开发与落地展开的主题活动,活动受众以关注 Serverless 和 AI 技术的开发者.企业决策人.云 ...
- shell脚本(8)-流程控制if
一.单if语法 1.语法格式: if [ condition ] #condition值为 then commands fi 2.举例: [root@localhost test20210725]# ...
- Python注释是什么东东
注释 标注解释,目的是帮助读者理解的文本 也就是说,注释首先是文本,其二是说明,其三是思路,其四是例子注释有两种形式 1. # ... 单行注释 用于对某句语句或语句块进行解释 放在语句块的头行或一个 ...
- [转帖]Jmeter中如何读取MYSQL数据作为请求参数
在项目测试过程中,我经常需要将数据库中的数据作为参数传递到请求中.Jmeter中MYSQL数据库连接操作过程如下: 1.下载/n导入mysql的jdbc驱动包 下载mysql驱动包地址: http:/ ...