0
votes

I have searching for a long time in different forums if it's possible to send a text to differents IP's using winsock but I can't find how. I have try putting two different winsocks in the same form but the message of the second winsock never arrives. This is my code:

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

Does anybody knows how to send a piece of text to, at least, two different Ip's (RemoteHost) using one or two winsock controls?

1
You can definately have multiple winsock objects in use at the same time. Are you sure you have a host listening for the second connection?tcarvin

1 Answers

1
votes

I created a new VB6 project and added two Winsock controls onto the Form1. Namely Winsock1 and Winsock2. I then added two TextBoxes (for testing only to display input and output from the Winsocks) and set them to allow MultiLines. I added one button (cmdSendData) that will send data to both winsocks. In the Form_Load event, I made these two Winsocks connect to a common TelNet server that I already have access to (you will need to change the IP address to something you can test with). I was able to send data through both Winsocks (even though they are connected to the same machine, they behave separately and could easily change the IP of one of them to another if I knew that I have another IP waiting for a connection request).

I skipped the Error Handling since I am sure you can figure out where to put that.

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

enter image description here