2015年世界人口展望:如何判断连接上了winsock

来源:百度文库 编辑:神马品牌网 时间:2024/04/28 01:48:37
连接上了会出发connect事件,那么如果没有连接上怎么给出提示?

五种查询Internet连接状态[含IP]的方法
1.Powersock控件法:
这种方法最简单,利用FastNet页的Powersock控件的LocalIP属性即可判断:
if(Powersock1->LocalIP=="127.0.0.1"):在线
else:离线
特点:[1]判断连接状态,[2]获得本地IP。
2.使用URL.DLL的InetIsOffline(0)函数:
Win2K:URL.DLL存放在\SYSTEM32\;
Win9x:URL.DLL存放在\SYSTEM\;
用GetSystemDirectory(...)得到系统目录。
InetIsOffline(0)返回值:
TRUE:离线;FALSE:在线。
特点:判断连接状态。
3.WinSock编程法:见程序
特点:[1]判断连接状态;[2]获得本地IP和主机名。
4.WinInet.DLL的InternetGetConnectedState(&dwFlag,0)函数:
注意:为使用该函数,须在项目文件中加入:USELIB("WinInet.LIB")
特点:获得较详的连接描述!
5.RASAPI32.DLL的RasEnumConnections函数:
要使用该"枚举所有活动连接"函数,必须:
#include"ras.h"。
若连接数>0:本机当前已连入Internet;
否则:本机当前未连入Internet;
源码如下,在[BCB5+WIN2K+拨号上网]下通过(N字头的为菜单项):
-------------Powersock控件法-----------------------------------------
void__fastcallTForm1::N11Click(TObject*Sender)
{
if(Powersock1->LocalIP=="127.0.0.1")
ShowMessage("未连接:"+Powersock1->LocalIP);
elseShowMessage("已连接:"+Powersock1->LocalIP);
}
-------------URL.DLL的InetIsOffline函数法----------------------------
HINSTANCEhDLL;
typedefbool__stdcall(*FUN)(int);定义DLL函数指针FUN
FUNisOffLine;
void__fastcallTForm1::N21Click(TObject*Sender)
{
charBuffer[MAX_PATH];
GetSystemDirectory(Buffer,MAX_PATH);
hDLL=LoadLibrary((AnsiString(Buffer)+"\\URL.DLL").c_str());
if(hDLL==NULL){ShowMessage("CannotloadURL.DLL!Return...");return;}
isOffLine=(FUN)GetProcAddress(hDLL,"InetIsOffline");
if(isOffLine==NULL){ShowMessage("CannotloadInetIsOffline(int),Return...");return;}
if(!isOffLine(0))ShowMessage("已连接");
elseShowMessage("未连接");
FreeLibrary(hDLL);
}
------------WinSock法------------------------------------------------
void__fastcallTForm1::N31Click(TObject*Sender)
{
WORDwVersionRequested;
WSADATAwsaData;
wVersionRequested=MAKEWORD(1,1);StartupWinSock
WSAStartup(wVersionRequested,&wsaData);
-----------------------------------------
hostent*p;char*p2;chars[128];
gethostname(s,128);Getthecomputername
p=gethostbyname(s);
p2=inet_ntoa(*((in_addr*)p->h_addr));GettheIpAddress
-----------------------------------------
AnsiStringLocationIP=p2;
if(LocationIP=="127.0.0.1")
ShowMessage("未连接:"+LocationIP);
elseShowMessage("已连接:"+LocationIP);
WSACleanup();
}
-----------WinInet.DLL的InternetGetConnectedState函数法----------------
void__fastcallTForm1::N41Click(TObject*Sender)
{
StaticText1->Caption="";StaticText2->Caption="";StaticText3->Caption="";
StaticText4->Caption="";StaticText5->Caption="";StaticText6->Caption="";
StaticText7->Caption="";
DWORDdwFlag;
InternetGetConnectedState(&dwFlag,0);
if(dwFlag&INTERNET_CONNECTION_MODEM)StaticText1->Caption="Yes";MODEM连接
elseStaticText1->Caption="No";
if(dwFlag&INTERNET_CONNECTION_LAN)StaticText2->Caption="Yes";LAN连接
elseStaticText2->Caption="No";
if(dwFlag&INTERNET_CONNECTION_PROXY)StaticText3->Caption="Yes";代理连接
elseStaticText3->Caption="No";
---------检查是否连接-------------------------------------------
if(InternetGetConnectedState(NULL,0))StaticText4->Caption="Yes";在线
elseStaticText4->Caption="No";
if(dwFlag&INTERNET_CONNECTION_OFFLINE)StaticText5->Caption="Yes";//离线。注:不好用!
elseStaticText5->Caption="No";
----------------------------------------------------------------
if(dwFlag&INTERNET_RAS_INSTALLED)StaticText6->Caption="Yes";
elseStaticText6->Caption="No";
if(dwFlag&INTERNET_CONNECTION_CONFIGURED)StaticText7->Caption="Yes";
elseStaticText7->Caption="No";
}
----------RASAPI32.DLL的RasEnumConnections函数法---------------------------
#include"ras.h"
void__fastcallTForm1::N51Click(TObject*Sender)
{
RASCONNRASconn[256];活动连接数组
DWORDBuffSize;数组所占内存大小;
DWORDConnNum;活动连接数目
RASconn[0].dwSize=sizeof(RASCONN);必须指定一个连接[数组元素]的内存大小;
BuffSize=sizeof(RASCONN)*256;
DWORDdwReturn=RasEnumConnections(RASconn,&BuffSize,&ConnNum);
if(dwReturn==0)
{
if(ConnNum>0)ShowMessage("已连接。当前激活连接数:"+AnsiString(ConnNum));
elseShowMessage("未连接。当前激活连接数:"+AnsiString(ConnNum));
}
elseShowMessage("RasEnumConnections函数失败!");
}