winsock多IP发送数据vb6

Posted

技术标签:

【中文标题】winsock多IP发送数据vb6【英文标题】:winsock multiple IP senddata vb6 【发布时间】:2012-10-31 06:58:38 【问题描述】:

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

【问题讨论】:

你肯定可以同时使用多个winsock对象。您确定有主机在监听第二个连接吗? 【参考方案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

【讨论】:

非常感谢!!我将尝试您的代码...但是在您的示例中,remoteHost Ip 在两个示例中都是“10.11.27.87”,更改它的结果是否相同? 是的。无论是相同的IP还是不同的IP都没有关系。只要你有两个独立的连接就可以完成这项工作

以上是关于winsock多IP发送数据vb6的主要内容,如果未能解决你的问题,请参考以下文章

VB6:如何获取通过 winsock 发送的字节数?

vb6 - 将消息发送到winsock服务器聊天

使用 vb6 winsock 发送 python 字符串

VB6 应用程序通过 winsock 发送 UDP 广播消息 - 仅每秒发送一次消息

VB6 Winsock 能够发送非常大的有效载荷

VB6 中的 Winsock 控件只向数组中的最后一个索引发送文本消息