Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Winsock- problem sending to several workstations

Status
Not open for further replies.

redshadow

Programmer
May 24, 2001
70
PH
Hi all,

Could anyone help me with this? Urgent please.

I have a code in vb utilizing winsock control but it can send to one workstation only, if msgbox is called it now can send data to all workstations.
the code for this is given below:

******************
rsQuestions.Open "SELECT * FROM tblquestions WHERE SetID = 1", db, adoopendynamic, adLockOptimistic
If (Socket1.State = sckConnected) Then
Socket1.SendData "ques:" & rsQuestions![Question]
Socket1.SendData "choa:" & rsQuestions![ChoiceA]
Socket1.SendData "chob:" & rsQuestions![ChoiceB]
Socket1.SendData "choc:" & rsQuestions![ChoiceC]
Socket1.SendData "chod:" & rsQuestions![ChoiceD]
End If
If (Socket2.State = sckConnected) Then
Socket2.SendData "ques:" & rsQuestions![Question]
Socket2.SendData "choa:" & rsQuestions![ChoiceA]
Socket2.SendData "chob:" & rsQuestions![ChoiceB]
Socket2.SendData "choc:" & rsQuestions![ChoiceC]
Socket2.SendData "chod:" & rsQuestions![ChoiceD]
End If
If (Socket3.State = sckConnected) Then
Socket3.SendData "ques:" & rsQuestions![Question]
Socket3.SendData "choa:" & rsQuestions![ChoiceA]
Socket3.SendData "chob:" & rsQuestions![ChoiceB]
Socket3.SendData "choc:" & rsQuestions![ChoiceC]
Socket3.SendData "chod:" & rsQuestions![ChoiceD]
End If
lblQuestion = rsQuestions![Question]
lblChoiceA = rsQuestions![ChoiceA]
lblChoiceB = rsQuestions![ChoiceB]
lblChoiceC = rsQuestions![ChoiceC]
lblChoiceD = rsQuestions![ChoiceD]

***************

it grabs data from a database and sends that data to workstations.

Thanks in advance.
 
Generally, this is done using a Winsock control array. To do this, add a Winsock control to your array and set the index to 0.

Declare a global variable sckMax
Code:
Dim sckMax as Long

In Form_Load:
Code:
sckMax = 0
Winsock1(0).Listen

Then add the following to accept connections:

Code:
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal RequestID As Long)
    If Index = 0 Then
        sckMax = sckMax + 1
        Load Winsock1(sckMax)
        With Winsock1(sckMax)
            .LocalPort = 0
            .Accept RequestID
        End With
    End If
End Sub

And handle disconnects
Code:
Private Sub Winsock1_SocketClosed(Index As Integer)
    If Winsock1(Index).state <> sckClosed Then Winsock1(Index).SocketClose
    Unload Winsock1(Index)
End Sub

Finally, use the Broadcast function declared below to send messages to all clients:
Code:
Private Function Broadcast(ByVal Message As String)
    Dim sck As Winsock
    
    For Each sck In Winsock1
        If sck.state <> sckListening Then sck.SendData Message
    Next
End Function


Hope this helps

Chaz
 
Hi,
To make sure that all clients get message, i think the code should be like this


If (Socket1.State = sckConnected) Then
str = &quot;ques:&quot; & rsQuestions![Question]
str = str & &quot;choa:&quot; & rsQuestions![ChoiceA]
str = str & &quot;chob:&quot; & rsQuestions![ChoiceB]
str = str & &quot;choc:&quot; & rsQuestions![ChoiceC]
str = str & &quot;chod:&quot; & rsQuestions![ChoiceD]
Socket1.SendData str
DoEvents
End If

If (Socket2.State = sckConnected) Then
str = &quot;ques:&quot; & rsQuestions![Question]
str = str & &quot;choa:&quot; & rsQuestions![ChoiceA]
str = str & &quot;chob:&quot; & rsQuestions![ChoiceB]
str = str & &quot;choc:&quot; & rsQuestions![ChoiceC]
str = str & &quot;chod:&quot; & rsQuestions![ChoiceD]
Socket2.SendData str
DoEvents

End If

Hope this helps
Jimmy Le
nhan_tiags@yahoo.com
 
Thank you all for the response, it's working now. I just have to finalize everything.

Best regards,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top