0
votes

I've a simple winsock server chat and this is the code:

Private Sub Form_Load()
Winsock1.LocalPort = 5100
Winsock1.Listen
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim sData As String
Winsock1.GetData sData
Text1.Text = Text1.Text & sData
End Sub

I receive the message from an ios app but I can't send message with a "sendData".

How can I send message to all the clients? I need to use a client?

Thanks.

1

1 Answers

1
votes

If you want to send messages to more than one client, then the best approach would be instead of closing your listening winsock1, and using it to accept the request, to create a new winsock control that will accept the request. This way you can accept connections from more than one source.

Example:

1st change winsock1's property Index to 0, to create a control array. Now all events's signature change to include the Index parameter.

Dim NumSockets As Integer

Private Sub Form_Load()
    Winsock1(0).LocalPort = 5100
    Winsock1(0).Listen
End Sub

Private Sub Winsock1_Close(Index As Integer)
    Winsock1(Index).Close
End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    NumSockets = NumSockets + 1
    Load Winsock1(NumSockets)                'create a new winsock control
    Winsock1(NumSockets).Accept requestID    'use that one to accept the request
End Sub

Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim vtData As String
    Winsock1(Index).GetData vtData, vbString
    Print vtData
End Sub