VB中WinSock控件的问题
我的代码是这样的:PrivateSubcmdConnect_Click()winsock1.ConnectendSubPrivateSubform1_load()wins...
我的代码是这样的:
Private Sub cmdConnect_Click()
winsock1.Connect
end Sub
Private Sub form1_load()
winsock1.RemoteHost=127.0.0.1
winsock1.remoteport=5211
end sub
Private Sub winsock1Connect()
MsgBox "连接成功!"
End Sub
我是连接到本自己的计算机上的
但是当我点击按钮的时候竟然什么反映都没有
再点就出错了
请各位帮下忙
我已经知道不是代码的问题了,因为我发到群里的,他们那边是没问题的,但是我自己又用不了,我根据MDSN里提供的程序来做也不行,如果能帮我把这个问题解决掉就更好了 展开
Private Sub cmdConnect_Click()
winsock1.Connect
end Sub
Private Sub form1_load()
winsock1.RemoteHost=127.0.0.1
winsock1.remoteport=5211
end sub
Private Sub winsock1Connect()
MsgBox "连接成功!"
End Sub
我是连接到本自己的计算机上的
但是当我点击按钮的时候竟然什么反映都没有
再点就出错了
请各位帮下忙
我已经知道不是代码的问题了,因为我发到群里的,他们那边是没问题的,但是我自己又用不了,我根据MDSN里提供的程序来做也不行,如果能帮我把这个问题解决掉就更好了 展开
展开全部
winsock1.RemoteHost="127.0.0.1" 或者是winsock1.RemoteHost=localhost"
记得加引号就是了
记得加引号就是了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Winsock通讯应该有2个程序来完成,你留个信箱,我明天发给你个例子
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我给你一个例子吧
不能发附件郁闷
Client:
Private Sub Form_Load()
Me.Caption = "Client-" & WinsockClient.LocalHostName & "-" & WinsockClient.LocalIP
TxtIP.Text = WinsockClient.LocalIP
If WinsockClient.State <> sckClosed Then WinsockClient.Close
WinsockClient.RemoteHost = Trim(TxtIP.Text)
WinsockClient.RemotePort = Val(TxtPort.Text)
WinsockClient.Connect
Debug.Print WinsockClient.State
TimerSendTimeCount.Interval = 2000
TimerSendTimeCount.Enabled = True
End Sub
Private Sub TimerSendTimeCount_Timer()
If WinsockClient.State = sckConnected Then
WinsockClient.SendData ("Client is ok!")
Debug.Print WinsockClient.State
Exit Sub
End If
If WinsockClient.State <> sckClosed And WinsockClient.State <> 6 Then WinsockClient.Close
WinsockClient.RemoteHost = Trim(TxtIP.Text)
WinsockClient.RemotePort = Val(TxtPort.Text)
WinsockClient.Connect
End Sub
Private Sub WinsockClient_DataArrival(ByVal bytesTotal As Long)
Dim Str As String
WinsockClient.GetData Str
Debug.Print Str
End Sub
Private Sub WinsockClient_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)
If TimerSendTimeCount.Enabled = False Then
TimerSendTimeCount.Interval = 2000
TimerSendTimeCount.Enabled = True
End If
End Sub
Host:
Private Sub Form_Load()
Me.Caption = "Host-" & WinsockHost.LocalHostName & "-" & WinsockHost.LocalIP
If WinsockHost.State <> sckClosed Then WinsockHost.Close
WinsockHost.LocalPort = Val(Me.TxtPort)
WinsockHost.Listen
End Sub
Private Sub WinsockHost_Close()
If WinsockHost.State <> sckClosed Then WinsockHost.Close
WinsockHost.LocalPort = Val(Me.TxtPort)
WinsockHost.Listen
End Sub
Private Sub WinsockHost_ConnectionRequest(ByVal requestID As Long)
If WinsockHost.State <> sckClosed Then WinsockHost.Close
WinsockHost.Accept requestID
End Sub
Private Sub WinsockHost_DataArrival(ByVal bytesTotal As Long)
Dim Str As String
Static n As Long
n = n + 1
If WinsockHost.BytesReceived = Len("Client is ok!") Then
WinsockHost.GetData Str, vbString, 13
TxtGetData.Text = TxtGetData.Text & Str & " " & n & vbCrLf
WinsockHost.SendData ("Host is ok! " & n)
End If
End Sub
Private Sub WinsockHost_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)
If WinsockHost.State <> sckClosed Then WinsockHost.Close
WinsockHost.LocalPort = Val(Me.TxtPort)
WinsockHost.Listen
End Sub
具体放那些控件你自己根据程序自己看着办吧呵呵
不能发附件郁闷
Client:
Private Sub Form_Load()
Me.Caption = "Client-" & WinsockClient.LocalHostName & "-" & WinsockClient.LocalIP
TxtIP.Text = WinsockClient.LocalIP
If WinsockClient.State <> sckClosed Then WinsockClient.Close
WinsockClient.RemoteHost = Trim(TxtIP.Text)
WinsockClient.RemotePort = Val(TxtPort.Text)
WinsockClient.Connect
Debug.Print WinsockClient.State
TimerSendTimeCount.Interval = 2000
TimerSendTimeCount.Enabled = True
End Sub
Private Sub TimerSendTimeCount_Timer()
If WinsockClient.State = sckConnected Then
WinsockClient.SendData ("Client is ok!")
Debug.Print WinsockClient.State
Exit Sub
End If
If WinsockClient.State <> sckClosed And WinsockClient.State <> 6 Then WinsockClient.Close
WinsockClient.RemoteHost = Trim(TxtIP.Text)
WinsockClient.RemotePort = Val(TxtPort.Text)
WinsockClient.Connect
End Sub
Private Sub WinsockClient_DataArrival(ByVal bytesTotal As Long)
Dim Str As String
WinsockClient.GetData Str
Debug.Print Str
End Sub
Private Sub WinsockClient_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)
If TimerSendTimeCount.Enabled = False Then
TimerSendTimeCount.Interval = 2000
TimerSendTimeCount.Enabled = True
End If
End Sub
Host:
Private Sub Form_Load()
Me.Caption = "Host-" & WinsockHost.LocalHostName & "-" & WinsockHost.LocalIP
If WinsockHost.State <> sckClosed Then WinsockHost.Close
WinsockHost.LocalPort = Val(Me.TxtPort)
WinsockHost.Listen
End Sub
Private Sub WinsockHost_Close()
If WinsockHost.State <> sckClosed Then WinsockHost.Close
WinsockHost.LocalPort = Val(Me.TxtPort)
WinsockHost.Listen
End Sub
Private Sub WinsockHost_ConnectionRequest(ByVal requestID As Long)
If WinsockHost.State <> sckClosed Then WinsockHost.Close
WinsockHost.Accept requestID
End Sub
Private Sub WinsockHost_DataArrival(ByVal bytesTotal As Long)
Dim Str As String
Static n As Long
n = n + 1
If WinsockHost.BytesReceived = Len("Client is ok!") Then
WinsockHost.GetData Str, vbString, 13
TxtGetData.Text = TxtGetData.Text & Str & " " & n & vbCrLf
WinsockHost.SendData ("Host is ok! " & n)
End If
End Sub
Private Sub WinsockHost_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)
If WinsockHost.State <> sckClosed Then WinsockHost.Close
WinsockHost.LocalPort = Val(Me.TxtPort)
WinsockHost.Listen
End Sub
具体放那些控件你自己根据程序自己看着办吧呵呵
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询