Results 1 to 15 of 15

Thread: [RESOLVED] Multiple connections

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    40

    Resolved [RESOLVED] Multiple connections

    I have a multiple client-server setup using VB 6.0 that is connecting upto 30 computers at once across a LAN using TCP/IP connection. After all the client computers are connected to the server computer, I was trying to broadcast a command to all the clients at once, but as I itterated through each winsock connection, the command would only being recieved by last computer which connected. Is there any way to do this so all the computers that are connected the receive the command?

    VB Code:
    1. for idx = 1 to 30
    2.    Socket(idx).SendData CmndStrng
    3. next
    Last edited by dlern; Jan 26th, 2006 at 10:56 AM.

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Multiple connections

    What type of connection do you use?
    TCP or UDP?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    40

    Re: Multiple connections

    TCP and don't want to use the UDP broadcast method

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Multiple connections

    You say that the 30 computers are all connected, did check them all? (I let each of them send a message when connected to server. That way Im sure all are connected)
    The sending code by itself seems okay to me (whatever that says)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    40

    Re: Multiple connections

    VB Code:
    1. Private Sub Form_Load()
    2.    
    3.     lblHostID.Caption = Socket(0).LocalHostName
    4.     lblAddress.Caption = Socket(0).LocalIP
    5.     Socket(0).LocalPort = 1007
    6.     sServerMsg = "Listening to port: " & Socket(0).LocalPort
    7.     List1.AddItem (sServerMsg)
    8.     Socket(0).Listen
    9. End Sub
    10.  
    11.  
    12. Private Sub socket_Close(Index As Integer)
    13.     sServerMsg = "Connection closed: " & "[" & Index & "]" & Socket(Index).RemoteHostIP
    14.     List1.AddItem (sServerMsg)
    15.     Socket(Index).Close
    16.     Unload Socket(Index)
    17.     iSockets = iSockets - 1
    18.     lblConnections.Caption = iSockets
    19.    
    20. End Sub
    21.  
    22. Private Sub socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    23.     sServerMsg = "Connection request id " & "[" & sckCount & "]" & requestID & " from " & Socket(Index).RemoteHostIP
    24.   If Index = 0 Then
    25.     List1.AddItem (sServerMsg)
    26.     sRequestID = requestID
    27.     iSockets = iSockets + 1
    28.     sckCount = sckCount + 1
    29.     lblConnections.Caption = iSockets
    30.     Load Socket(sckCount)
    31.     Socket(sckCount).LocalPort = 1111
    32.     Socket(sckCount).Accept requestID
    33.     Debug.Print requestID
    34.   End If
    35.  
    36. End Sub
    37.  
    38. Private Sub socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    39.    
    40.        
    41.     ' get data from client
    42.     Socket(Index).GetData sRData, vbString
    43.  
    44. End Sub
    45.  
    46. Private Cmnd_click
    47. for idx = 1 to 30
    48.    Socket(idx).SendData CmndStrng
    49. next
    50.  
    51. End sub

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    40

    Re: Multiple connections

    I have include the server code. Within the form, the number of active connections is listed as a label and a listbox displays as each client connects or closes.

    So I do know all of the clients are connected, but the command only gets received my the most recently connected client (client form also displays the received data).

    Any ideas?

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Multiple connections

    i've only browsed the thread but try this,

    VB Code:
    1. for i = 0 to winsock.ubound - 1
    2.     if winsock(i).state = sckconnected then
    3.        winsock(i).senddata Data
    4.     end if
    5.   doevents
    6. next i

    Pino

  8. #8
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Multiple connections

    do all your connections share a single port on the serverside?
    I think that is the problem!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Multiple connections

    opus is correct your code here needs changing.

    VB Code:
    1. Private Sub socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    2.     sServerMsg = "Connection request id " & "[" & sckCount & "]" & requestID & " from " & Socket(Index).RemoteHostIP
    3.   If Index = 0 Then
    4.     List1.AddItem (sServerMsg)
    5.     sRequestID = requestID
    6.     iSockets = iSockets + 1
    7.     sckCount = sckCount + 1
    8.     lblConnections.Caption = iSockets
    9.     Load Socket(sckCount)
    10.     Socket(sckCount).LocalPort = 1111
    11.     Socket(sckCount).Accept requestID
    12.     Debug.Print requestID
    13.   End If

    chang eit to this....

    VB Code:
    1. Private Sub socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    2.    
    3. sServerMsg = "Connection request id " & "[" & sckCount & "]" & requestID & " from " & Socket(Index).RemoteHostIP
    4.  
    5.   Load Winsock(Winsock.UBound + 1)
    6.   Winsock(Winsock.UBound).Accept RequestID
    7.  
    8. End Sub

    Not the 100% best way but for 30 sockets you should be ok, and it will work

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    40

    Thumbs up Re: Multiple connections

    Pino, that fixed it perfectly. I can now send commands to individual clients or broadcast to all the connected clients.

    Only, what is the problem with this method. What if I was connected to 100 clients, or 500?

    Thanks Again!!!!!!

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Multiple connections

    the code i gaev u will let you have 1 million clients connect (memory dependant)

    Basicly its not so good because lets say we have 5 clients connected and client 3 leaves and so does client 1, thast 2 open slots not being used just munching memory, good code would search each slot to see if its free, but for now you dont need to concern yourself with that!

    Pino

  12. #12
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Multiple connections

    Look at the server side project in here:
    VB - How to send a file using the Winsock control

    That is how you properly accept connections. The server is re-using sockets that are closed, therefore saving memory.

  13. #13
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Houston, TX
    Posts
    342

    Re: Multiple connections

    ....I'm just adding my two cents here....

    Instead of sending individual messages to each computer (especially if you get into the hundreds of connections) what about using a multicast address. This is not difficult to do in Visual Studio and is extremely efficient for sending one copy of a message to many computers (thousands, if your network can support it) simultaneously. This also solves the issue of computers leaving the network during the broadcast because the multicast mechanisms automatically adjust when clients join/leave the multicast domain.

    The only caveat here is that if your computers are on different subnets you will need to make sure your routers support multicast routing.
    Last edited by MagellanTX; Jan 12th, 2006 at 03:08 PM.

  14. #14
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Multiple connections

    He's using VB6, you can't do multicast in VB6...

  15. #15
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Houston, TX
    Posts
    342

    Re: Multiple connections

    Whoops, was focusing on the issue as a whole...completely missed that important detail.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width