TcpClient list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perrytje
    New Member
    • Mar 2010
    • 3

    TcpClient list

    I'm making a C# server for flash clients using TcpClients and listeners. I got the whole thing working when using just one TcpClient, I can send to and recieve from it. Now I'm working on a function to broadcast a message to all the TcpClients, which I've added to a List. It says it sent but in fact I don't recieve anything on my flash apps. Does anybody know what's wrong?

    Here's the bit of code that broadcasts it, it's just the single client message for each client.

    Code:
    public void sendToClient(String line)
            {
                NetworkStream clientStream;
                clientStream = activeClient.GetStream();
                ASCIIEncoding encoder = new ASCIIEncoding();
                byte[] buffer = encoder.GetBytes(line + "\0");
    
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();
            }
    
            public void broadcast(String line)
            {
                    for (int i = 0; i < connClients.Count; i++)
                    {
                        if (connClients[i].GetStream().CanWrite)
                        {
                            activeClient = connClients[i];
                            sendToClient(line);
                        }
                        else
                        {
                            connClients.Remove(connClients[i]);
                            myInterface.logTxt.AppendText("Client disconnected."+ Environment.NewLine);
                        }
                    }
                    myInterface.logTxt.AppendText("Message broadcasted to " + connClients.Count.ToString() + " clients." + Environment.NewLine);
                                    
            }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Shouldn't your send to client function take in the client object you want to send to?
    Do you get any exceptions?

    Comment

    • perrytje
      New Member
      • Mar 2010
      • 3

      #3
      You are right, I've changed the function so it includes the tcpclient object, but although of this it still doesn't get recieved, I don't get any exceptions.

      Comment

      Working...