1 Star 0 Fork 0

木鹿/remote_c

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 2.83 KB
一键复制 编辑 原始数据 按行查看 历史
dhtesty 提交于 2024-12-06 16:39 . init
#include <iostream>
#include <thread>
#include <string>
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <boost/asio/detail/socket_option.hpp>
#include <boost/system/system_error.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/lexical_cast.hpp>
int main(){
std::string local,remote, network;
std::vector<std::string> locals,remotes;
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("help,h", "Print help message")
("network,n", boost::program_options::value<std::string>(&network), "tcp or udp")
("local,l", boost::program_options::value<std::string>(&local), "local ip address")
("remote,r", boost::program_options::value<std::string>(&remote)->required(), "remote ip address");
boost::asio::thread_pool pool(10);
boost::split(locals,local,boost::is_any_of(","));
boost::split(remotes,remote,boost::is_any_of(","));
if(network=="tcp"){
boost::asio::io_service io_service;
boost::asio::ip::tcp::acceptor acceptor(io_service,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), boost::lexical_cast<int>(locals[1])));
while (true) {
std::list<boost::asio::ip::tcp::socket> sockets;
sockets.emplace_back(acceptor.accept());
std::thread([](std::list<boost::asio::ip::tcp::socket> ss,boost::asio::ip::tcp::socket s){
boost::array<char, 1024> data;
while(true){
boost::system::error_code ignored_error;
size_t length = s.read_some(boost::asio::buffer(data),ignored_error);
if(ignored_error) ss.remove_if([&](auto& it){return s.remote_endpoint()==it.remote_endpoint();});
std::string message(data.data(), length);
std::string addr = s.remote_endpoint().address().to_string();
s.write_some(boost::asio::buffer(addr),ignored_error);
if(ignored_error) ss.remove_if([&](auto& it){return s.remote_endpoint()==it.remote_endpoint();});
}
}, std::move(sockets), std::move(sockets.back())).detach();
}
}else{
boost::array<char, 1024> data;
boost::asio::io_service io_service;
boost::asio::ip::udp::socket socket(io_service,
boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), boost::lexical_cast<int>(locals[1])));
while(true){
boost::asio::ip::udp::endpoint recv_endpoint;
size_t length = socket.receive_from(boost::asio::buffer(data), recv_endpoint);
socket.send_to(boost::asio::buffer(recv_endpoint.address().to_string()),recv_endpoint);
}
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sofu456/remote_c.git
git@gitee.com:sofu456/remote_c.git
sofu456
remote_c
remote_c
master

搜索帮助