2 ecz00 ecz00 于 2015.07.25 04:52 提问

winsock两个问题,关闭后怎么重新监听?在close事件中不能重新listen?

Private Sub Command1_Click()
sock.Close
End Sub

Private Sub Command2_Click() 'tcp测试
sock.Connect "192.168.200.10", 39963
Debug.Print sock.State
End Sub

'--------------------------------------以上是客户端

'-------------------以下服务器端

Private Sub Command1_Click()
wsk.Close
Debug.Print "sock关闭后:" & wsk.State
wsk.LocalPort = 39963
wsk.Listen
Debug.Print "当前sock:" & wsk.State
End Sub

Private Sub Form_Load()
wsk.LocalPort = 39963
wsk.Listen
edeal:
If Err.Number = 10048 Then
If wsk.State = 1 Then wsk.Close
wsk.LocalPort = 64399
wsk.Listen
End If
Debug.Print "newLocalIP:" & wsk.LocalIP & " " & wsk.LocalPort
End Sub

Private Sub Form_Unload(Cancel As Integer)
End
End Sub

Private Sub Timer1_Timer()
On Error GoTo Errs
wsk.SendData "xtbjc"

Errs:

If Err.Number <> 0 Then
Timer1.Interval = 0
wsk.Close
End If
End Sub

Private Sub wsk_Close()
Debug.Print "产生关闭事件了"
Timer1.Enabled = False
Text1.Text = "客户端断开了!"

'wsk.Listen '在这里不能启动监听?????**********************
End Sub

Private Sub wsk_ConnectionRequest(ByVal requestID As Long)
Debug.Print "产生连接事件了"
If wsk.State <> 0 Then wsk.Close
wsk.Accept requestID
Timer1.Interval = 2000
Text1.Text = "连接成功"
End Sub

1个回答

ecz00
ecz00   2015.07.25 07:27

问题已经解决

wsk.listen前面少了一个wsk.close

正常情况下是可以关闭重新listen再连接的,因为客户端设了localport,导致出问题,没有错误提示,总的说就是TCP连接时,客户端不能设任何loacalport

Csdn user default icon
上传中...
上传图片
插入图片