19.13 Boost Asio 发送TCP流数据

news/2024/5/17 15:51:07 标签: tcp/ip, 网络协议, 网络, Boost, Visual C++, TCP, 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;
}

读者可自行编译并运行上述代码片段,则可看到如下图所示的输出信息;


http://www.niftyadmin.cn/n/5162149.html

相关文章

Flink集群的搭建

1、Flink独立集群模式 1、首先Flink的独立集群模式是不依赖于Hadoop集群。 2、上传压缩包&#xff0c;配置环境&#xff1a; 1、解压&#xff1a; tar -zxvf flink-1.15.2-bin-scala_2.12.tgz2、配置环境变量&#xff1a;vim /etc/profileexport FLINK_HOME/usr/local/soft/fl…

联表查询(一对多 多对一)

一对多情况处理流程&#xff1a; 在user类中引入dept类Data public class User {private Long id;private String username&#xff1b;private String password;private String phone;private String info;private Integer status&#xff1b;private Integer balance;privat…

11-2 mybatis入门细节

mybatis Mybatis 单表CURD细节 ${} 与#{} 区别(面试题) ${} 拼接sql 造成sql注入 #{} 使用?占位 如果作为值, 推荐使用#{} ${} 实现一些动态排序,使用 #{column} select * from tb_userinfo order by ? desc column: id 赋值 sql: select * from tb_userinfo order by id …

Flink 基础 -- 应用开发(项目配置)

1、概述 本节中的指南将向您展示如何通过流行的构建工具(Maven, Gradle)配置项目&#xff0c;添加必要的依赖项(即连接器和格式&#xff0c;测试)&#xff0c;并涵盖一些高级配置主题。 每个Flink应用程序都依赖于一组Flink库。至少&#xff0c;应用程序依赖于Flink api&…

PHP将pdf转为图片后用OCR识别

1.确保apt包是最新 sudo apt update 2.使用apt安装 sudo apt install tesseract-ocr 3.检查版本 tesseract --version 4.pdf转成图片&#xff0c;这边需要安装imagick插件 $pdf new Imagick(); $pdf->setResolution(150, 150); $pdf->readImage(..$temp); $pdf->…

Mybatis-Plus同时使用逻辑删除和唯一索引的问题及解决办法

1 问题背景 在开发中&#xff0c;我们经常会有逻辑删除和唯一索引同时使用的情况。但当使用mybatis plus时&#xff0c;如果同时使用逻辑删除和唯一索引&#xff0c;会报数据重复Duplicate entry的问题。 举例来说&#xff0c;有表user&#xff0c;建立唯一索引&#xff08;u…

分布式数据库·Hive和MySQL的安装与配置

一、版本要求&#xff1a;Hadoop:hadoop-2.10.1、MySQL&#xff1a;mysql-8.0.35、 HIVE&#xff1a;apache-hive-3.1.2、MySQL驱动&#xff1a;mysql-connector-java-5.1.49 安装包网盘链接&#xff1a;阿里云盘分享 安装位置 Hive:master、MySQL:slave1 二、卸载已安装的…

腾讯待办关停,导出的数据怎么恢复到手机上面?

相信有不少腾讯待办的用户都发现了其“业务关停通知”&#xff0c;确实如此&#xff0c;由于业务调整&#xff0c;腾讯待办将于2023年的12月20日全面停止运营并下架&#xff0c;这就表示以后我们无法继续使用它了。在腾讯待办关停之前&#xff0c;绝大多数用户需要做的就是及时…