VB 实时错误'10054'连接由远程端重新设置
代码如下:
Private Sub cmdFs_Click()
If XZ = 1 Then
If txtFs.Text = "" Then
MsgBox "请输入信息......", vbCritical, "提示"
Else
On Error Resume Next
Wsk1.SendData txtFs.Text
txtJs.Text = vbCrLf & txtJs.Text & "我说:" & txtFs.Text
txtFs.Text = ""
If Err Then
End ' handle the error here
End If
End If
Else
Wsk2.RemoteHost = frmJm.txtIP.Text
Wsk2.RemotePort = frmJm.txtYDK.Text
'Wsk2.Bind Wsk2.LocalPort
If txtFs.Text = "" Then
MsgBox "请输入信息......", vbCritical, "提示"
Else
On Error Resume Next
Wsk2.SendData txtFs.Text
txtJs.Text = txtJs.Text + vbCrLf + CStr(Date) + Chr(32) + CStr(Time$()) + vbCrLf & TxtNc & ":" & txtFs.Text
txtFs.Text = ""
If Err Then
End ' handle the error here
End If
End If
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
If sckconnect.State <> sckClosed Then
sckconnect.Close
End If
End Sub
Private Sub FWD_Click()
XZ = 1
frmJm.Show
frmJm.Caption = "服务端连接"
frmJm.Label1.Caption = "服务端IP"
frmJm.Label2.Visible = False
frmJm.txtYDK.Visible = False
Wsk1.Protocol = sckTCPProtocol
End Sub
Private Sub KHD_Click()
XZ = 2
frmJm.Show
frmJm.Caption = "客户端连接"
frmJm.Label1.Caption = "服务端IP"
Wsk2.Protocol = sckUDPProtocol
End Sub
Private Sub TC_Click()
If Wsk1.State <> sckClosed Then
Wsk1.Close
Wsk1.Accept requestID
End If
End
End Sub
Private Sub Timer1_Timer()
Select Case Wsk1.State
Case 0
Label1.Caption = "关闭"
Case 1
Label1.Caption = "打开"
Case 2
Label1.Caption = "侦听"
Case 5
Label1.Caption = "以识别主机"
Case 6
Label1.Caption = "正在连接 "
Case 7
Label1.Caption = "已连接"
Case 9
Label1.Caption = "错误"
End Select
End Sub
Private Sub wsk1_ConnectionRequest(ByVal requestID As Long)
If Wsk1.State <> sckClosed Then
Wsk1.Close
Wsk1.Accept requestID
End If
End Sub
Private Sub wsk1_DataArrival(ByVal bytesTotal As Long)
Dim Js As String
Wsk1.GetData Js, vbString
txtJs.Text = txtJs.Text & "对方说:" & Js & vbCrLf
End Sub
Private Sub wsk2_DataArrival(ByVal bytesTotal As Long)
Dim Js As String
Wsk2.GetData Js, vbString
txtJs.Text = txtJs.Text & "对方说:" & Js & vbCrLf
End Sub
--------------
程序图片:
--------------------------------------------------------------------------
搞不清楚到底是在那里错拉 老是在发送信息那里卡住,连接没有问题 很晕
[解决办法]
注意你的协议 如果采用tcp 就不要udp
Wsk1.Protocol = sckTCPProtocol
Wsk2.Protocol = sckUDPProtocol
[解决办法]
楼主代码有问题,我估计你想用UDP协议,但又把用TCP协议时的代码copy 过来了,如果你是想用UDP协议,那么发生10054错误出现在 Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
里面的这一句
Winsock.GetData sRecv
在前面加上 On Error Resume Next 就好了
本来UDP协议是无连接协议,按理说不应该出现“关闭连接。。。”之类的错,但是微软说了,这个一个bug,是因为ICMP导致的