| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 986 人关注过本帖
标题:如何快速判断SQL服务器是否开机
只看楼主 加入收藏
y12692189
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2021-9-23
结帖率:58.33%
收藏
已结贴  问题点数:20 回复次数:11 
如何快速判断SQL服务器是否开机
如何快速判断SQL服务器是否开机呢?
用SQLSTRINGCONNECT()>0  来判断,如果联通会很快,如果没开机会等半天,有没有更快的办法呢?
搜索更多相关主题的帖子: 开机 是否 判断 快速 服务器 
2023-06-26 16:45
y12692189
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2021-9-23
收藏
得分:0 
别的论坛找了段代码,运行的时候说找不到MSWINSCK.WINSOCK类定义

程序代码:
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
2023-06-26 16:54
厨师王德榜
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:199
帖 子:995
专家分:4966
注 册:2013-2-16
收藏
得分:20 
如果要简单点,先ping一下,
参考代码:
程序代码:
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
2023-06-26 17:51
厨师王德榜
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:199
帖 子:995
专家分:4966
注 册:2013-2-16
收藏
得分:0 
回复 2楼 y12692189
还有,你这代码 抄得不对 .最关键的一句是错的.
你的: loPing=createobject("mswinsck.winsock")
应为: loPing=createobject("mswinsock.winsock")
2023-06-27 09:56
csyx
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:火星
等 级:版主
威 望:37
帖 子:709
专家分:2848
注 册:2018-3-13
收藏
得分:0 
ping得通只是必要条件,非充要条件

这家伙很懒,啥也没留下
2023-06-27 11:12
y12692189
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2021-9-23
收藏
得分:0 
回复 3楼 厨师王德榜
感谢,短小精炼,非常适用
2023-06-30 15:07
y12692189
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2021-9-23
收藏
得分:0 
回复 3楼 厨师王德榜
我测试了一下,把127.0.0.1改成任意地址均返回.T.

包括192.168.0.500,也返回的.T.

1.1.1000.500,也返回的.T.

您受累再给改改
2023-06-30 15:20
y12692189
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2021-9-23
收藏
得分:0 
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


上边的127.0.0.1改成任意文字,均返回.T.
2023-06-30 15:51
nbwww
Rank: 8Rank: 8
等 级:贵宾
威 望:11
帖 子:334
专家分:810
注 册:2021-1-9
收藏
得分:0 
https://bbs.bccn.net/viewthread.php?tid=441382&highlight=PING

程序代码:
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
2023-06-30 16:23
schtg
Rank: 12Rank: 12Rank: 12
来 自:Usa
等 级:贵宾
威 望:67
帖 子:1790
专家分:3389
注 册:2012-2-29
收藏
得分:0 
回复 9楼 nbwww
高!
2023-06-30 19:33
快速回复:如何快速判断SQL服务器是否开机
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027302 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved