我正在尝试为我们的基础架构开发内部扫描仪,当时我已经开始研究 boost asio 以启动 254 连接。
我在 boost asio 文档中没有找到任何可以为我指明正确方向的示例。
我想创建一个对象的 254 个实例,每个实例都使用 async_read_until 连接和读取,使用 io_service。
从 boost 中使用多个 tcp::resolver 和多个 tcp::socket 的正确方法是什么(理想情况下每个类一个?)
我还看到一些关于解析器和插座不可移动的投诉
我有非常基本的代码,不太确定下一步该去哪里……我将不胜感激。谢谢。
主.cpp:
#include <cstdlib>
#include <iostream>
#include <vector>
#include <thread>
#include <boost/asio.hpp>
#include "Harvester.hpp"
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: harvest <range> IE: 10.30.0" << std::endl;
return EXIT_FAILURE;
}
boost::asio::io_service io_service;
// Launch the io_service thread ...
std::thread t([&io_service]() { io_service.run(); });
// do a bunch of stuff
std::string range(argv[1]);
std::cout << "Range is " << range << std::endl;
// Build up a list of harvester
std::string tempRange;
std::vector<Harvester> harvesters;
//harvesters.reserve(254);
for (int x=1; x<255; x++) {
tempRange = range + "." + std::to_string(x);
std::cout << "Going to harvest " << tempRange << std::endl;
harvesters.emplace_back( io_service );
}
t.join();
return EXIT_SUCCESS;
}
收割机.hpp:
#include <boost/asio.hpp>
#include <string>
using boost::asio::ip::tcp;
class Harvester {
public:
Harvester(boost::asio::io_service &io_service);
Harvester(const Harvester&&); // move constructor..
void connectTo(std::string &ip);
void doRead();
void closeConnection();
private:
std::string receivedData;
boost::asio::io_service &io_service_;
//tcp::resolver resolver;
tcp::socket socket_;
};
收割机.cpp
#include "Harvester.hpp"
Harvester::Harvester(boost::asio::io_service &io_service) : io_service_(io_service), socket_(io_service) {
}
// This is for the emplace_back? Create a new object and move into the vector (this break everything big time)
Harvester::Harvester(const Harvester&& other) {
io_service_ = other.io_service_;
socket_ = other.socket_;
}
void Harvester::connectTo(std::string &ip) {
}