Board logo

标题: [网络] VB 使用WINSOCK1群的的问题 [打印本页]

作者: xtahtygyqq    时间: 2012-5-15 10:41     标题: VB 使用WINSOCK1群的的问题

Dim uh As Integer

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






Winsock1(Index).Close
Unload Winsock1(Index)
ConnectState(Index) = FREE

Print "完成sock(" & Index & ")卸载"

Call SendClientTable   '就是这里发送的数据有些丢失了

End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Index = FindFreeSock    '寻找空闲的sock

If Index <> -1 Then
    Load Winsock1(Index)    '如果有空闲的sock,就加载sock

End If

Winsock1(Index).Accept requestID
ConnectState(Index) = BUSY
   List1.AddItem Winsock1(Index).RemoteHostIP
   
   

Call SendClientTable    '这里发送的所有客服端都可以接到
   
End Sub

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

For i = 0 To Winsock1.Count - 1
应该是这样




欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/) Powered by Discuz! 6.0.0