Const MaxConnect = 8 Dim InUsed(MaxConnect) As BooleanPrivate Sub Form_Load() Form1.Caption = "Server" 'Listener为服务器程序监听控件 Listener.LocalPort = 4477 Listener.Listen End SubPrivate Sub Listener_ConnectionRequest(ByVal requestID As Long) Dim i As Long
'MaxConnect为最大连接数 For i = 1 To MaxConnect 'InUsed(i)数组保存Winsock控件数组的使用情况 If Not InUsed(i) Then InUsed(i) = True Exit For End If Next i
'MaxConnect为最大连接数 If i > MaxConnect Then Exit Sub End If
's(i)为Winsock控件数组 Load S(i) '实际建立连接 S(i).Accept requestID '发送数据 S(i).SendData i End SubPrivate Sub s_Close(Index As Integer) '关闭连接 S(Index).Close
'卸载Winsock控件 Unload S(Index)
InUsed(Index) = False End SubPrivate Sub s_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim str As String
2、index是叔祖的下标
3、用户断开会触发一个事件
4、因为client和一个固定的winsock连接,这样你不就知道了吗
具体的代码查看msdn,直接查找关键字“winsocket控件”,然后选择“使用 Winsock 控件”
里面就有使用控件数组建立多点监听的例子了!
Dim InUsed(MaxConnect) As BooleanPrivate Sub Form_Load()
Form1.Caption = "Server" 'Listener为服务器程序监听控件
Listener.LocalPort = 4477
Listener.Listen
End SubPrivate Sub Listener_ConnectionRequest(ByVal requestID As Long)
Dim i As Long
'MaxConnect为最大连接数
For i = 1 To MaxConnect
'InUsed(i)数组保存Winsock控件数组的使用情况
If Not InUsed(i) Then
InUsed(i) = True
Exit For
End If
Next i
'MaxConnect为最大连接数
If i > MaxConnect Then
Exit Sub
End If
's(i)为Winsock控件数组
Load S(i)
'实际建立连接
S(i).Accept requestID
'发送数据
S(i).SendData i
End SubPrivate Sub s_Close(Index As Integer)
'关闭连接
S(Index).Close
'卸载Winsock控件
Unload S(Index)
InUsed(Index) = False
End SubPrivate Sub s_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim str As String
S(Index).GetData str
Text1.Text = str
End Sub