954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

TcpClient List for Server

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), 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?

perrytje
Newbie Poster
3 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Antenka
Posting Whiz
362 posts since Nov 2008
Reputation Points: 293
Solved Threads: 82
 

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

perrytje
Newbie Poster
3 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

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?

Antenka
Posting Whiz
362 posts since Nov 2008
Reputation Points: 293
Solved Threads: 82
 

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.

perrytje
Newbie Poster
3 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: