VB 用winsock建立server与client间的数据传递,连接可以建立,但数据无法发送。
SERVER端程序PrivateSubCommand1_Click()IfWinsock1.State=sckConnectedThenWinsock1.SendData...
SERVER端程序
Private Sub Command1_Click()
If Winsock1.State = sckConnected Then
Winsock1.SendData Text1.Text
Text1.Text = ""
Else
MsgBox "发送失败!"
Winsock1.Close
Winsock1.Listen
End If
End Sub
Private Sub Form_Load()
Winsock1.LocalPort = 6001 '设置侦听端口
Winsock1.Listen '侦听开始
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then
Winsock1.Close
End If
Winsock1.Accept requestID
Label1.Caption = "有客户端连入本主机!"
End Sub
CLINT端程序
Private Sub Timer1_Timer()
Winsock1.Close
Winsock1.Connect
End Sub
Private Sub Winsock1_Close()
If Winsock1.State <> sckClosed Then Winsock1.Close
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then
Winsock1.Close
End If
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim StrDataa As String '收到上位机发来的data
If Yxtjs = True Then
Exit Sub
Else
Winsock1.GetData StrDataa
Text1.Text = StrDataa
Debug.Print StrDataa
Call Yxtjieshou(StrDataa)
End If
End Sub
运行结果:SERVER显示“有客户端连入本主机!” 但发送语句未执行,哪里错了,请高手给予指点。 展开
Private Sub Command1_Click()
If Winsock1.State = sckConnected Then
Winsock1.SendData Text1.Text
Text1.Text = ""
Else
MsgBox "发送失败!"
Winsock1.Close
Winsock1.Listen
End If
End Sub
Private Sub Form_Load()
Winsock1.LocalPort = 6001 '设置侦听端口
Winsock1.Listen '侦听开始
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then
Winsock1.Close
End If
Winsock1.Accept requestID
Label1.Caption = "有客户端连入本主机!"
End Sub
CLINT端程序
Private Sub Timer1_Timer()
Winsock1.Close
Winsock1.Connect
End Sub
Private Sub Winsock1_Close()
If Winsock1.State <> sckClosed Then Winsock1.Close
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then
Winsock1.Close
End If
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim StrDataa As String '收到上位机发来的data
If Yxtjs = True Then
Exit Sub
Else
Winsock1.GetData StrDataa
Text1.Text = StrDataa
Debug.Print StrDataa
Call Yxtjieshou(StrDataa)
End If
End Sub
运行结果:SERVER显示“有客户端连入本主机!” 但发送语句未执行,哪里错了,请高手给予指点。 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询