OK, so i've written a server to listen to TCP connections, and send a response. It can be connected to via telnet and works fine.

Next, i started writing the client side of the application - I started out simple by trying the following:

public static void Send2(Server server)
        {
            TcpClient client = new TcpClient();

            client.Connect(server.HostName, server.Port);

            NetworkStream clientStream = client.GetStream();

            byte[] data = Encoding.ASCII.GetBytes("Testing 123...");

            clientStream.Write(data, 0, data.Length);
        }

If i call it with:

Server server = new Server("127.0.0.1", 15000);

            for (int i = 0; i <= 50; i++)
            {
                TcpSender.Send2(server);
            }

It should send "Testing 123..." 50 times, but only shows up on the server a few times (2-10, varies every time).

I'm assuming the client code is at fault, as telnet clients work with the server fine.
Is there anything that would cause this in the code above?
If it's fine - please say so and I will post my server code,
Thanks in advance!

OK, so with the lack of responses I assumed the code was fine, so i mannually wrote the system the longer way (with sockets). Exactly the same problem :(

OK, so now I figured the problem may be the server after all:

public class TcpReceiver
    {
        private TcpListener tcpListener;
        private Thread listenThread;

        public TcpReceiver(ushort port, List<Client> clients)
        {
            tcpListener = new TcpListener(IPAddress.Any, port);
            listenThread = new Thread(new ParameterizedThreadStart(ListenForClients));
            listenThread.Start(clients);
        }

        private void ListenForClients(object objClients)
        {
            List<Client> clients = (List<Client>)objClients;
            tcpListener.Start();

            while (true)
            {
                //Blocks until a client has connected to the server
                clients.Add(new Client(tcpListener.AcceptTcpClient()));

                int index = clients.Count - 1;

                //Create a thread to handle communication with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientCommunication));

                SendTwoObjects sendTwoObjects = new SendTwoObjects((object)clients, (object)index);

                clientThread.Start(sendTwoObjects);

                //Thread responseThread = new Thread(new ParameterizedThreadStart(Respond));
                //responseThread.Start(sendTwoObjects);
            }
        }

        private void HandleClientCommunication(object objSendTwoObjects)
        {
            //This function had to accept a type object, as ParameterizedThreadStart only handles objects
            //It also only accepts one object, so ive casted two datatypes into objects, and then changed the two objects into one using the 'SendTwoObjectsAsOne' class
            //The following reverses the process:
            SendTwoObjects sendTwoObjects = (SendTwoObjects)objSendTwoObjects;
            List<Client> clients = (List<Client>)sendTwoObjects.Obj1;
            int index = (int)sendTwoObjects.Obj2;

            NetworkStream clientStream = clients[index].ClientStream;

            //Note that message is limited to 4096 bytes = MiB
            byte[] message = new byte[4096];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    //Blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, message.Length);
                }
                catch
                {
                    //A socket error!!
                    break;
                }

                if (bytesRead == 0)
                {
                    //i.e no bytes receieved, the client has disconnected
                    break;
                }

                //Message recieved successfully
                ASCIIEncoding encoder = new ASCIIEncoding();
                string strMessage = encoder.GetString(message, 0, bytesRead);

                //Message gotten & stored in strMessage
                //write it to the console, & also send it to OTHER clients
                Console.WriteLine(strMessage);

                for (int i = 0; i < clients.Count; i++)
                {
                    //Don't send the message to the sender
                    if (i != index)
                    {
                        clients[i].ToSend.Add(strMessage);
                    }
                }

                RelayMessage(clients);
            }
            clients[index].TcpClient.Close();
        }

        private void RelayMessage(List<Client> clients)
        {
            ASCIIEncoding encoder = new ASCIIEncoding();

            for (int i = 0; i < clients.Count; i++)
            {
                if (clients[i].ToSend.Count > 0)
                {
                    try
                    {
                        byte[] msg = encoder.GetBytes(clients[i].ToSend[0]);
                        clients[i].ClientStream.Write(msg, 0, msg.Length);
                        clients[i].ToSend.Remove(clients[i].ToSend[0]);
                    }
                    catch { }
                }
            }
        }
    }

Which is called with:

Console.WriteLine("Chat Server Proof of concept");

            List<Client> clients = new List<Client>();

            //Start the TCP server component
            TcpReceiver tcpReciever = new TcpReceiver(15000, clients);

Can anybody find the problem in my code?
Cheers,
Josh.

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.