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流数据的更多相关文章

  1. boost::asio::ip::tcp实现网络通信的小例子

    同步方式: Boost.Asio是一个跨平台的网络及底层IO的C++编程库,它使用现代C++手法实现了统一的异步调用模型. 头文件 #include <boost/asio.hpp> 名空 ...

  2. boost::asio::ip::tcp中几个重要类型

    typedef basic_stream_socket socket; 流式套接字,提供同/异步发送接收数据,连接,绑定,设置套接字选项等功能 对于socket中的connect()方法,它只针对某一 ...

  3. boost::asio::socket tcp 连接 在程序结束时崩溃。

    刚开始的时候一直不知道怎么回事,不过幸好我有在每个class 的析构时都打印一条信息. 这个时候发现我的一个tcp_connection (就是自定义的一个连接类) 在最后才被析构. 所以感觉这里可能 ...

  4. boost asio 学习(八) 网络基础 二进制写发送和接收

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=9 8. Net ...

  5. boost asio 异步实现tcp通讯

    ---恢复内容开始--- asioboost   目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化   一.前言 boost asio可算是一个简 ...

  6. boost asio tcp 多线程

    common/pools.h // common/pools.h #pragma once #include <string> #include <boost/pool/pool.h ...

  7. boost asio tcp 多线程异步读写,服务器与客户端。

    // server.cpp #if 0 多个线程对同一个io_service 对象处理 用到第三方库:log4cplus, google::protobuf 用到C++11的特性,Windows 需要 ...

  8. boost asio tcp server 拆分

    从官方给出的示例中对于 boost::asio::ip::tcp::acceptor 类的使用,是直接使用构造函数进行构造对象,这一种方法用来学习是一个不错的方式. 但是要用它来做项目却是不能够满足我 ...

  9. boost asio 学习(七) 网络基础 连接器和接收器(TCP示例)

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=8 7. Net ...

  10. Boost.Asio基本原理(CSDN也有Markdown了,好开森)

    Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情.我们也将深入研究比同步编程更复杂.更有乐趣的异步编程. 网络API 这一部分包含了当使用Boost.Asio编写 ...

随机推荐

  1. AliSSR 语音超分算法:让在线会议语音更明亮更自然

    超分让在线会议语音更明亮,在线会议已成为日常工作中较为普遍的沟通交流方式,接入会议的方式也呈现多样化,比如电脑入会.手机入会又或是电话入会. 雪雅.曜辰|作者 众所周知,高采样率且高带宽的音频信号富含 ...

  2. Linux CentOS 7 离线安装.NET环境

    下载 下载.NET 例如: aspnetcore-runtime-6.0.15-linux-x64.tar.gz 复制 复制到如下目录: /usr/local/dotnet/aspnetcore-ru ...

  3. Redis 缓存性能实践及总结

    一.前言 在互联网应用中,缓存成为高并发架构的关键组件.这篇博客主要介绍缓存使用的典型场景.实操案例分析.Redis使用规范及常规 Redis 监控. 二.常见缓存对比 常见的缓存方案,有本地缓存,包 ...

  4. 为什么很多候选人投出去的简历石沉大海(面向Java方向)

    我最近在帮上海某培训学校里的毕业生做面试辅导,普遍发现很多候选人不是没能力,或者说能力没有差到没有面试机会的程度,但这些同学投出去的简历大多石沉大海,即使有回应,也大多是些外包外派公司或者小公司. 而 ...

  5. <vue 基础知识 3、v-bind使用>

    代码结构 一.     v-bind基本使用 1.效果 2.代码 01-v-bind基本使用.html <!DOCTYPE html> <html lang="en&quo ...

  6. S3C2440移植uboot之新建单板_时钟_SDRAM_串口

    上一节S3C2440移植uboot之启动过程概述我们我们分析了uboot启动流程,这节将开始新建一块单板支持S3C2440. 目录 1.新建单板 1.1 将2410的单板文件夹拷贝成2440: 1.2 ...

  7. windows10/liunx创建空大文件

    1.windows10创建空大文件打开cmd命令,进入需要创建文件的目录,使用以下命令创建 fsutil file createnew test001.txt 1073741824 最后的数字代表文件 ...

  8. [转帖]Docker最佳实践:5个方法精简镜像

    https://juejin.cn/post/6844903880526921741   精简Docker镜像的好处很多,不仅可以节省存储空间和带宽,还能减少安全隐患.优化镜像大小的手段多种多样,因服 ...

  9. [转帖]Linux命令之——rsync

    文章目录 1 rsync是干什么用的 2 rsync和scp有什么区别 3 rsync简单用法介绍 rsync四种工作方式 1. 本地文件系统上实现同步 2. 本地主机使用远程shell和远程主机通信 ...

  10. [转帖]021系统状态检测命令sosreport

    https://www.cnblogs.com/anyoneofus/p/16467677.html   sosreport命令用于收集系统配置及架构信息并输出诊断文档.