我以前也遇到过这种情况,我的解决方法是:在客户端设定服务器的地址,而在服务器端不设定客户端地址,只有监听端口号.这样在连接断开后就可以继续连接.怕说的不清楚,给你代码吧 Dim BLNFLAG As Boolean Private Sub cmdquit_Click() Winsock1.Close Label1.Caption = "" Text1.Text = "" End SubPrivate Sub cmdsend_Click() Winsock1.SendData (Text2.Text) End SubPrivate Sub cmdTCP_Click() BLNFLAG = False Call CconnTCPIP Debug.Print 11111 If BLNFLAG Then MsgBox 33 End If End SubPrivate Sub Winsock1_Close() Winsock1.Close Label2.Caption = "CLOSEING" End SubPrivate Sub Winsock1_Connect() Select Case Winsock1.State Case 6 Label1.Caption = "CONNECTING" Case 7 Label1.Caption = "CONNECTED" Case 9 Label1.Caption = "ERROR" End Select Debug.Print 2222 BLNFLAG = True End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim temp As String Winsock1.GetData temp, vbString Debug.Print "temp=" & temp If StrComp(temp, "RBN", 0) = 0 Then BLNFLAG = True End If End SubPrivate Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) Text1.Text = Number & "//////" & Description End SubPrivate Sub CconnTCPIP() Winsock1.RemotePort = 1024 Winsock1.RemoteHost = "192.168.1.130" Winsock1.Connect End Sub另一端 Dim i As Long Dim J As LongPrivate Sub Form_Load() Dim i As Long Dim strData As String If Winsock1.State <> sckClosed Then Winsock1.Close End If Winsock1.LocalPort = 1024 Winsock1.Listen End SubPrivate Sub Winsock1_Close() Label1.Caption = "CLOSED" Winsock1.Close Winsock1.Listen End SubPrivate Sub Winsock1_ConnectionRequest(ByVal requestID As Long) i = i + 1 If Winsock1.State <> sckClosed Then Winsock1.Close End If Winsock1.Accept requestID Label2.Caption = "CONNECTIONGREQUEST 第" & i & "次" End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim strData As String Winsock1.GetData strData, vbString If StrComp(strData, "DCN", 0) = 0 Then Winsock1.Close Label1.Caption = strData & "**" & i Winsock1.Listen End If Call ResponceFlame End SubPrivate Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) MsgBox "22"
End SubPrivate Sub ResponceFlame() On Error Resume Next Dim i As Long Dim strData As String Dim MessageBinary(1 To 7) As Byte MessageBinary(1) = 0 MessageBinary(2) = 0 MessageBinary(3) = 0 MessageBinary(4) = &HC2 MessageBinary(5) = 0 MessageBinary(6) = &HCC MessageBinary(7) = &HEF Winsock1.SendData MessageBinary Debug.Print "Responce flame" & " " & J J = J + 1 End Sub
我研究Winsock好几天了,虽然有无数的疑问,但楼主的问题我可以避免。首先按照1楼的提示,不要接收一次就关闭连接。 然后,你的客户端如果关闭了连接,就会触发服务端的Close事件。在服务端的Colse事件中,一定要关闭服务端的Winsock。另外,发送之前一定要确认连接状态,客户端Close了服务器端可能还没有关闭,这时的State可能不是7了,所以SendData就会发生“所需事物或请求的错误协议或连接状态”错误。 改成这样: If myWinSock.State = sckConnected Then myWinSock.SendData DoEvents End If
只需要在服务器端加上如下代码就可以了。(注意,必须是在Winsock1_Close()事件里加.) Private Sub Winsock1_Close() Winsock1.Close Winsock1.Listen End Sub
然后在客户端要关闭前发送一个标志给服务端,服务端根据这个标志来关闭自己
Dim BLNFLAG As Boolean
Private Sub cmdquit_Click()
Winsock1.Close
Label1.Caption = ""
Text1.Text = ""
End SubPrivate Sub cmdsend_Click()
Winsock1.SendData (Text2.Text)
End SubPrivate Sub cmdTCP_Click()
BLNFLAG = False
Call CconnTCPIP
Debug.Print 11111
If BLNFLAG Then
MsgBox 33
End If
End SubPrivate Sub Winsock1_Close()
Winsock1.Close
Label2.Caption = "CLOSEING"
End SubPrivate Sub Winsock1_Connect()
Select Case Winsock1.State
Case 6
Label1.Caption = "CONNECTING"
Case 7
Label1.Caption = "CONNECTED"
Case 9
Label1.Caption = "ERROR"
End Select
Debug.Print 2222
BLNFLAG = True
End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim temp As String
Winsock1.GetData temp, vbString
Debug.Print "temp=" & temp
If StrComp(temp, "RBN", 0) = 0 Then
BLNFLAG = True
End If
End SubPrivate Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Text1.Text = Number & "//////" & Description
End SubPrivate Sub CconnTCPIP()
Winsock1.RemotePort = 1024
Winsock1.RemoteHost = "192.168.1.130"
Winsock1.Connect
End Sub另一端
Dim i As Long
Dim J As LongPrivate Sub Form_Load()
Dim i As Long
Dim strData As String
If Winsock1.State <> sckClosed Then
Winsock1.Close
End If
Winsock1.LocalPort = 1024
Winsock1.Listen
End SubPrivate Sub Winsock1_Close()
Label1.Caption = "CLOSED"
Winsock1.Close
Winsock1.Listen
End SubPrivate Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
i = i + 1
If Winsock1.State <> sckClosed Then
Winsock1.Close
End If
Winsock1.Accept requestID
Label2.Caption = "CONNECTIONGREQUEST 第" & i & "次"
End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData, vbString
If StrComp(strData, "DCN", 0) = 0 Then
Winsock1.Close
Label1.Caption = strData & "**" & i
Winsock1.Listen
End If
Call ResponceFlame
End SubPrivate Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox "22"
End SubPrivate Sub ResponceFlame()
On Error Resume Next
Dim i As Long
Dim strData As String
Dim MessageBinary(1 To 7) As Byte
MessageBinary(1) = 0
MessageBinary(2) = 0
MessageBinary(3) = 0
MessageBinary(4) = &HC2
MessageBinary(5) = 0
MessageBinary(6) = &HCC
MessageBinary(7) = &HEF
Winsock1.SendData MessageBinary
Debug.Print "Responce flame" & " " & J
J = J + 1
End Sub
然后,你的客户端如果关闭了连接,就会触发服务端的Close事件。在服务端的Colse事件中,一定要关闭服务端的Winsock。另外,发送之前一定要确认连接状态,客户端Close了服务器端可能还没有关闭,这时的State可能不是7了,所以SendData就会发生“所需事物或请求的错误协议或连接状态”错误。
改成这样:
If myWinSock.State = sckConnected Then
myWinSock.SendData
DoEvents
End If
Private Sub Winsock1_Close()
Winsock1.Close
Winsock1.Listen
End Sub