Hello!

I'm trying to code a C# server for flash apps, and I'm currently working on a broadcast function. When flash clients connect to the server they are added to the connClients list (List<TcpClient>), and with the following function I send the stuff to my flash apps:

public void sendToClient(String line, TcpClient client)
        {
            NetworkStream clientStream = client.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)
                    {
                        sendToClient(line,connClients[i]);
                    }
                    else
                    {
                        connClients.Remove(connClients[i]);
                        myInterface.logTxt.AppendText("Client disconnected."+ Environment.NewLine);
                    }
                }
                myInterface.logTxt.AppendText("Message broadcasted to " + connClients.Count.ToString() + " clients." + Environment.NewLine);
                                
        }

The problem is, I can use the sendToClient function on one specific client, but the broadcast doesn't work. It says the message is submitted to the right number of clients, but I never recieve anything. No error messages, no nothing.

Anyone?

Recommended Answers

All 6 Replies

Hello,
I can't see the problem yet, but I got one thing to say - you don't close neither the connection to your client, nor the NetworkStream , that you're working with.

This thingy can bring some troubles :)
Let me know if it solves the problem .. if not - we'll try to find another solution.

When I close the stream the client gets disconnected too, and my attempt to use .Connect(); gets blocked

Ok then .. could you provide the code, that you use to run server and a client's part, when you're making attempt to connect?

This is the client part, it's written in actionscript 3, this works:

public function Connect() 
		{
			socket = new XMLSocket();
			socket.connect(hostName, port);
		}

When I trigger this code I recieve a policy file request on my server, and I am able to send the policy file back without any problems, this is handeled by this bit of code at the end of my recieve function (server);

if (msg == "<policy-file-request/>")
                {
                    sendToClient("<?xml version=\'1.0\'?><cross-domain-policy><allow-access-from domain=\'*\' to-ports=\'*\' /></cross-domain-policy>",tcpClient);
                    myInterface.logTxt.Invoke(new AppendTextDelegate(writeIntoLog), "Policy file sent."+Environment.NewLine);
                    connClients.Add(tcpClient);
                    break;
                }

See the SendToClient function in my first post.

This all works, when I only accept one client and keep the connection open I am able to send and recieve to both sides, when I try to send using the list with clients I dont recieve anything on my client.
Sending from client to server works all the time.

hello,
can you help me, could you provide the code of how you create the list and how save the connection on it and how the server can communicate with the multiple Client

  1. This thread is 3 years old and the op only made 3 posts so probably isn't here anymore
  2. Your question basically asked the OP for the entire solution. The first two parts of your questions are that easy any .net programmer can do it. So chalk one up for laziness.
  3. You didn't show any of your own work or any attempt you've made.

In response, I'll help you with the same amount of effort you used to post.

Open Visual Studio, File, New Project, C#, Win32, Windows Forms Application.

Have fun! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.