问题描述
好球员,我有这样的想法,重新建立一个程序至极能找到在IP网络中最好的溃败。我发现谁做互联网上的一个人,他分享了一些code..but不管我用它做,我不能使它工作。我使用Visual Studio 2012与最新的BOOST库,C ++。
IP协议具有由非标准路由数据传输的可能性。因此,在IP-packege头,可以指定通过该packege转发交付给收件人的IP地址列表网络路由器的特定源路由选项。
在标准和替代路线数据的同步传输会降低紧张标准的路线,并增加与收件人连接的带宽。
这项目的主要目的是增加与同时使用几个数据信道的用户定义路由连接的带宽,该计划被分成子任务:
- 开发一种算法,以确定在给定方向的节点的基于IP的网络的连接性。
- 制定了predetermined方向查找和选择传送数据的替代路线的算法。
- 创建的方法的软件实现动态确定基础上开发的算法和源路由IP选项的路由流量。
- 使用所开发的软件实施开展一项实验,以评价疗效的数据。
的#includestdafx.h中
#包括LT&;&iostream的GT;
#包括LT&;串GT;
#包括LT&;&stdio.h中GT;
#包括LT&;升压/ asio.hpp>
#包括LT&;升压/ array.hpp>
#包括LT&;升压/ bind.hpp>
#包括LT&;升压/ DATE_TIME /了posix_time / posix_time.hpp>
#包括LT&;升压/间/ file_mapping.hpp>
#包括LT&;升压/间/ mapped_region.hpp>
#包括LT&;升压/ filesystem.hpp>
#包括LT&;升压/线程/ thread.hpp>
#包括LT&;升压/ date_time.hpp>使用命名空间std;
使用命名空间std :: TR1;
使用名字空间boost;
使用空间boost :: this_thread;
使用空间boost :: ASIO;
使用空间boost ::支持ASIO ::知识产权;
使用空间boost ::文件系统;
使用空间boost ::制度;
使用空间boost ::进程间;
使用空间boost ::了posix_time;INT发送(字符*主机,无符号短端口,为const char * FILE_PATH){
尝试{
io_service对象_io_service;
TCP ::插座_socket(_io_service,TCP :: V4());
系统::错误_ code _error_ code;
TCP ::端点TARGET_ADDRESS(address_v4 :: from_string(主机),端口);
_socket.connect(TARGET_ADDRESS); uintmax_t型REGION_SIZE = 5 * 1024 * 1024;
uintmax_t型_file_size = FILE_SIZE(FILE_PATH);
file_mapping _file_mapping(FILE_PATH,READ_ONLY); size_t型书面= 0;
分组时间开始=提振::了posix_time :: microsec_clock :: universal_time();
对于(uintmax_t型位置= 0;&位置LT; _file_size;仓位+ = REGION_SIZE){
mapped_region _mapped_region(_file_mapping,READ_ONLY,位置,分(REGION_SIZE,_file_size - 位置));
书面+ =写入(_socket,缓冲液(_mapped_region.get_address(),_mapped_region.get_size()),transfer_all(),
_error_ code);
}
分组时间完成=提振::了posix_time :: microsec_clock :: universal_time(); COUT<< 已发送<<写<< 的&所述;&下; _file_size<< 在&所述;&下;完成 - 开始与LT;< ENDL;
_socket.close();
返回0;
}赶上(的std ::例外急症){
CERR<< e.what()&所述;&下; ENDL;
返回1;
}
}INT接收(无符号短端口,为const char * FILE_NAME){
尝试{
io_service对象_io_service;
TCP ::插座_socket(_io_service); 路径FILE_PATH(FILE_NAME);
如果(存在(FILE_PATH)){
删除(FILE_PATH);
} const int的BUFFER_SIZE = 5 * 1024 * 1024;
烧焦_buffer [BUFFER_SIZE];
FILE *文件= FOPEN(FILE_NAME,WB); TCP ::端点_endpoint(TCP :: V4(),端口);
TCP ::受_acceptor(_io_service,_endpoint);
系统::错误_ code _error_ code; _acceptor.accept(_socket);
COUT<< &LT来自请求;< _socket.remote_endpoint()&所述;&下; \\ n; 为size_t接收= 0;
为size_t部= 0;
分组时间开始=提振::了posix_time :: microsec_clock :: universal_time();
做{
部分=读(_socket,缓冲区(_buffer,BUFFER_SIZE),_error_ code);
接到+ =部分;
FWRITE(_buffer,1,部分文件);
}而(部分大于0);
分组时间完成=提振::了posix_time :: microsec_clock :: universal_time(); COUT<< 收到<<接收到的LT;< 在&所述;&下;完成 - 开始与LT;< ENDL;
_socket.close();
FCLOSE(文件);
返回0;
}赶上(的std ::例外急症){
CERR<< e.what()&所述;&下; ENDL;
返回1;
}
}INT检查(字符* file_name1,字符* file_name2){
COUT<< 检查......;
cout.flush();
FILE *文件1 = FOPEN(file_name1,RB);
FILE *文件2 = FOPEN(file_name2,RB);
const的无符号整型的buffer_size = 2 * 1024 * 1024;
烧焦缓冲器1 [BUFFER_SIZE];
烧焦缓冲器2 [BUFFER_SIZE]; 做{
为size_t RED1 = FREAD(缓冲器1,1,BUFFER_SIZE,文件1);
为size_t RED2 = FREAD(缓冲器2,1,BUFFER_SIZE,文件2);
如果(的feof(文件1)!=的feof(文件2)|| RED1!= || RED2 memcmp(缓冲器1,缓冲器2,RED1)){
COUT<< 错误!!! << ENDL;
返回-1;
}
}而(的feof(文件1)及!&安培;!的feof(文件2)); COUT<< OK&所述;&下; ENDL;
返回0;
}INT sendTest(字符*目标,INT端口,INT ARGC,字符** argv的){
io_service对象_io_service;
TCP ::端点TARGET_ADDRESS(address_v4 :: from_string(目标),端口);
TCP ::插座_socket(_io_service,TCP :: V4());
系统::错误_ code _error_ code; 如果(argc个大于0){
INT路线[ARGC + 1];
((字符*)航线)[0] = 1;
((字符*)航线)[1] = 131;
((字符*)途径)[2] = 3 +的argc * 4;
((字符*)途径)[3] = 4;
的for(int i = 0; I< ARGC,我++){
路线[I + 1] = inet_addr(的argv [I]);
} 如果(setsockopt的(_socket.native_handle(),IPPROTO_IP,IP_OPTIONS,路线,(的argc + 1)* 4)℃,){
PERROR(无法设置套接字选项);
}
} _socket.connect(TARGET_ADDRESS);
性病::法院LT&;<写(_socket,缓冲液(测试,4),_error_ code)<<的std :: ENDL; _socket.close();
返回0;
}INT MAIN2(INT ARGC,字符** argv的){
如果(的argc&GT = 2){
字符串选项(的argv [1]);
如果(的argc&GT = 5和;&放大器; option.compare( - 的)== 0){
回送(的argv [2],(无符号短)的atoi(argv的[3]),ARGV [4]);
}否则如果(的argc> = 4和&放大器; option.compare( - R)== 0){
收到返回((无符号短)的atoi(argv的[2])的argv [3]);
}否则如果(的argc> = 4和&放大器; option.compare( - T)== 0){
返回sendTest(的argv [2],与atoi(argv的[3]),的argc - 4,argv的+ 4);
}否则如果(的argc> = 4和&放大器; option.compare( - C)== 0){
返回检查(argv的[2],的argv [3]);
}
}
COUT<< 参数必须是:<< ENDL<< \\ - 的主机POST FILE_NAME \\发件人<< ENDL
<< \\ - R PORT FILE_NAME \\接收器<< ENDL<< \\ - ÇFILI_1_NAME FILE_2_NAME \\接收器<< ENDL
<< \\ - T的主机端口ROUTE_HOST_1 ... \\来测试松散源发送<< ENDL;
返回1;
}INT主(INT ARGC,字符** argv的){
性病:: TR1 :: unordered_multimap<为unsigned int,unsigned int类型>图形;
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(1,2));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(2,1));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(1,3));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(3,1));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(3,5));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(5,3));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(2,4));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(4,2));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(1,4));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(4,1));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(1,7));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(7,1));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(5,6));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(6,5));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(5,7));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(7,5));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(8,7));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(7,8));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(4,8));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(8,4));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(8,9));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(9,8));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(9,10));
graph.insert(性病:: make_pair<为unsigned int,unsigned int类型>(10,9));
名单<清单及LT; unsigned int类型> *>路线;
UIntUIntMap our_reverse;
UIntUIntMap target_reverse;
UIntSet交叉口;
升压:: ASIO :: LSR ::路由器:: findIntersections(图中,1,8,4,交叉路口,our_reverse,target_reverse);
}
对不起球员,我使用VS2012的乌克兰版本,我不能显示我的警告,你就看不明白他们
现在我有与code的烦恼:
如果(ARGC大于0){
INT路线[ARGC + 1];
((字符*)航线)[0] = 1;
((字符*)航线)[1] = 131;
((字符*)途径)[2] = 3 +的argc * 4;
((字符*)途径)[3] = 4;
的for(int i = 0; I< ARGC,我++){
路线[I + 1] = inet_addr(的argv [I]);
}
我就想不通了这一切,但下面的是一个固定的后续版本没有一些暴行
- 所有的
使用命名空间
取值求冲突/干扰默默 - TR1的依赖肯定是过时的,即使在VS现在
现在,剩下的事情与做
- 假设无符号字符(字面131不直接支持很多编译)
- 使用沃拉斯(这是一个C的东西或编译器扩展,据我所知)
-
的boost ::支持ASIO :: LSR
的由于缺少
看来你发现的主要程序和 LSR :: findIntersections
code丢失。
下面是清理版本,在情况下,它可以帮助你。
//#包括stdafx.h中
#包括LT&;升压/ array.hpp>
#包括LT&;升压/ asio.hpp>
#包括LT&;升压/ bind.hpp>
#包括LT&;升压/ date_time.hpp>
#包括LT&;升压/ DATE_TIME /了posix_time / posix_time.hpp>
#包括LT&;升压/ filesystem.hpp>
#包括LT&;升压/间/ file_mapping.hpp>
#包括LT&;升压/间/ mapped_region.hpp>
#包括LT&;升压/线程/ thread.hpp>
#包括LT&;&iostream的GT;
//#包括LT&;&stdio.h中GT;
#包括LT&;串GT;
#包括LT&;&unordered_map GT;命名空间ASIO =提振:: ASIO;
命名空间FS =的boost ::文件系统;
命名空间SYS =提振::制度;
命名空间BIP =的boost ::进程间;
使用boost ::支持ASIO ::知识产权:: TCP;
使用boost ::了posix_time ::分组时间;INT发送(字符*主机,无符号短端口,为const char * FILE_PATH){
尝试{
ASIO :: io_service对象io_service_;
TCP ::插座socket_(io_service_,TCP :: V4());
SYS ::错误_ code错误_ code_;
TCP ::端点TARGET_ADDRESS(ASIO ::知识产权:: address_v4 :: from_string(主机),端口);
socket_.connect(TARGET_ADDRESS); uintmax_t型REGION_SIZE = 5 * 1024 * 1024;
uintmax_t型file_size_ = FS :: FILE_SIZE(FILE_PATH); BIP :: file_mapping file_mapping_(FILE_PATH,BIP :: READ_ONLY); size_t型书面= 0;
分组时间开始=提振::了posix_time :: microsec_clock :: universal_time();
对于(uintmax_t型位置= 0;&位置LT; file_size_;仓位+ = REGION_SIZE){
BIP :: mapped_region mapped_region_(
file_mapping_,
BIP :: READ_ONLY,
位置,
的std ::分(REGION_SIZE,file_size_ - 位置)
); 书面+ =写(
插座_,
ASIO ::缓冲液(mapped_region_.get_address(),mapped_region_.get_size()),
ASIO :: transfer_all()
错误_ code_
);
}
分组时间完成=提振::了posix_time :: microsec_clock :: universal_time(); 性病::法院LT&;< 已发送<<写<< 的&所述;&下; file_size_<< 在&所述;&下;完成 - 开始与LT;<的std :: ENDL;
socket_.close();
}赶上(的std ::例外急症){
的std :: CERR<< e.what()&所述;&下;的std :: ENDL;
返回1;
}
返回0;
}INT接收(无符号短端口,为const char * FILE_NAME){
尝试{
ASIO :: io_service对象io_service_;
TCP ::插座socket_(io_service_); FS ::路径FILE_PATH(FILE_NAME);
如果(FS ::存在(FILE_PATH)){
FS ::删除(FILE_PATH);
} const int的BUFFER_SIZE = 5 * 1024 * 1024;
FILE *文件= FOPEN(FILE_NAME,WB);
烧焦buffer_ [BUFFER_SIZE]; TCP ::端点endpoint_(TCP :: V4(),端口);
TCP ::受acceptor_(io_service_,endpoint_);
SYS ::错误_ code错误_ code_; acceptor_.accept(socket_);
性病::法院LT&;< &LT来自请求;< socket_.remote_endpoint()&所述;&下; \\ n; 为size_t接收= 0;
为size_t部= 0;
分组时间开始=提振::了posix_time :: microsec_clock :: universal_time();
做{
部分=读(socket_,ASIO ::缓冲(buffer_,BUFFER_SIZE),错误_ code_);
接到+ =部分; FWRITE(buffer_,1,部分文件);
}而(部分大于0); 分组时间完成=提振::了posix_time :: microsec_clock :: universal_time(); 性病::法院LT&;< 收到<<接收到的LT;< 在&所述;&下;完成 - 开始与LT;<的std :: ENDL;
socket_.close();
FCLOSE(文件);
}赶上(的std ::例外急症){
的std :: CERR<< e.what()&所述;&下;的std :: ENDL;
返回1;
}
返回0;
}INT检查(字符* file_name1,字符* file_name2){
性病::法院LT&;< 检查......;
的std :: cout.flush(); FILE *文件1 = FOPEN(file_name1,RB);
FILE *文件2 = FOPEN(file_name2,RB);
const的无符号整型的buffer_size = 2 * 1024 * 1024;
烧焦缓冲器1 [BUFFER_SIZE];
烧焦缓冲器2 [BUFFER_SIZE]; 做{
为size_t RED1 = FREAD(缓冲器1,1,BUFFER_SIZE,文件1);
为size_t RED2 = FREAD(缓冲器2,1,BUFFER_SIZE,文件2);
如果(的feof(文件1)!=的feof(文件2)|| RED1!= || RED2 memcmp(缓冲器1,缓冲器2,RED1)){
性病::法院LT&;< 错误!!! <<的std :: ENDL;
返回-1;
}
}而(的feof(文件1)及!&安培;!的feof(文件2)); 性病::法院LT&;< OK&所述;&下;的std :: ENDL;
返回0;
}INT sendTest(字符*目标,INT端口,INT ARGC,字符** argv的){
ASIO :: io_service对象io_service_;
TCP ::端点TARGET_ADDRESS(ASIO ::知识产权:: address_v4 :: from_string(目标),端口);
TCP ::插座socket_(io_service_,TCP :: V4());
SYS ::错误_ code错误_ code_; 如果(argc个大于0){
INT路线[ARGC + 1];
((字符*)航线)[0] = 1;
((字符*)航线)[1] = 131;
((字符*)途径)[2] = 3 +的argc * 4;
((字符*)途径)[3] = 4;
的for(int i = 0; I< ARGC,我++){
路线[I + 1] = inet_addr(的argv [I]);
} 如果(setsockopt的(socket_.native_handle(),IPPROTO_IP,IP_OPTIONS,路线,(的argc + 1)* 4)℃,){
PERROR(无法设置套接字选项);
}
} socket_.connect(TARGET_ADDRESS);
性病::法院LT&;<写(socket_,ASIO ::缓冲液(测试,4),错误_ code_)LT;<的std :: ENDL; socket_.close();
返回0;
}INT MAIN2(INT ARGC,字符** argv的){
如果(的argc&GT = 2){
性病::字符串选项(的argv [1]);
如果(的argc&GT = 5和;&放大器; option.compare( - 的)== 0){
回送(的argv [2]的static_cast<无符号短>(与atoi(argv的[3]))的argv [4]);
}否则如果(的argc> = 4和&放大器; option.compare( - R)== 0){
返回接收(的static_cast<无符号短>(与atoi(argv的[2]))的argv [3]);
}否则如果(的argc> = 4和&放大器; option.compare( - T)== 0){
返回sendTest(的argv [2],与atoi(argv的[3]),的argc - 4,argv的+ 4);
}否则如果(的argc> = 4和&放大器; option.compare( - C)== 0){
返回检查(argv的[2],的argv [3]);
}
}
性病::法院LT&;< 参数必须是:<<的std :: ENDL<< \\ - 的主机POST FILE_NAME \\发件人<<的std :: ENDL
<< \\ - R PORT FILE_NAME \\接收器<<的std :: ENDL<< \\ - ÇFILI_1_NAME FILE_2_NAME \\接收器<<的std :: ENDL
<< \\ - T的主机端口ROUTE_HOST_1 ... \\来测试松散源发送<<的std :: ENDL;
返回1;
}INT主(INT ARGC,字符** argv的){
的std :: unordered_multimap<为unsigned int,unsigned int类型>图{
{1,2},{2,1},{1,3},{3,1},{3,5},{5,3},{2,4},{4,2},{1 ,4},
{4,1},{1,7},{7,1},{5,6},{6,5},{5,7},{7,5},{8,7},{7 ,8},
{4,8},{8,4},{8,9},{9,8},{9,10},{10,9}}; 性病::名单<的std ::名单< unsigned int类型> *>路线; //这里的猜测工作启动
的typedef的std ::设为< unsigned int类型> UIntSet;
的typedef的std ::地图<为unsigned int,unsigned int类型> UIntUIntMap; UIntUIntMap our_reverse;
UIntUIntMap target_reverse;
UIntSet交叉口; 升压:: ASIO :: LSR ::路由器:: findIntersections(图中,1,8,4,交叉路口,our_reverse,target_reverse);
}
Okay guys, I have this idea to recreate a program wich could find the best rout in IP networks. I found one guy on the internet who did it, and he shared some code..but whatever I'm doing with it, I can't make it work. I'm using Visual Studio 2012 with latest BOOST libs, C++.
The IP protocol has a possibility of data transmission by the non-standard routes. therefore, at the IP-packege header, you can specify specific source routing option of IP-addresses list network routers through which the packege is forwarded for delivery to the addressee.Simultaneous transmission of data over the standard and alternative routes will reduce the tense on the standard route and increase the bandwidth of the connection with the addressee.
The main goal of that project is increasing bandwidth of connections with user-defined route of simultaneous use of several channels of data, the plan was divided into sub-tasks:
- Develop an algorithm to determine the connectivity of nodes IP-based network in a given direction.
- Develop an algorithm of finding and selecting alternative routes of transmission data in a predetermined direction.
- Create a software implementation of a method for dynamically determining the routes traffic based on the developed algorithms and source routing option of IP.
- Conduct an experiment to evaluate the efficacy data using the developed software implementation.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/filesystem.hpp>
#include <boost/thread/thread.hpp>
#include <boost/date_time.hpp>
using namespace std;
using namespace std::tr1;
using namespace boost;
using namespace boost::this_thread;
using namespace boost::asio;
using namespace boost::asio::ip;
using namespace boost::filesystem;
using namespace boost::system;
using namespace boost::interprocess;
using namespace boost::posix_time;
int send(char *host, unsigned short port, const char *file_path) {
try {
io_service _io_service;
tcp::socket _socket(_io_service, tcp::v4());
system::error_code _error_code;
tcp::endpoint target_address(address_v4::from_string(host), port);
_socket.connect(target_address);
uintmax_t region_size = 5 * 1024 * 1024;
uintmax_t _file_size = file_size(file_path);
file_mapping _file_mapping(file_path, read_only);
size_t written = 0;
ptime start = boost::posix_time::microsec_clock::universal_time();
for (uintmax_t position = 0; position < _file_size; position += region_size) {
mapped_region _mapped_region(_file_mapping, read_only, position, min(region_size, _file_size - position));
written += write(_socket, buffer(_mapped_region.get_address(), _mapped_region.get_size()), transfer_all(),
_error_code);
}
ptime finish = boost::posix_time::microsec_clock::universal_time();
cout << "Sent " << written << " of " << _file_size << " in " << finish - start << endl;
_socket.close();
return 0;
} catch (std::exception &e) {
cerr << e.what() << endl;
return 1;
}
}
int receive(unsigned short port, const char *file_name) {
try {
io_service _io_service;
tcp::socket _socket(_io_service);
path file_path(file_name);
if (exists(file_path)) {
remove(file_path);
}
const int buffer_size = 5 * 1024 * 1024;
char _buffer[buffer_size];
FILE *file = fopen(file_name, "wb");
tcp::endpoint _endpoint(tcp::v4(), port);
tcp::acceptor _acceptor(_io_service, _endpoint);
system::error_code _error_code;
_acceptor.accept(_socket);
cout << "Request from " << _socket.remote_endpoint() << "\n";
size_t received = 0;
size_t portion = 0;
ptime start = boost::posix_time::microsec_clock::universal_time();
do {
portion = read(_socket, buffer(_buffer, buffer_size), _error_code);
received += portion;
fwrite(_buffer, 1, portion, file);
} while (portion > 0);
ptime finish = boost::posix_time::microsec_clock::universal_time();
cout << "Received " << received << " in " << finish - start << endl;
_socket.close();
fclose(file);
return 0;
} catch (std::exception &e) {
cerr << e.what() << endl;
return 1;
}
}
int check(char *file_name1, char *file_name2) {
cout << "Checking ... ";
cout.flush();
FILE *file1 = fopen(file_name1, "rb");
FILE *file2 = fopen(file_name2, "rb");
const unsigned int buffer_size = 2 * 1024 * 1024;
char buffer1[buffer_size];
char buffer2[buffer_size];
do {
size_t red1 = fread(buffer1, 1, buffer_size, file1);
size_t red2 = fread(buffer2, 1, buffer_size, file2);
if (feof(file1) != feof(file2) || red1 != red2 || memcmp(buffer1, buffer2, red1)) {
cout << "ERROR!!!" << endl;
return -1;
}
} while (!feof(file1) && !feof(file2));
cout << "OK" << endl;
return 0;
}
int sendTest(char *destination, int port, int argc, char **argv) {
io_service _io_service;
tcp::endpoint target_address(address_v4::from_string(destination), port);
tcp::socket _socket(_io_service, tcp::v4());
system::error_code _error_code;
if (argc > 0) {
int route[argc + 1];
((char *)route)[0] = 1;
((char *)route)[1] = 131;
((char *)route)[2] = 3 + argc * 4;
((char *)route)[3] = 4;
for (int i = 0; i < argc; i++) {
route[i + 1] = inet_addr(argv[i]);
}
if (setsockopt(_socket.native_handle(), IPPROTO_IP, IP_OPTIONS, route, (argc + 1) * 4) < 0) {
perror("can't set socket option");
}
}
_socket.connect(target_address);
std::cout << write(_socket, buffer("Test", 4), _error_code) << std::endl;
_socket.close();
return 0;
}
int main2(int argc, char **argv) {
if (argc >= 2) {
string option(argv[1]);
if (argc >= 5 && option.compare("-s") == 0) {
return send(argv[2], (unsigned short)atoi(argv[3]), argv[4]);
} else if (argc >= 4 && option.compare("-r") == 0) {
return receive((unsigned short)atoi(argv[2]), argv[3]);
} else if (argc >= 4 && option.compare("-t") == 0) {
return sendTest(argv[2], atoi(argv[3]), argc - 4, argv + 4);
} else if (argc >= 4 && option.compare("-c") == 0) {
return check(argv[2], argv[3]);
}
}
cout << "Argument must be:" << endl << "\"-s HOST POST FILE_NAME\" for sender" << endl
<< "\"-r PORT FILE_NAME\" for receiver" << endl << "\"-c FILI_1_NAME FILE_2_NAME\" for receiver" << endl
<< "\"-t HOST PORT ROUTE_HOST_1 ...\" to test loose source send" << endl;
return 1;
}
int main(int argc, char **argv) {
std::tr1::unordered_multimap<unsigned int, unsigned int> graph;
graph.insert(std::make_pair<unsigned int, unsigned int>(1, 2));
graph.insert(std::make_pair<unsigned int, unsigned int>(2, 1));
graph.insert(std::make_pair<unsigned int, unsigned int>(1, 3));
graph.insert(std::make_pair<unsigned int, unsigned int>(3, 1));
graph.insert(std::make_pair<unsigned int, unsigned int>(3, 5));
graph.insert(std::make_pair<unsigned int, unsigned int>(5, 3));
graph.insert(std::make_pair<unsigned int, unsigned int>(2, 4));
graph.insert(std::make_pair<unsigned int, unsigned int>(4, 2));
graph.insert(std::make_pair<unsigned int, unsigned int>(1, 4));
graph.insert(std::make_pair<unsigned int, unsigned int>(4, 1));
graph.insert(std::make_pair<unsigned int, unsigned int>(1, 7));
graph.insert(std::make_pair<unsigned int, unsigned int>(7, 1));
graph.insert(std::make_pair<unsigned int, unsigned int>(5, 6));
graph.insert(std::make_pair<unsigned int, unsigned int>(6, 5));
graph.insert(std::make_pair<unsigned int, unsigned int>(5, 7));
graph.insert(std::make_pair<unsigned int, unsigned int>(7, 5));
graph.insert(std::make_pair<unsigned int, unsigned int>(8, 7));
graph.insert(std::make_pair<unsigned int, unsigned int>(7, 8));
graph.insert(std::make_pair<unsigned int, unsigned int>(4, 8));
graph.insert(std::make_pair<unsigned int, unsigned int>(8, 4));
graph.insert(std::make_pair<unsigned int, unsigned int>(8, 9));
graph.insert(std::make_pair<unsigned int, unsigned int>(9, 8));
graph.insert(std::make_pair<unsigned int, unsigned int>(9, 10));
graph.insert(std::make_pair<unsigned int, unsigned int>(10, 9));
list<list<unsigned int> *> routes;
UIntUIntMap our_reverse;
UIntUIntMap target_reverse;
UIntSet intersections;
boost::asio::lsr::router::findIntersections(graph, 1, 8, 4, intersections, our_reverse, target_reverse);
}
Sorry guys, I'm using ukrainian version of VS2012 and I can't show the warnings I have, you wouldn't understand them anyway
right now I have troubles with that code:
if (argc > 0) {
int route[argc + 1];
((char*) route)[0] = 1;
((char*) route)[1] = 131;
((char*) route)[2] = 3 + argc * 4;
((char*) route)[3] = 4;
for (int i = 0; i < argc; i++) {
route[i + 1] = inet_addr(argv[i]);
}
I can't figure it all out, but the below is a fixed up version without some of atrocities
- all those
using namespace
s beg to conflict/interfere silently - the tr1 dependency surely is obsolete even in VS by now
Now, the remaining things are to do with
- assuming unsigned characters (the literal 131 isn't directly supported on many compilers)
- using VLAs (which are a C things or a compiler extension, AFAIK)
- the absense of
boost::asio::lsr
It seems you found the main program and the lsr::findIntersections
code is missing.
Here's the cleaned up version, in case it helps you
//#include "stdafx.h"
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem.hpp>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/thread/thread.hpp>
#include <iostream>
//#include <stdio.h>
#include <string>
#include <unordered_map>
namespace asio = boost::asio;
namespace fs = boost::filesystem;
namespace sys = boost::system;
namespace bip = boost::interprocess;
using boost::asio::ip::tcp;
using boost::posix_time::ptime;
int send(char *host, unsigned short port, const char *file_path) {
try {
asio::io_service io_service_;
tcp::socket socket_(io_service_, tcp::v4());
sys::error_code error_code_;
tcp::endpoint target_address(asio::ip::address_v4::from_string(host), port);
socket_.connect(target_address);
uintmax_t region_size = 5 * 1024 * 1024;
uintmax_t file_size_ = fs::file_size(file_path);
bip::file_mapping file_mapping_(file_path, bip::read_only);
size_t written = 0;
ptime start = boost::posix_time::microsec_clock::universal_time();
for (uintmax_t position = 0; position < file_size_; position += region_size) {
bip::mapped_region mapped_region_(
file_mapping_,
bip::read_only,
position,
std::min(region_size, file_size_ - position)
);
written += write(
socket_,
asio::buffer(mapped_region_.get_address(), mapped_region_.get_size()),
asio::transfer_all(),
error_code_
);
}
ptime finish = boost::posix_time::microsec_clock::universal_time();
std::cout << "Sent " << written << " of " << file_size_ << " in " << finish - start << std::endl;
socket_.close();
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
int receive(unsigned short port, const char *file_name) {
try {
asio::io_service io_service_;
tcp::socket socket_(io_service_);
fs::path file_path(file_name);
if (fs::exists(file_path)) {
fs::remove(file_path);
}
const int buffer_size = 5 * 1024 * 1024;
FILE *file = fopen(file_name, "wb");
char buffer_[buffer_size];
tcp::endpoint endpoint_(tcp::v4(), port);
tcp::acceptor acceptor_(io_service_, endpoint_);
sys::error_code error_code_;
acceptor_.accept(socket_);
std::cout << "Request from " << socket_.remote_endpoint() << "\n";
size_t received = 0;
size_t portion = 0;
ptime start = boost::posix_time::microsec_clock::universal_time();
do {
portion = read(socket_, asio::buffer(buffer_, buffer_size), error_code_);
received += portion;
fwrite(buffer_, 1, portion, file);
} while (portion > 0);
ptime finish = boost::posix_time::microsec_clock::universal_time();
std::cout << "Received " << received << " in " << finish - start << std::endl;
socket_.close();
fclose(file);
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
int check(char *file_name1, char *file_name2) {
std::cout << "Checking ... ";
std::cout.flush();
FILE *file1 = fopen(file_name1, "rb");
FILE *file2 = fopen(file_name2, "rb");
const unsigned int buffer_size = 2 * 1024 * 1024;
char buffer1[buffer_size];
char buffer2[buffer_size];
do {
size_t red1 = fread(buffer1, 1, buffer_size, file1);
size_t red2 = fread(buffer2, 1, buffer_size, file2);
if (feof(file1) != feof(file2) || red1 != red2 || memcmp(buffer1, buffer2, red1)) {
std::cout << "ERROR!!!" << std::endl;
return -1;
}
} while (!feof(file1) && !feof(file2));
std::cout << "OK" << std::endl;
return 0;
}
int sendTest(char *destination, int port, int argc, char **argv) {
asio::io_service io_service_;
tcp::endpoint target_address(asio::ip::address_v4::from_string(destination), port);
tcp::socket socket_(io_service_, tcp::v4());
sys::error_code error_code_;
if (argc > 0) {
int route[argc + 1];
((char *)route)[0] = 1;
((char *)route)[1] = 131;
((char *)route)[2] = 3 + argc * 4;
((char *)route)[3] = 4;
for (int i = 0; i < argc; i++) {
route[i + 1] = inet_addr(argv[i]);
}
if (setsockopt(socket_.native_handle(), IPPROTO_IP, IP_OPTIONS, route, (argc + 1) * 4) < 0) {
perror("can't set socket option");
}
}
socket_.connect(target_address);
std::cout << write(socket_, asio::buffer("Test", 4), error_code_) << std::endl;
socket_.close();
return 0;
}
int main2(int argc, char **argv) {
if (argc >= 2) {
std::string option(argv[1]);
if (argc >= 5 && option.compare("-s") == 0) {
return send(argv[2], static_cast<unsigned short>(atoi(argv[3])), argv[4]);
} else if (argc >= 4 && option.compare("-r") == 0) {
return receive(static_cast<unsigned short>(atoi(argv[2])), argv[3]);
} else if (argc >= 4 && option.compare("-t") == 0) {
return sendTest(argv[2], atoi(argv[3]), argc - 4, argv + 4);
} else if (argc >= 4 && option.compare("-c") == 0) {
return check(argv[2], argv[3]);
}
}
std::cout << "Argument must be:" << std::endl << "\"-s HOST POST FILE_NAME\" for sender" << std::endl
<< "\"-r PORT FILE_NAME\" for receiver" << std::endl << "\"-c FILI_1_NAME FILE_2_NAME\" for receiver" << std::endl
<< "\"-t HOST PORT ROUTE_HOST_1 ...\" to test loose source send" << std::endl;
return 1;
}
int main(int argc, char **argv) {
std::unordered_multimap<unsigned int, unsigned int> graph {
{1, 2}, {2, 1}, {1, 3}, {3, 1}, {3, 5}, {5, 3}, {2, 4}, {4, 2}, {1, 4},
{4, 1}, {1, 7}, {7, 1}, {5, 6}, {6, 5}, {5, 7}, {7, 5}, {8, 7}, {7, 8},
{4, 8}, {8, 4}, {8, 9}, {9, 8}, {9, 10}, {10, 9} };
std::list<std::list<unsigned int> *> routes;
// here the guess work starts
typedef std::set<unsigned int> UIntSet;
typedef std::map<unsigned int, unsigned int> UIntUIntMap;
UIntUIntMap our_reverse;
UIntUIntMap target_reverse;
UIntSet intersections;
boost::asio::lsr::router::findIntersections(graph, 1, 8, 4, intersections, our_reverse, target_reverse);
}
这篇关于在IP网络中动态松散源路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!