代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。