0

如果可以使用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)?

4

1 回答 1

1

我创建了一个新的 VB6 项目并在 Form1 上添加了两个 Winsock 控件。即 Winsock1 和 Winsock2。然后我添加了两个文本框(仅用于测试以显示来自 Winsocks 的输入和输出)并将它们设置为允许多行。我添加了一个按钮(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

在此处输入图像描述

于 2012-11-03T10:19:14.580 回答