在PB程序中如何测试网络连接是否中断

添加者:Ericwoo, 最后更新者: Ericwoo 于 二月 20, 2009  (查看变更)

原作者:hygougou http://topic.csdn.net/t/20050412/18/3930736.html

 

出于这样的考虑:
connect using sqlca //如果远程数据库网络不通,这句语句将会耗费比较长的时间等待。所以考虑速度的问题,在连接之前现判断网络是否连通,如果连通则执行这句,不通则提示。

//声明全局变量global var
string password_,password_hcy=""
long dw_address
dec  dj[]
Constant String ver_old="2K3.11(20)"
//声明全局外部函数global ext fun
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"
//声明结构
1、type icmp_echo_reply from structure
unsignedlongaddress
unsignedlongstatus
unsignedlongroundtriptime
unsignedlongdatasize
unsignedlongreserved
unsignedlongdatapointer
icmp_optionsoptions
stringdata
2、icmp_options from structure
characterTtl
characterTos
characterFlags
characterOptionsSize
characterOptionsData
//函数,f_isipaddress
        /*----------------------
        功能:判断是否IP有效地址
        传入:string f_p_address
        返回:boolean
        ------------------------*/
string a_s[16],as_1[4]
int a_dot[3]
int i,j
j=1
for i=1 to 16
a_s[i]=mid(f_p_address,i,1)
if  a_s[i]="." then
a_dot[j]=i
j++
end if
next
if j<>4 then return false
if a_dot[2]=a_dot[1]+1 or a_dot[3]=a_dot[2]+1 then
return false
end if
for i=1 to a_dot[1] - 1
as_1[1]=as_1[1]+a_s[i]
next
for i=a_dot[1]+1 to a_dot[2] - 1
as_1[2]=as_1[2]+a_s[i]
next
for i=a_dot[2]+1 to a_dot[3] - 1
as_1[3]=as_1[3]+a_s[i]
next
for i=a_dot[3]+1 to 16
as_1[4]=as_1[4]+a_s[i]
next
for i=1 to 4
if long(as_1[i])>255 or long(as_1[i])<0 then
return false
exit
end if
next
dw_address=long(as_1[4])*16777216+long(as_1[3])*65536+long(as_1[2])*256+long(as_1[1])
return true
--------------------------------------------------------------------------
函数f_ping
        /*----------------------
        功能:pingIP有效地址
        传入:string u_ipaddress
        返回:无
        ------------------------*/
if f_isipaddress(u_ipaddress) then
else
setprofilestring("ping.ini","properties","result","Ping Failure")
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
dw_address=long(u_ipaddress)
rtn=IcmpSendEcho(l_hPort,dw_address,s_DataToSend,strlen,0,echo,56,200)
if rtn=1 then
//messagebox("Hello","Ping successful")
setprofilestring("ping.ini","properties","result","Ping Successful")
setprofilestring("ping.ini","properties","flag","1")
else
setprofilestring("ping.ini","properties","result","Ping Failure")
setprofilestring("ping.ini","properties","flag","1")
end if
long l_rtn
l_rtn=IcmpCloseHandle(l_hPort)
dw_address=0
// 窗口实例子变量:int return_b
// open事件
   Timer(0.5)
//Timer事件
timer(0)
idle(1)
//SQLCA.DBMS = "O73 ORACLE 7.3"
//SQLCA.LogPass = "zxcvbn"
//SQLCA.ServerName = "@mybzk"
//SQLCA.LogId = "bzk22"
//SQLCA.AutoCommit = False
//SQLCA.DBParm = "CommitOnDisconnect='NO'"
label1:
f_ping(STRING(f_ipaddressany('192.168.1.1')))
if ProfileString("ping.INI", "properties", "Result","None")="Ping Successful" then 
   //CONNECT USING SQLCA;
   //if sqlca.sqlcode<>0 then
   //   messagebox("错误","能连通指定的IP地址,但远程服务器未提供数据服务~r~n"sqlca.sqlerrtext"~r~n~r~n远程服务器未启动或网络配置问题",StopSign!,RetryCancel!)
   //   HALT close
   //else
   w_logo.height=1593
      sle_1.setfocus()
   //end if
else
return_b=messagebox("错误","与中实运业IP地址通迅失败~r~n原因:①远程服务器关机~r~n>>>>>>②网络太忙,稍后再试~r~n>>>>>>③网络中断",StopSign!,RetryCancel!)
   choose case return_b
   case 1
   goto label1
   case 2
         HALT close
   end choose
end if
----屏蔽的代码是连接数据库的代码,自己适当修改

不过,确实漏了一函数,不过不是PING的函数
f_ipaddressany
//    传:string   f_p_address
//    返:long
string a_s[16],as_1[4]
int a_dot[3]
int i,j
j=1
for i=1 to 16
a_s[i]=mid(f_p_address,i,1)
if  a_s[i]="." then
a_dot[j]=i
j++
end if
next
//if j<>4 then return false
if a_dot[2]=a_dot[1]+1 or a_dot[3]=a_dot[2]+1 then
//return false
end if
for i=1 to a_dot[1] - 1
as_1[1]=as_1[1]+a_s[i]
next
for i=a_dot[1]+1 to a_dot[2] - 1
as_1[2]=as_1[2]+a_s[i]
next
for i=a_dot[2]+1 to a_dot[3] - 1
as_1[3]=as_1[3]+a_s[i]
next
for i=a_dot[3]+1 to 16
as_1[4]=as_1[4]+a_s[i]
next
for i=1 to 4
if long(as_1[i])>255 or long(as_1[i])<0 then
//return false
exit
end if
next
long ll_return
ll_return=long(as_1[4])*16777216+long(as_1[3])*65536+long(as_1[2])*256+long(as_1[1])
return ll_return

 

posted @ 2011-02-25 11:08  lenya  阅读(558)  评论(0编辑  收藏  举报