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编写 ...
随机推荐
- 路径规划之 A* 算法
算法介绍 A*(念做:A Star)算法是一种很常用的路径查找和图形遍历算法.它有较好的性能和准确度.本文在讲解算法的同时也会提供Python语言的代码实现,并会借助matplotlib库动态的展示算 ...
- python使用sql批量插入数据+查看执行的语句+动态sql创建表+动态创建索引
class Test(): cursor = connection.cursor() data_to_insert = [] sql = "INSERT INTO test_t (id, n ...
- java bean和String之间相互转化
开发中有的表字段特别多,在数据传递过程中要写很多类似实体类的get.set方法把字符串型的数据放到对象里然后,在做存储之类的操作,如果实体的字段少不会觉得多麻烦,但是字段如果有几十个或者更多那么这种简 ...
- mybatis-plus-QueryWrapper 如何写or效果的语句 以及如何给or加括号
先说想要的结果 希望mybatis-plus中QueryWrapper写法生成的sql语句中查询条件是 WHERE (( (LOGIN_ID = ? OR SHI_JI_LOGIN_ID = ?) ) ...
- linux驱动开发中copy_from_user open read write等常用函数总结
目录 open read write copy_to_user copy_from_user open 函数定义: int open( const char * pathname, int flags ...
- Contest3376 - 2024寒假集训-排位赛竞赛(一)
A: 幂位和 高精度. 用高精度加法或乘法算出\(2^{1000}\),再将各位累加即为答案. #include <bits/stdc++.h> using namespace std; ...
- Introduction to DFT
服务器使用 登陆服务器:输入账号密码 打开terminal,保证至少一个terminal窗口是打开的 取消Linux操作系统的屏幕保护 设置Linux EDA工具配置 // 自定义环境变量设置 gvi ...
- [SpringMVC] - 解决Jackson中文乱码 : springmvc-servlet.xml
<!-- 指定响应体返回类型和编码 , 解决乱码????的问题 --> <mvc:annotation-driven> <mvc:message-converters r ...
- 让vs自动提示没有using的类
默认情况下,没有using的类,敲代码时没有智能提示,需要在[工具]->[选项]中开启
- 百度网盘(百度云)SVIP超级会员共享账号每日更新(2023.11.30)
一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...