本地机器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网络
有两条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网络
解决方案 »
- 找合作伙伴(写售餐软件)
- 关于Delphi7中的控件使用问题,Delphi7高手达人们帮忙啊!
- 如何设置某按钮为窗体中的默认按钮
- 怎么在delphi中的按钮中加图标啊,在线等~~
- 为什么会是这样呢?200分
- 别人提供的VB控件在DELPHI里能用吗?如果能,怎么用?
- 如何在程序运行过程中随时在popupmenu对象中增加一个item
- 如何响应COMBOBOX的下拉选项关闭时的消息
- 用tchart得AddXY()画图的疑惑
- 求delphi制作MP3播放器的代码或教程.(能有帮助的都行)
- ASP.net 调用神思二代身份证RdCard.dll 返回值为0
- Delphi2010里面,如何把GB2312转换为utf编码呢,在线等!!!
如果不是,在 delphi 里使用 windows api 也是很容易的事啊,只要知道属于哪个 DLL 以及函数名称和参数定义。
如果 indy 可以通过指定网关访问网络,那说明socket是支持选择网关,注意:我说的选择网关,不是说本机是多IP,程序绑定某个IP访问网络。