给你一个思路先:
1 必须有一个服务器端,使用一个固定IP和端口侦听。当他接收到一个连接请求后,保存对方的IP和主机名,并建立一个Winsock回应对方,发回用户列表。
2 一个客户端启动后,先登录服务,取得用户列表。每个客户端必须有一个用于侦听其它客户的Winsocj。它可以利用客户列表的IP,向其它用户请求连接。
3 每个客户端必须有一个用于侦听其它客户的Winsock。每接到一个其它客户的连接请求,建立一个新的Winsock(数组)。当发生接到数据事件时,根据Index判断是哪个客户。
4 客户退出时,向服务端发出注销消息。服务端清除用户列表记录。此外,服务端定时向客户端轮询,如果没有应答,删除其用户列表记录。

解决方案 »

  1.   

    Private Sub sckServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
        Dim Sip As String
        Dim I As Integer
        
        Sip = sckServer(0).RemoteHostIP         '获得登陆者的IP地址
        I = 1
        Do While I <= sckServer.UBound          '检查是否已经有该地址的记录
            If sckServer(I).RemoteHostIP = Sip Then   '如有,不必加载新的控件
                sckServer(I).Close
                sckServer(I).Accept requestID
                sckServer(I).SendData "SUCCESS"
                Exit Sub
            End If
            I = I + 1
        Loop
        Load sckServer(I)                     '否则,加载新的控件
        sckServer(I).Accept requestID
        sckServer(I).SendData "SUCCESS"
    End SubPrivate Sub Form_Load()
        
        '开一个连接
        sckServer(0).RemotePort = 1200
        sckServer(0).Bind sckServer(0).LocalPort
        sckServer(0).Listen      '监听End Sub
    Private Sub sckServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)Dim sData As String
        sckServer(Index).GetData sData, vbString        '得到数据End Sub
      

  2.   

    服务器程序里加入sckServer(0)的winsock控件index为0
      

  3.   

    '*********************************************************
    '* 名称:服务器端加密狗程序
    '* 功能:控制客户端点数,将加密狗中的连接字符串返加客户端
    '* 日期:11-05-2002
    '*********************************************************Dim intComputerNum As Integer       '定义机器名
    Dim strConnectString As String      '定义连接字符串
    Dim strEthernetAddress As String    '定义网卡地址Dim objCn As New coLCn              '定义连接对象Private Sub Form_Load()
        Dim strForm As String
        strForm = FormSet(Me, 5)
        
        '开一个连接
        sckServer(0).RemotePort = 1200
        sckServer(0).Bind sckServer(0).LocalPort
        sckServer(0).Listen      '监听
        Call GetData             '调用过程,从狗中得到数据
        '初始化网格
        Call InitMsHFlexGrid
    End SubPrivate Sub Form_Unload(Cancel As Integer)
        Dim I As Integer
        If MsgBox("你是否要退出本系统 ?", 4 + 32 + 256, "通用管理软件") = vbYes Then
            I = 1
            Do While I <= sckServer.UBound
                If Not (sckServer(I) Is Nothing) Then
                    If sckServer(I).State <> sckClosed Then
                        sckServer(I).SendData "END"
                    End If
                End If
                I = I + 1
            Loop
            Cancel = False
            End
        Else
            Cancel = True
        End If
    End SubPrivate Sub mnuDisconnect_Click()
    '断开连接
        If MsgBox("你是否要断开连接 ?", 4 + 32 + 256, "通用管理软件") = vbYes Then
            MSHFlexGrid1.Col = 0
            sckServer(MSHFlexGrid1.Text).SendData "END"
        End If
    End SubPrivate Sub mnuExit_Click()
        Unload Me
    End SubPrivate Sub MSHFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
        '右键弹出菜单
        If Button = 2 Then
            PopupMenu mnuPop
        End If
    End SubPrivate Sub sckServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
        Dim Sip As String
        Dim I As Integer
        
        Sip = sckServer(0).RemoteHostIP         '获得登陆者的IP地址
        I = 1
        Do While I <= sckServer.UBound          '检查是否已经有该地址的记录
            If sckServer(I).RemoteHostIP = Sip Then   '如有,不必加载新的控件
                sckServer(I).Close
                sckServer(I).Accept requestID
                sckServer(I).SendData strConnectString
                Exit Sub
            End If
            I = I + 1
        Loop
        Load sckServer(I)                     '否则,加载新的控件
        sckServer(I).Accept requestID
        sckServer(I).SendData strConnectString  '发送连接串到客户端
    End SubPrivate Sub sckServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)    Dim strStatus As mStatus
        Dim sData As String
        sckServer(Index).GetData sData, vbString        '得到数据
        
    '    If sData = "GetConnect" Then
    '        sckServer(Index).SendData strConnectString  '发送连接串到客户端
       ' Else
            For I = 1 To objCn.Count
                If objCn(I).proIp = sckServer(Index).RemoteHostIP Then
                    Exit Sub
                End If
            Next
            
            '添加到对象中
            objCn.Add "key" & Index, sData, sckServer(Index).RemoteHostIP, "连接"
            
            Call mshData
            sckServer(Index).SendData strConnectString
       ' End If
           
    End SubPrivate Sub GetData()
        '*********得到加密狗中的数据*********************
        Dim strGet As String
        strGet = Left(Trim(ReadDog(0, 200)), 120)
        intComputerNum = Trim(Mid$(strGet, 13, 4))         '得到加密狗中的机器名
        strConnectString = Trim(Mid$(strGet, 17, 100))     '得到加密狗中的连接串
        strEthernetAddress = Mid$(strGet, 1, 12)           '得到加密狗中的网卡地址
    End SubPrivate Sub mshData()
        Dim I As Integer
        If objCn.Count = 0 Then    '如果没有机器连接,则清空数据
            Call InitMsHFlexGrid
        Else
            '将连接对象中的所有数据加入到网格中
            MSHFlexGrid1.Rows = objCn.Count + 1
            For I = 1 To objCn.Count
                With MSHFlexGrid1
                    .Row = I                     '机器名
                    .Col = 0
                    .Text = Right(objCn.Item(I).key, Len(objCn.Item(I).key) - 3)
                    .Col = 1
                    .Text = objCn.Item(I).proComputerName
                    .Row = I                     '机器IP
                    .Col = 2
                    .Text = objCn.Item(I).proIp
                    .Row = I                     '机器状态
                    .Col = 3
                    .Text = objCn.Item(I).proStatus
                End With
            Next
        End If
    End SubPrivate Sub Timer1_Timer()
    'On Error GoTo errHandler
        Dim I As Integer
        Dim intItem As Integer
        I = 1
        Do While I <= sckServer.UBound
            'If Not (sckServer(I) Is Nothing) Then
                If Int(sckServer(I).State) <> 7 Then        '如果连接断开,则清除对象中的数据
                    For intItem = 1 To objCn.Count
                        If intItem > objCn.Count Then
                            Exit For
                        End If
                        If objCn(intItem).key = "key" & I Then
                            objCn.Remove ("key" & I)
                           
                            Call mshData
                            sckServer(I).Close
                        End If
                    Next
                End If
            'End If
            I = I + 1
        Loop
        Exit Sub
    End SubPrivate Sub InitMsHFlexGrid()    '初始化网格
        With MSHFlexGrid1
            .Clear
            .Cols = 4
            .ColWidth(0) = 500
            .ColWidth(1) = 2500
            .ColWidth(2) = 2500
            .ColWidth(3) = 2000
            .Col = 0
            .Row = 0
            .Text = "ID"
            .Col = 1
            .Text = "机器名"
            .Col = 2
            .Text = "IP地址"
            .Col = 3
            .Text = "机器状态"
        End With
        
        '初始化网格
        With MSHFlexGrid1
            .Redraw = False                        '禁止重画,防止闪动
            .BackColorFixed = RGB(230, 250, 230)   '标题行背景色
            .Row = 0                               '设置标题行文字的对齐方式
            For I = 0 To .Cols - 1
                .Col = I
                .ColSel = .Cols - 1
                .CellBackColor = RGB(0, 128, 255)
                .CellForeColor = RGB(255, 255, 0)      '标题行文字颜色
                .CellAlignment = 4                     '对其方式为中间
            Next
            .ScrollTrack = True                    '移动滚动条的同时数据也滚动
            .Row = .Rows - 1
            .Col = .FixedCols
            .Redraw = True                         '允许重画,显示表格
        End With
    End Sub