A
A
Anton20012021-12-29 07:13:33
C++ / C#
Anton2001, 2021-12-29 07:13:33

Tcp chat, how to fix duplicates?

Server code

private void serverThread()
        {
            var tcpListener = new TcpListener(IPAddress.Any, 8080);
            tcpListener.Start();
            while (true)
            {
                var tcpClient = tcpListener.AcceptTcpClient();

                Collections.listConnectedClients.Add(tcpClient);

                byte[] data = new byte[256];
                StringBuilder response = new StringBuilder();
                NetworkStream stream = tcpClient.GetStream();

                do
                {
                    int bytes = stream.Read(data, 0, data.Length);
                    response.Append(Encoding.Unicode.GetString(data, 0, bytes));
                }

                while (stream.DataAvailable);

                string ip = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString();

                string name = response.ToString();

                MessageBox.Show(name);

                this.Invoke((MethodInvoker)(() =>
                {
                    ListViewItem item = new ListViewItem(new string[] { ip,  name });
                    lvOnlineUser.Items.Add(item);
                }));
            }
        }


Client code (test)
private void connect()
        {
            try
            {
                using (var tcpClient = new TcpClient())
                {
                    tcpClient.Connect(address, port);
                    while (true)
                    {
                        if (!IsConnected(tcpClient))
                        {
                            break;
                        }

                        NetworkStream stream = tcpClient.GetStream();
                        byte[] msg = System.Text.Encoding.Unicode.GetBytes("Alex");
                        stream.Write(msg, 0, msg.Length);

                    }
                    connect();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                connect();
            }
        }


If the server was initially turned on, then the name comes up normally, but if the client was turned on first and only then the server, then n calls come. duplicates, example "Alex AlexAlex ..." Tell me
how to fix it, with tcp there was no experience at all. I couldn't google, maybe I googled it wrong.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton2001, 2021-12-30
@Anton2001

This is how it seems to work as it should, but it still seems to me that I'm doing it wrong

private void connect()
        {
            try
            {
                using (var tcpClient = new TcpClient())
                {
                    tcpClient.Connect(address, port);
                    SendName(tcpClient);
                    while (true)
                    {
                        if (!IsConnected(tcpClient))
                        {
                            tcpClient.Close();
                            connect();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                connect();
            }
        }
        private void SendName(TcpClient client)
        {
            NetworkStream stream = client.GetStream();
            byte[] msg = System.Text.Encoding.Unicode.GetBytes("Alex");
            if (IsConnected(client))
            {
                stream.Write(msg, 0, msg.Length);
            }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question