VB中WinSock控件的问题

2024-12-26 06:11:45
推荐回答(3个)
回答1:

winsock1.RemoteHost="127.0.0.1" 或者是winsock1.RemoteHost=localhost"
记得加引号就是了

回答2:

Winsock通讯应该有2个程序来完成,你留个信箱,我明天发给你个例子

回答3:

我给你一个例子吧
不能发附件郁闷

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

具体放那些控件你自己根据程序自己看着办吧呵呵