希望用VB编程实现:服务器端
1、服务器端随时监视N台客户端是否在线
2、服务器端随时监视客户端的USB是否启用

解决方案 »

  1.   

    1、如果客户端是正常退出的话,会触发Close事件,如果还想检测非常退出,由于Winsock控件没有超时属性,故需要自己定时检测Winsock的状态进行判断。
    2、判断HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\usbstor的Start值,如果为3则为已启用。
      

  2.   

    谢谢 lyserver 朋友的回复!对不起,第2点我没说清楚,应该是2、服务器端随时监视客户端的USB的插入或拔出,即使用状态。同时也希望高手能给出程序代码,谢谢!
      

  3.   

    以下是检测USB磁盘是否插入的代码,如果要定时检测,可以配合Timer控件进行使用。Option ExplicitPrivate Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
    Private Const DRIVE_REMOVABLE = 2Function HasUSBDrive() As Boolean
        Dim FSO As Object, Drv As Object
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
        For Each Drv In FSO.Drives
            If InStr("AB", Drv.DriveLetter) = 0 And GetDriveType(Drv) = DRIVE_REMOVABLE Then
                If Drv.IsReady Then
                    HasUSBDrive = True
                    Exit Function
                End If
            End If
        Next
    End Function
      

  4.   

    试过,可以,谢谢 lyserver 朋友!请再给出实现第一条功能的代码吧,我是一个非常菜的菜鸟。
      

  5.   

    如果客户端正常关闭,则会触发服务器端的Close事件,此时可以判断该客户端已中断连接。如果客户端非正常关闭(如拨掉网线或电源),则只能定时发送自己定义的在线检测数据包来检测。
    假设服务器查询的数据包为:“Network Status Inquiry”,等待超时为2秒。
    假设客户端应答数据包为:“Normal network"。
    服务器循环往状态为sckConnected的客户端发送“Network Status Inquiry”字符串,当客户端收到服务器检测数据包(即“Network Status Inquiry”)时,并启动相应的定时器(和winsock一样,定时器也要使用数组),应立即回发“Normal network”字符串,如果服务器在规定的超时时间内收到客户端发回的应答,则说明在线,反之,则视为连接已中断。
      

  6.   

    代码有点长,未经测试,大致思路就是这样的。Option Explicit'假设用于监听的Winsock控件名为sckListen
    '假设用于与客户端连接和通讯的Winsock控件数组名为sckServer
    '假设用于检测客户端连结状态的Timer控件数组名为sckTimer
    '根据楼主要求,只进行了连结性检测,未区别连接中断原因
    Dim m_tmCount As Long'初始化监听套接字
    Private Sub Form_Load()
        Me.sckTimer(0).Enabled = False
        Me.sckListen.Bind 9999, "127.0.0.1"
        Me.sckListen.Listen '开始监听
    End Sub'关闭并卸载所有套接字和定时器
    Private Sub Form_Unload(Cancel As Integer)
        Dim i As Long
        
        sckTimer(0).Enabled = False
        sckServer(0).Close
        For i = sckServer.UBound To 1
            sckTimer(i).Enabled = False
            Unload sckTimer(i)
            sckServer(i).Close
            Unload sckServer(i)
        Next
    End Sub'处理来自客户端的连接请求
    Private Sub sckListen_ConnectionRequest(ByVal requestID As Long)
        Dim i As Long
        
        '遍历数组查找空闲的套接字
        For i = 0 To sckServer.UBound
            If sckServer(i).State = sckClosed Then '找到空闲的套接字
                sckServer(i).Connect requestID '接受连接
                Exit Sub
            End If
        Next
        '如果上面未找到,则增加一个新的套接字
        Load sckServer(i)
        sckServer(i).Connect requestID '接受连接
        '为此套接字增加一个超时检测定时器
        Load sckTimer(i)
        sckTimer(i).Enabled = False
        sckTimer(i).Interval = 1000 '设置超时检测为1秒,楼主可以修改。
    End Sub'Winsock错误处理
    Private Sub sckServer_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
        If Number <> sckSuccess Then '出现错误
            sckServer(Index).Close '关闭此套接字
            '以下处理定时器及全局计数
            If sckTimer(Index).Tag = 1 Then
                sckTimer(Index).Enabled = False
                m_tmCount = m_tmCount - 1
                sckTimer(Index).Tag = ""
            End If
        End If
    End Sub'处理客户端正常关闭的请求
    Private Sub sckServer_Close(Index As Integer)
        sckServer(Index).Close '关闭此套接字
        '以下处理定时器及全局计数
        If sckTimer(Index).Tag = 1 Then
            sckTimer(Index).Enabled = False
            m_tmCount = m_tmCount - 1
            sckTimer(Index).Tag = ""
        End If
    End Sub'处理来自客户端的数据
    Private Sub sckServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
        Dim strData As String
        
        sckServer(Index).GetData strData, vbString
        If strData = "Normal network" Then '收到客户端关于检测连接情况的应答
            '以下处理定时器及全局计数
            If sckTimer(Index).Tag = 1 Then
                sckTimer(Index).Enabled = False
                m_tmCount = m_tmCount - 1
                sckTimer(Index).Tag = ""
            End If
        Else '这是你自己需要处理的数据
            '......
        End If
    End Sub'------------------------------------------------------
    '自定义函数,检测套接字的连接状况,楼主调用此函数即可检测连接情况
    '------------------------------------------------------
    Private Sub CheckConnect()
        Dim i As Long
        
        '遍历套接字数组,对处于连接状态的发送检测请求
        For i = 0 To sckServer.UBound
            If sckServer(i).State <> sckClosed Then
                m_tmCount = m_tmCount + 1 '全局计数器加1
                sckServer(i).SendData "Network Status Inquiry" '发送检测数据
                sckTimer(i).Tag = 1
                sckTimer(i).Enabled = True '启动超时检测
            End If
        Next
        '等待定时器数组完成检测
        Do While m_tmCount > 0
            DoEvents
        Loop
        '再一次遍历套接字数组,获得连接状况
        For i = 0 To sckServer.UBound
            If sckServer(i).State = sckClosed Then '已关闭或断开
                Debug.Print "客户端" & sckServer(i).RemoteHostIP & "-" & sckServer(i).RemotePort & "已断开。"
            Else '在线
                Debug.Print "客户端" & sckServer(i).RemoteHostIP & "-" & sckServer(i).RemotePort & "在线。"
            End If
        Next
    End SubPrivate Sub scktimer_Timer(Index As Integer)
        '以下处理定时器及全局计数
        sckTimer(Index).Enabled = False
        If sckTimer(Index).Tag = 1 Then
            m_tmCount = m_tmCount - 1
            sckTimer(Index).Tag = ""
        End If
        sckServer(Index).Close '检测超时,关闭此套接字
    End Sub