winsock连接的问题
'服务端Option Explicit
Const BUSY As Boolean = False
Const FREE As Boolean = True
Dim ConnectState() As Boolean
Private Sub Form_Load()
ReDim Preserve ConnectState(0 To 1)
On Error Resume Next
ConnectState(0) = FREE
ConnectState(1) = FREE
Listener.LocalPort = 9003
Listener.Listen
End Sub
Private Sub Listener_ConnectionRequest(ByVal requestID As Long)
Dim SockIndex As Integer
Dim SockNum As Integer
On Error Resume Next
SockNum = UBound(ConnectState)
If SockNum > 14 Then
Exit Sub
End If
SockIndex = FindFreeSocket()
If SockIndex > SockNum Then
Load Sock(SockIndex)
End If
ConnectState(SockIndex) = BUSY
Sock(SockIndex).Tag = SockIndex
Sock(SockIndex).Accept (requestID)
List1.AddItem Sock(0).RemoteHostIP
End Sub
Private Sub Sock_Close(Index As Integer)
If Sock(Index).State <> sckClosed Then
Sock(Index).Close
End If
ConnectState(Index) = FREE
List1.RemoveItem Index
End Sub
Private Sub Sock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim dx As String
Sock(Index).GetData dx, vbDouble
End Sub
Public Function FindFreeSocket()
Dim SockCount, i As Integer
SockCount = UBound(ConnectState)
For i = 0 To SockCount
If ConnectState(i) = FREE Then
FindFreeSocket = i
Exit Function
End If
Next i
ReDim Preserve ConnectState(0 To SockCount + 1)
FindFreeSocket = UBound(ConnectState)
End Function
'客户端
Option Explicit
'发送数据
Private Sub command1_Click()
Dim dx As String
dx = Text1.Text
sock.SendData dx
Print "发送成功"
End Sub
Private Sub Form_Load()
'远程主机名
sock.RemoteHost = "127.0.0.1"
'网络端口
sock.RemotePort = 9003
'发出连接命令
sock.Connect
Command1.Enabled = False
End Sub
'服务器关闭
Private Sub sock_Close()
Label1.Caption = "断开连接"
sock.Close
Call Form_Load
End Sub
'连接成功
Private Sub sock_Connect()
Label1.Caption = "连接成功"
Command1.Enabled = True
End Sub
Private Sub Timer1_Timer()
If sock.State = sckError Then
Label1.Caption = "连接失败"
sock.Close
Call Form_Load
Else
'Timer1.Enabled = False
End If
End Sub
当我同时打开2个客户端的时候 服务端中list1中出现了3个IP,而关闭客户端的时候list移除了却是2个,这是什么原因?哪位朋友帮帮我,谢谢 --------------------编程问答-------------------- 自己顶 --------------------编程问答-------------------- 怎么没有回答??
补充:VB , 网络编程