用Winsock控件判断对方是否关机

在一个网络中怎样判断对方是否关机,知道对方的IP地址。不能用Ping的方法,因为有些电脑限制Ping入。请问怎样用Winsock控件来判断对方是否关机。谢过了!

作者: yqwdn   发布时间: 2011-06-13

应该不行吧,winsock发送信息是要由对方的相关程序响应并返回信息的,如果对方没有相关的协议,是不会发送回馈信息给你的,你也就无法得知对方是否开机了.

作者: qianjin036a   发布时间: 2011-06-13

应该是可以的。
使用api函数获取对方MAC地址,如果获取不到表示关机,否则说明它是开机的(未测试不登录状态下的返回情况)
VB code
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal _
dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) _
As Long
Private Const INFINITE = -1&
Private Const SYNCHRONIZE = &H100000
Private Declare Function inet_addr Lib "wsock32.dll" (ByVal s As String) As Long
Private Declare Function SendARP Lib "iphlpapi.dll" (ByVal DestIP As Long, ByVal SrcIP As Long, pMacAddr As Long, PhyAddrLen As Long) As Long
Private Declare Sub CopyMemory1 Lib "kernel32" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal bcount As Long)

Public Function GetRemoteMACAddress(ByVal sRemoteIP As String)
      Dim dwRemoteIP     As Long
      Dim pMacAddr     As Long
      Dim bpMacAddr()     As Byte
      Dim PhyAddrLen     As Long
      Dim cnt     As Long
      Dim tmp     As String
      'convert    the    string    IP    into
      'an    unsigned    long    value    containing
      'a    suitable    binary    representation
      'of    the    Internet    address    given
      dwRemoteIP = inet_addr(sRemoteIP)
If dwRemoteIP <> 0 Then
          'set    PhyAddrLen    to    6
          PhyAddrLen = 6
          'retrieve    the    remote    MAC    address
          On Error Resume Next
          If SendARP(dwRemoteIP, 0&, pMacAddr, PhyAddrLen) = 0 Then
'GetRemoteMACAddress = pMacAddr
'Exit Function
              If pMacAddr <> 0 And PhyAddrLen <> 0 Then
                  'returned    value    is    a    long    pointer
                  'to    the    mac    address,    so    copy    data
                  'to    a    byte    array
                  ReDim bpMacAddr(0 To PhyAddrLen - 1)
                  CopyMemory1 bpMacAddr(0), pMacAddr, ByVal PhyAddrLen
                  'loop    through    array    to    build    string
                  For cnt = 0 To PhyAddrLen - 1
                      If bpMacAddr(cnt) = 0 Then
                          tmp = tmp & "00-"
                      Else
                          If Len(Hex$(bpMacAddr(cnt))) = 1 Then
                              tmp = tmp & "0" & Hex$(bpMacAddr(cnt)) & "-"
                          Else
                              tmp = tmp & Hex$(bpMacAddr(cnt)) & "-"
                          End If
                      End If
                  Next
                  'remove    the    trailing    dash
                  'added    above    and    return    True
                  If Len(tmp) > 0 Then
                      'sRemoteMacAddress = Left$(tmp, Len(tmp) - 1)
                      GetRemoteMACAddress = Left$(tmp, Len(tmp) - 1)
                  End If
                  Exit Function
              Else
                  GetRemoteMACAddress = False
              End If
          Else
              GetRemoteMACAddress = False
          End If       'SendARP

      Else
          GetRemoteMACAddress = False
      End If       'dwRemoteIP
End Function


以上代码本人不拥有版权,也忘记从哪儿复制的了。

作者: yiguangqiang88   发布时间: 2011-06-13

哦,错了,这没用到winsock。
直接用api行不?

作者: yiguangqiang88   发布时间: 2011-06-13