Const NUM As Integer = 20
Const BUSY As Boolean = False
Const FREE As Boolean = True
Dim ConnectState(NUM) '用于标识对应id号的sock是否空闲
Private Sub Command1_Click()
On Error Resume Next
MsgBox Winsock1.Count 在这里加篱msgbox 就会全部九哪。不然只会发到最后一个上线的用户!
For j = 1 To Winsock1.Count
If Winsock1(j).State <> sckClosed Then
Winsock1(j).SendData Text1.Text
End If
Next j
End Sub
Private Sub Command2_Click()
On Error Resume Next
Winsock1(Label4).SendData Text1.Text
End Sub
Private Sub Form_Load()
Dim i As Integer
Winsock1(0).LocalPort = 5000
Winsock1(0).Listen
For i = 0 To NUM
ConnectState(i) = FREE
Next i
End Sub
Private Sub List2_Click()
Label3.Caption = "发送给" & List2.Text
List3.ListIndex = List2.ListIndex
Label4.Caption = List3.Text
End Sub
Private Sub Winsock1_Close(Index As Integer)
For i = 0 To List1.ListCount - 1
If List1.List(i) = Winsock1(Index).RemoteHostIP Then
List1.RemoveItem (i)
List2.RemoveItem (i)
List3.RemoveItem (i)
End If
Next i
Public Function FindFreeSock() '寻找空闲的sock
Dim i As Integer
For i = 1 To NUM
If ConnectState(i) = FREE Then
FindFreeSock = i
If i > maxnum Then
maxnum = i
End If
Exit Function
End If
Next i
FindFreeSock = -1
End Function
Public Function SendClientTable() '向所有客服端发送在线客服id号
Dim j, k As Integer
For j = 1 To NUM
If ConnectState(j) = BUSY Then
For k = 1 To NUM
If ConnectState(k) = BUSY Then
Winsock1(j).SendData Str(k)
End If
Next k
End If
Next j
End Function
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim SckData As String
Winsock1(Index).GetData SckData
If Mid(SckData, 1, 3) = "^^#" Then
List2.AddItem Mid(SckData, 4, Len(SckData) - 3)
Label1.Caption = Index
List3.AddItem Label1.Caption
Text2 = Text2.Text & SckData & vbNewLine
Else
Text3.Text = Text3.Text & SckData & vbNewLine
End If
End Sub 作者:
Nothing 时间: 2012-5-16 00:44