winsock控件数组的问题
在vb中通过winsock控件数组通讯,希望在wisock控件超过3个后就从头开始使用第一个,可是每次超过3再建立连接时都提示我对象已加载,我每次都已经unload了啊,应该怎样才对啊!?求指点!
服务器程序:
Dim intmax As Integer
Private Sub cmdC1_Click()
frmC1.Show
End Sub
Private Sub cmdC2_Click()
frmC2.Show
End Sub
Private Sub Form_Load()
intmax = 0
Winsock(0).LocalPort = 1001
Winsock(0).Listen
End Sub
Private Sub Timer1_Timer()
Text1.Text = intmax
Text2.Text = Winsock.Count
End Sub
Private Sub Winsock_Close(index As Integer)
Unload Winsock(index)
End Sub
Private Sub Winsock_ConnectionRequest(index As Integer, ByVal requestID As Long)
If intmax = 0 Then
intmax = 1
End If
If intmax >= 3 Then
intmax = 1
End If
intmax = intmax + 1
Load Winsock(intmax)
Winsock(intmax).Accept requestID
MsgBox ("Link is made")
End Sub
Private Sub Winsock_DataArrival(index As Integer, ByVal bytesTotal As Long)
Dim s As String
Winsock(index).GetData s
If s = "!C" Then
Winsock(index).Close
End If
End Sub
客户端程序:
Private Sub Command1_Click()
Winsock.Connect
End Sub
Private Sub Command3_Click()
Winsock.SendData "!C"
End Sub
Private Sub Form_Load()
Label1.Caption = "等待建立连接"
Winsock.RemoteHost = "127.0.0.1"
Winsock.RemotePort = 1001
End Sub
Private Sub Winsock_Close()
Winsock.Close
Label1.Caption = "连接已断开"
End Sub
Private Sub Winsock_Connect()
Label1.Caption = "已经建立连接"
End Sub
[解决办法]
抱歉我说的有点问题
Private Sub Winsock_Close(index As Integer)
此事件是当远程计算机关闭连接时出现。应用程序应正确使用 Close 方法关闭 TCP 连接。
也就上说客户端里关闭连接服务器里才会触发此事件或者说服务器里关闭连接客户端里才会触发此事件。
你要确定的是以下事件已经触发,也就是说你的控件已经卸载:
Private Sub Winsock_Close(index As Integer)
Unload Winsock(index)
End Sub