本地机器ip:192.168.0.100
有两条ADSL线路,地址:192.168.0.1  192.168.0.2请问:如何才能让程序可以同时通过两个网关访问互联网【是多线程】?我在网上搜索过资料,有人说是可以实现的,只需要更改路由表,我用c++程序在虚拟机里面测试,但是没有成功,C++代码如下
/*
g++ -lz -lm  rout.cpp  -o rout.out -lpthread
*/
#include <sys/ioctl.h>//#include <net/if.h>
//#include <arpa/inet.h>
//#include <linux/netfilter.h>
//#include <linux/if_tun.h>
#include <signal.h>
//#include <assert.h>
//#include <fcntl.h>
#include <net/route.h>
#include <time.h>
#include <pthread.h>
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <zlib.h>using namespace std;
bool Func()
{
signal(SIGPIPE,SIG_IGN);
int sid;
if((sid=socket(AF_INET,SOCK_STREAM,0))==-1) return false;
struct rtentry new_krtentry;
struct sockaddr_in dst;
struct sockaddr_in gw;
struct sockaddr_in genmask;
dst.sin_family = AF_INET;
dst.sin_port=htons(80);
dst.sin_addr.s_addr = inet_addr("118.144.132.110");
gw.sin_family = AF_INET;
gw.sin_addr.s_addr = inet_addr("192.168.0.1");
//http://iframe.ip138.com/ic.asp
genmask.sin_family = AF_INET;
genmask.sin_addr.s_addr = inet_addr("255.255.255.255");
new_krtentry.rt_flags = RTF_UP | RTF_HOST | RTF_GATEWAY;
new_krtentry.rt_metric = 0;
new_krtentry.rt_dev = "eth0";
new_krtentry.rt_dst = *(struct sockaddr*) &dst;
new_krtentry.rt_gateway = *(struct sockaddr*) &gw;
new_krtentry.rt_genmask = *(struct sockaddr*) &genmask;
ioctl(sid, SIOCADDRT, new_krtentry);
unsigned long ul = 0;
ioctl(sid, FIONBIO, &ul);
fd_set set;
FD_ZERO(&set);
FD_SET(sid,&set);
struct timeval tm;
int slen = sizeof(struct sockaddr);
tm.tv_sec = 5000;
tm.tv_usec = 0;
cout << "yes 1" << endl;
setsockopt(sid,SOL_SOCKET,SO_SNDTIMEO,&tm,sizeof(timeval));
setsockopt(sid,SOL_SOCKET,SO_RCVTIMEO,&tm,sizeof(timeval));
select(sid+1, NULL, &set, NULL, &tm);
cout << "yes 1.1" << endl;
int kn=connect(sid,(struct sockaddr *)(&dst),slen);
cout << "yes 2" << endl;
if(kn==-1)
{

if(errno != EINPROGRESS)
{
cout << string(strerror(errno)) << endl;//程序长时间停留在connect,最后这里超时退出
return false;
}
}
cout << "yes 3" << endl;
string s;
int n;
s="GET /ic.asp HTTP/1.1";
s+="Accept:image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*\n";
s+="Accept-Language:zh-cn\n";
s+="User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\n";
s+="Host:iframe.ip138.com\n";
s+="Connection:Keep-Alive\n";
s+="Cookie:CNZZDATA3830481=cnzz_eid%3D1833203805-1373684849-http%253A%252F%252Fip138.com%26ntime%3D1375429904%26cnzz_a%3D0%26retime%3D1375429902697%26sin%3D%26ltime%3D1375429902697%26rtime%3D1; ASPSESSIONIDAATBSSRB=DNFNBAOAPOPLHLOAAPDAJCNF\n";
s+="\n\n";
s+="";
n=send(sid,s.c_str(),s.length(),MSG_NOSIGNAL);
cout << "yes 5" << endl;
if(n!=s.length()) return false;
cout << "yes 6" << endl;
char buf[2000];
bzero(buf,2000);
n=recv(sid,buf,2000,0);

cout << n << endl;
cout << buf << endl;

return true;
}
int main(int argc,char *argv[])
{
cout << SIOCADDRT << endl;
if(Func()) cout << "yes" << endl;
return 0;}
/*
g++ -lz -lm  rout.cpp  -o rout.out -lpthread
*/socketgateway网络

解决方案 »

  1.   

    网上有人说虚拟机不支持路由选择,而我在delphi里面又找不到和  ioctl(sid, SIOCADDRT, new_krtentry); 相同功能的函数 。
      

  2.   

    对了,我是想要知道delphi下面该如何实现。
      

  3.   

    不知道 winsock 里的 ioctlsocket 是不是一样的。
    如果不是,在 delphi 里使用 windows api 也是很容易的事啊,只要知道属于哪个 DLL 以及函数名称和参数定义。
      

  4.   

    indy的话,修改tcp的BoundIP属性。
      

  5.   


    如果 indy 可以通过指定网关访问网络,那说明socket是支持选择网关,注意:我说的选择网关,不是说本机是多IP,程序绑定某个IP访问网络。