使用connect using sqlca连接数据库,如果和服务器网络不通,会等待很长时间(大约30~60秒)
通过Windows自带的icmp.dll,可以对服务器进行Ping,在很短时间内即可判断网络是否连通。
我将网上的一个例子封装为类n_cst_ping,来简化调用过程。
$PBExportHeader$n_cst_ping.sru $PBExportComments$用API实现的ping,检查网络连接 forward global type n_cst_ping from nonvisualobject end type type icmp_options from structure within n_cst_ping end type type icmp_echo_reply from structure within n_cst_ping end type end forward type icmp_options from structure character Ttl character Tos character Flags character OptionsSize character OptionsData end type type icmp_echo_reply from structure unsignedlong address unsignedlong status unsignedlong roundtriptime unsignedlong datasize unsignedlong reserved unsignedlong datapointer icmp_options options string data end type global type n_cst_ping from nonvisualobject end type global n_cst_ping n_cst_ping type prototypes //声明外部函数 function ulong CreateMutexA (ulong lpMutexAttributes, int bInitialOwner, ref string lpName) library "kernel32.dll" function ulong GetLastError () library "kernel32.dll" Function long IcmpCreateFile() Library"icmp.dll" Function long IcmpCloseHandle(ulong IcmpHandle)Library"icmp.dll" Function long IcmpSendEcho(ulong IcmpHandle1,ulong DesAddress,string requestdata,long datasize,ulong requestoption,ref ICMP_ECHO_REPLY replybuffer,ulong Replysize,ulong timeout)Library"icmp.dll" function int ShellAboutA( ulong al_hWnd, string as_szApp, string as_szOtherStuff, ulong hIcon ) library "shell32" end prototypes type variables string password_,password_hcy="" dec dj[] Constant String ver_old="2K3.11(20)" end variables forward prototypes public function boolean of_ping (string as_ipaddress) public function long of_ipaddresstolong (string as_ipaddress) end prototypes public function boolean of_ping (string as_ipaddress);//Ping IP地址 Long ll_Address //先转换 ll_Address = of_IPAddressToLong(as_IPAddress) If (ll_Address = 0) then MessageBox("提示", as_IPAddress + " 不是一个正确的IP地址", Exclamation!) Return False End if long l_hPort string s_DataToSend long l_iOpt ICMP_ECHO_REPLY echo s_DataToSend="Send this characters" long strlen l_hport=0 strlen=len(s_datatosend) l_hPort = IcmpCreateFile() long rtn rtn=0 rtn=IcmpSendEcho(l_hPort,ll_Address,s_DataToSend,strlen,0,echo,56,200) //关闭通道 long l_rtn l_rtn = IcmpCloseHandle(l_hPort) //返回结果 Return (rtn = 1) end function public function long of_ipaddresstolong (string as_ipaddress);//将IP地址转为长整数 Long ll_Block[4] String ls_Temp int i, j, p ls_Temp = as_IPAddress j = 1 //先判断是否为有效IP地址 p = Pos(ls_Temp, ".") Do While (p > 0) ll_Block[j] = Long(Left(ls_Temp, p - 1)) ls_Temp = Mid(ls_Temp, p + 1) j++ p = Pos(ls_Temp, ".") Loop If (Trim(ls_Temp) <> "") Then ll_Block[j] = Long(Trim(ls_Temp)) End If If (j <> 4) Then Return 0 For i = 1 to 4 If (ll_Block[i] > 255) or (ll_Block[i] < 0) then Return 0 End if Next Return ll_Block[4] * 16777216 + ll_Block[3] * 65536 + ll_Block[2] * 256 + ll_Block[1] end function on n_cst_ping.create call super::create TriggerEvent( this, "constructor" ) end on on n_cst_ping.destroy TriggerEvent( this, "destructor" ) call super::destroy end on
调用方式:
n_cst_Ping Ping Ping = Create n_cst_Ping If Ping.of_Ping(sle_ip.Text) then Messagebox("恭喜!","能连通指定的IP地址") Else Messagebox("错误","无法连通指定的IP地址:" + sle_ip.Text + ",原因:~r~n ①远程服务器关机~r~n ②网络太忙,稍后再试~r~n ③网络中断", StopSign!) End if Destroy Ping
感谢原作者:hygougou
http://topic.csdn.net/t/20050412/18/3930736.html