我已经在不同的论坛上搜索了很长时间,如果可以使用winsock将文本发送到不同的IP,但我找不到如何。 我尝试以相同的形式放置两个不同的winsock,但第二个winsock的消息永远不会到来。 这是我的代码:
Private Sub Form_Load()
On Error Resume Next
a = bytMsg()
wsck.RemoteHost = strNombreATMMonitor2
wsck.RemotePort = vPuertoATMMonitor2
wsck.Connect
'Conectamos el segundo
Winsock1.RemoteHost = strNombreATMMonitor
Winsock1.RemotePort = vPuertoATMMonitor
Winsock1.Connect
End Sub
Private Sub wsck_Close()
On Error Resume Next
wsck.Close
End
End Sub
Private Sub wsck_Connect()
On Error GoTo Err_wsck_Connect
wsck.SendData a
End_wsck_Connect:
Exit Sub
Err_wsck_Connect:
GrabaLog "wsck_Connect", Err.Description, " " & Err.Number & " Host: " & strNombreATMMonitor
Resume End_wsck_Connect
End Sub
Private Sub wsck_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)
'si no es un recordatorio, lo grabo
If bytRecordatorio = 0 Then
GrabaOff strLineaComandos
End If
GrabaLog "wsck_Error", "RemoteHost" & " - " & wsck.RemoteHost & " - RemotePort - " & Trim(wsck.RemotePort), "Error = " & Trim(Number) & " - " & Description
End
End Sub
Private Sub wsck_SendComplete()
On Error GoTo Err_wsck_SendComplete
wsck.Close
'Graba en un log el mensaje de NCR pasado como parametro a ATMSpyNEW + FTP
TrazaLog "ATMMonitor1 - " & strLineaComandos
End_wsck_SendComplete:
End
Err_wsck_SendComplete:
GrabaLog "wsck_SendComplete", Err.Description, "" & Err.Number
Resume End_wsck_SendComplete
End Sub
Private Sub Winsock1_Close()
On Error Resume Next
Winsock1.Close
End
End Sub
Private Sub Winsock1_Connect()
On Error GoTo Err_Winsock1_Connect
Winsock1.SendData a
End_Winsock1_Connect:
Exit Sub
Err_Winsock1_Connect:
GrabaLog "Winsock1_Connect", Err.Description, " " & Err.Number & " Host: " & strNombreATMMonitor
Resume End_Winsock1_Connect
End Sub
Private Sub Winsock1_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)
'si no es un recordatorio, lo grabo
If bytRecordatorio = 0 Then
GrabaOff strLineaComandos
End If
GrabaLog "Winsock1_Error", "RemoteHost" & " - " & Winsock1.RemoteHost & " - RemotePort - " & Trim(Winsock1.RemotePort), "Error = " & Trim(Number) & " - " & Description
End
End Sub
有人知道如何使用一个或两个winsock控件向至少两个不同的Ip(RemoteHost)发送一段文本吗?
答案 0 :(得分:1)
我创建了一个新的VB6项目,并在Form1上添加了两个Winsock控件。即Winsock1和Winsock2。然后我添加了两个TextBox(仅用于测试显示Winsocks的输入和输出)并将它们设置为允许MultiLines。我添加了一个按钮(cmdSendData),它将数据发送到两个winsocks。在Form_Load事件中,我将这两个Winsocks连接到我已经访问过的公共TelNet服务器(您需要将IP地址更改为可以测试的内容)。我能够通过两个Winsocks发送数据(即使它们连接到同一台机器,它们的行为是分开的,如果我知道我有另一个IP等待连接请求,它们可以轻松地将其中一个的IP更改为另一个)。
我跳过错误处理,因为我相信你可以弄清楚放在哪里。
Private Sub Form_Load()
Me.Winsock1.RemoteHost = "10.11.27.87"
Me.Winsock1.RemotePort = "23"
Me.Winsock1.Connect
Me.Winsock2.RemoteHost = "10.11.27.87"
Me.Winsock2.RemotePort = "23"
Me.Winsock2.Connect
End Sub
Private Sub cmdSendData_Click()
Me.Winsock1.SendData "admin" & vbCrLf
DoEvents 'This is necessary to immediatelly send the data without waiting for
' a certain buffer to be filled first
Me.Winsock2.SendData "admin" & vbCrLf
DoEvents 'same as above
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim txt As String
Me.Winsock1.GetData txt
Me.Text1.Text = Me.Text1.Text & vbCrLf & txt
End Sub
Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
Dim txt As String
Me.Winsock2.GetData txt
Me.Text2.Text = Me.Text2.Text & vbCrLf & txt
End Sub