Results 1 to 2 of 2

Thread: Send message to all multithreaded clients help.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    70

    Send message to all multithreaded clients help.

    Given the code, ss there a way to propagate messages send from any of the multithreaded chat client to all the other chat client:

    Thanks!

    First the main will call startClient:

    Code:
       Public Sub startClient(ByVal inClientSocket As TcpClient, _
            ByVal clineNo As String)
    
                'Dim clientCounter As Integer
    
                Dim arr_inClientSocket(clientCounter) As TcpClient
                arr_inClientSocket(clientCounter) = inClientSocket
                clientCounter += 1
    
                Me.clientSocket = inClientSocket
                Me.clNo = clineNo
    
                Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
                ctThread.Start()
    
            End Sub
    
    Private Sub doChat()
    
                Dim requestCount As Integer
                Dim bytesFrom(10024) As Byte
                Dim dataFromClient As String
                Dim sendBytes As [Byte]()
                Dim serverResponse As String
                Dim rCount As String
    
                requestCount = 0
    
                While (True)
    
                    Try
    
                        requestCount = requestCount + 1
    
                        Dim networkStream As NetworkStream = _
                        clientSocket.GetStream()
    
                        networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
    
                        dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
    
                        dataFromClient = _
                        dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
    
                        msg("From client-" + clNo + dataFromClient)
    
                        rCount = Convert.ToString(requestCount)
    
                        serverResponse = "Server to clinet(" + clNo + ") " + rCount ' Return message to client
    
                        sendBytes = Encoding.ASCII.GetBytes(serverResponse)
    
                        'I thought I could do something here tosend the message to all chat clients but ain't got an idea how.
    
                        networkStream.Write(sendBytes, 0, sendBytes.Length)
    
                        networkStream.Flush()
    
                        msg(serverResponse)
    
                    Catch ex As Exception
    
                        MsgBox(ex.ToString)
    
                    End Try
    
                End While
    
            End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Send message to all multithreaded clients help.

    I would recommend against creating a new Thread for every client. You can follow the CodeBank link in my signature and check out my Asynchronous TCP post for a better way to do it. You only manage a single thread and make asynchronous calls, letting the system worry about the details. To broadcast a message to all clients you can then use the ThreadPool, e.g.
    vb.net Code:
    1. Private Sub BroadcastMessage(ByVal originatingClient As TcpClient, ByVal message As String)
    2.     For Each client As TcpClient In Me.allClients
    3.         If client IsNot originatingClient Then
    4.             ThreadPool.QueueUserWorkItem(AddressOf SendMessage, New ClientMessage(client, message))
    5.         End If
    6.     Next
    7. End Sub
    You'd have to define the ClientMessage type and the SendMessage method appropriately.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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