将更新的客户列表发送给所有连接的客户

时间:2013-08-31 17:05:55

标签: vb6 winsock

好吧,我有这个代码用于我的服务器连接请求。

If sckClients(clientCounter).State <> adstateclosed Then sckClients(clientCounter).Close        
sckClients(clientCounter).Accept requestID
Command1.Enabled = True
MsgBox "Connected to client."
Combo1.AddItem sckClients(clientCounter).LocalIP & "(" & clientCounter & ")"
clientCounter = clientCounter + 1
Load sckClients(clientCounter)

这是为了更新客户列表。

StrMsg = "1~Server~"
For x = 0 To Combo1.ListCount - 1
    If x = Combo1.ListCount - 1 Then
        StrMsg = StrMsg & Combo1.List(x)
    Else
        StrMsg = StrMsg & Combo1.List(x) & "~"
    End If
Next x


For x = 0 To clientCounter - 1
    sckClients(x).SendData StrMsg
Next x

我的问题是,如何将其发送到每个客户端的组合框客户端列表?

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的代码(并且基于Bob77的澄清),您发送给连接客户端的消息可能是这样的:

"1~Server~192.168.0.101(0)~69.125.83.76(1)~127.0.0.1(2)"

然后你只需要使用VB6的split函数来获取信息:

On Client(伪代码):

Private Sub WinSock_OnData(...)
    Dim Buffer As String
    Dim BufferArray() As String
    Dim Index As Long

    Call WinSock.GetData(Buffer, ...)        
    BufferArray = Split(Buffer, "~")

    If BufferArray(0) = "1" Then
        Call ComboxBox1.Clear        
        For Index = 1 To Ubound(BufferArray)
            Call ComboxBox1.AddItem(BufferArray(Index))
        Next Index
    End If

End Sub