如何快速判断SQL服务器是否开机
如何快速判断SQL服务器是否开机呢?用SQLSTRINGCONNECT()>0 来判断,如果联通会很快,如果没开机会等半天,有没有更快的办法呢?
Function VfpPing Lparameters tcWhere, tnWhichPort Local lReturnValue * [color=#0000FF]by josh@jassing.com[/color] * [color=#0000FF]with LOTS of help from (actually; all the hard work by) Markus Voellmy[/color] * wrapped up [color=#0000FF]as a big function by Lou Harris[/color] * * ? pingstate( pingx( "www.,[color=#800000]80 ) )[/color] * would [color=#0000FF]check to be sure it's running a web server * NOTE: it does not do any dns lookups; so if you[/color]'re checking for a mail server, you need * [color=#0000FF]to resolve the mx record yourself.[/color] If Vartype(tnWhichPort) # "N" tnWhichPort = 80 Endif *? pingstate( pingx( "www.,80 ) ) lReturnValue = pingstate( pingx( m.tcWhere, m.tnWhichPort ) ) Return m.lReturnValue #Define sckClosed 0 #Define sckOpen 1 #Define sckListening 2 #Define sckConnectionPending 3 #Define sckResolvingHost 4 #Define sckHostResolved 5 #Define sckConnecting 6 #Define sckConnected 7 #Define sckClosing 8 #Define sckError 9 Endfunc Function pingstate Lparameters tnState Local lcState Do Case Case tnState = sckClosed lcState = "Closed" Case tnState = sckOpen lcState = "Open" Case tnState = sckListening lcState = "Listening" Case tnState = sckConnectionPending lcState = "Waiting" Case tnState = sckResolvingHost lcState = "Resolving" Case tnState = sckConnecting lcState = "Connecting" Case tnState = sckConnected lcState = "Connected" Case tnState = sckClosing lcState = "Closing" Case tnState = sckError lcState = "Error" Otherwise lcState = "Unknown" Endcase Return lcState Endfunc Function pingx Lparameters tcServer, tnPort Local lnState, loPing, lnLast loPing=Createobject("mswinsck.winsock") With loPing .remoteport = tnPort .remotehost = tcServer .Connect() lnLast = -1 Do While .state < sckConnected If lnLast # .state lnLast = .state Endif Enddo lnState = .state .Close() Endwith Return lnState
FUNCTION IsCanConnect(lcRemoteIP) as Boolean * 原意是测试某远程主机是否开机 ?但是本函数的实质是:测试某远程IP是否可ping ? * 如果远程主机设置了不允许ping,不等于远程主机未开机.所以本函数酌情使用. local lnResult lcRemoteIP= IIF(!EMPTY(lcRemoteIP),lcRemoteIP,"127.0.0.1") && 防参数错误 toShell = CREATEOBJECT("WScript.Shell") lnResult = toShell.Run("ping " + lcRemoteIP, 0, 1) If lnResult == 0 RETURN .T. Else RETURN .F. ENDIF ENDFUNC
CLEAR DECLARE LONG WSAStartup IN Ws2_32 LONG, STRING@ DECLARE LONG WSACleanup IN Ws2_32 DECLARE LONG socket IN Ws2_32 LONG, LONG, LONG DECLARE LONG closesocket IN Ws2_32 LONG DECLARE LONG inet_addr IN Ws2_32 STRING@ DECLARE LONG inet_ntoa IN Ws2_32 LONG DECLARE LONG setsockopt IN Ws2_32 LONG, LONG, LONG, LONG@, LONG DECLARE LONG sendto IN Ws2_32 LONG, STRING@, LONG, LONG, STRING@, LONG DECLARE LONG recvfrom IN Ws2_32 LONG, STRING@, LONG, LONG, STRING@, LONG@ DECLARE LONG WSAGetLastError IN Ws2_32 DECLARE LONG GetCurrentProcessId IN Kernel32 DECLARE LONG GetTickCount IN Kernel32 DECLARE LONG Sleep IN Kernel32 LONG _Ping("180.97.33.107") *_Ping("127.0.0.1") CLEAR DLLS RETURN FUNCTION _Ping(cDestIP) LOCAL WsaData, IcmpHdr, SockAddr_Dest, SockAddr_From LOCAL i, hSocket, nTimeout, cRecvBuf, cIP, nVal, nTime WsaData = REPLICATE(0h00, 398) WSAStartup(0x202, @WsaData) hSocket = socket(2, 3, 1) IF hSocket != -1 nTimeout = 1000 setsockopt(hSocket, 0xFFFF, 0x1006, @nTimeout, 4) cRecvBuf = REPLICATE(0h00, 1024) SockAddr_From = REPLICATE(0h00, 16) SockAddr_Dest = 0h02000000 + BINTOC(inet_addr(@cDestIP), "4RS") + 0h0000000000000000 FOR i = 1 TO 4 IcmpHdr = 0h08000000; && 请求应答 + LEFT(BINTOC(GetCurrentProcessId(), "4RS"), 2); + BINTOC(i-1, "2RS"); + BINTOC(GetTickCount(), "4RS") IcmpHdr = STUFF(IcmpHdr, 3, 2, LEFT(BINTOC(_CheckSum(IcmpHdr, LEN(IcmpHdr)), "4RS"), 2)) IF sendto(hSocket, @IcmpHdr, LEN(IcmpHdr), 0, @SockAddr_Dest, LEN(SockAddr_Dest)) != -1 nVal = LEN(SockAddr_From) nVal = recvfrom(hSocket, @cRecvBuf, LEN(cRecvBuf), 0, @SockAddr_From, @nVal) nTime = GetTickCount() IF nVal != -1 IF nVal >= (20 + LEN(IcmpHdr)) cIP = SYS(2600, inet_ntoa(CTOBIN(SUBSTR(cRecvBuf, 13, 4), "4RS")), 16) cIP = LEFT(cIP, AT(0h00, cIP) - 1) IF (ASC(SUBSTR(cRecvBuf, 10, 1)) == 1) AND ; && 是ICMP协议 (cIP == cDestIP) AND ; (ASC(SUBSTR(cRecvBuf, 21, 1)) == 0) AND ; && 响应应答 (CTOBIN(SUBSTR(cRecvBuf, 25, 2), "2RS") == GetCurrentProcessId()) nTime = nTime - CTOBIN(SUBSTR(cRecvBuf, 29, 4), "4RS") * 来自 180.97.33.107 的回复: 字节=32 时间=46ms TTL=53 ? "来自 " + cIP ?? " 的回复: 字节=" + TRANSFORM(nVal) ?? " 时间" + IIF(nTime < 1, "<1", "=" + TRANSFORM(nTime)) + "ms" ?? " TTL=" + TRANSFORM(ASC(SUBSTR(cRecvBuf, 9, 1))) && IP头ip_ttl Sleep(1000) ENDIF ENDIF ELSE nVal = WSAGetLastError() IF nVal == 10060 ? "请求超时" ELSE ? "Error: " + TRANSFORM(nVal) ENDIF ENDIF ENDIF ENDFOR ELSE nVal = WSAGetLastError() IF nVal == 10013 ? "因权限被拒,可尝试以管理员身份运行主程序。" ELSE ? "Error: " + TRANSFORM(nVal) ENDIF ENDIF closesocket(hSocket) WSACleanup() ENDFUNC * 校验和 FUNCTION _CheckSum(cBuffer, nSize) LOCAL i, nCheckSum i = 1 nCheckSum = 0 DO WHILE nSize > 1 nCheckSum = nCheckSum + CTOBIN(SUBSTR(cBuffer, i, 2) + 0h0000, "4RS") i = i + 2 nSIze = nSIze - 2 ENDDO IF nSize == 1 nCheckSum = nCheckSum + ASC(SUBSTR(cBuffer, i, 1)) ENDIF nCheckSum = BITRSHIFT(nCheckSum, 16) + BITAND(nCheckSum, 0x0000FFFF) nCheckSum = nCheckSum + BITRSHIFT(nCheckSum, 16) RETURN BITAND(BITNOT(nCheckSum), 0x0000FFFF) ENDFUNC