How to make the server recive inlimmited number of clients usig TcpClients , TcpListener in .Net

Ghada Alsayed 1 Reputation point
2021-03-15T08:39:00.653+00:00

Hi
I need help please I wrote this chat application in c#, .net, the chat work beautiful for one client how to make the server accept multiple client at the same time. I nedd your help please.
Here is my simple code,

namespace Server_chat  
{  
    public partial class Form1 : Form  
    {  
        TcpListener lyssnare;  
        TcpClient klient = new TcpClient();  
        List<TcpClient> klientList = new List<TcpClient>();  
        //int port = 126;  
  
        public Form1()  
        {  
            InitializeComponent();  
            try   
            {  
  
                IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);  
                lyssnare = new TcpListener(endpoint);  
                lyssnare.Start();  
              StartaReciving()  
              
  
            }  
            catch (Exception error)  
            {  
                MessageBox.Show(error.Message, Text);  
                return;  
            }  
             
              
  
        }  
  
            public async void StartaReciving()  
            {  
                try  
                {  
  
                while (true)  
                {  
                      
  
                   klient = await lyssnare.AcceptTcpClientAsync();   
  
                    StartReading(klient);  
                }    
                  
                }  
                catch (Exception error)  
                {  
                    MessageBox.Show(error.Message, Text);  
                    return;  
                }  
  
            }  
            
            public async void StartReading(TcpClient k)  
            {  
                byte[] buffert = new byte[1024];  
                int n = 0;  
                try  
                {  
                    n = await k.GetStream().ReadAsync(buffert, 0, buffert.Length);  
                }  
                catch (Exception error)  
                {  
                    MessageBox.Show(error.Message, Text);  
                    return;  
                }  
              
                richTextBox1.AppendText(Encoding.Unicode.GetString(buffert, 0, n));  
                StartaLäsning(k);   
                Console.WriteLine();  
  
            }  
          
  
            private void richTextBox1_TextChanged(object sender, EventArgs e)  
            {  
  
            }  
        }  
    }  

Client.cs

 TcpClient klient = new TcpClient();  
         
        int port = 125;  
  
        public Form1()  
        {  
            InitializeComponent();  
            klient.NoDelay = true;  
        }  
  
        private void richTextBox2_TextChanged(object sender, EventArgs e)  
        {  
  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            if (!klient.Connected)  
                Startconnection();  
        }  
        public async void  Startconnection()  
        {  
            try  
            {  
                IPAddress adress = IPAddress.Parse(textBox1.Text);  
                 
                await klient.ConnectAsync(adress, port);  
  
            }  
            catch (Exception e)  
            {  
                MessageBox.Show(e.Message, Text);  
                return;  
            }  
            button2.Enabled = true;  
            button1.Enabled = false;  
        }  
  
        private void button2_Click(object sender, EventArgs e)  
        {  
             
            Startsending(richTextBox2.Text);  
             
          }  
        public async void Startsending(String message)  
        {  
            richTextBox2.Text = textBox1.Text + ":" + port + "> " + richTextBox1.Text;  
            byte[] ut = Encoding.Unicode.GetBytes(richTextBox2.Text);   
  
            try  
            {  
                await klient.GetStream().WriteAsync(ut, 0, ut.Length);   
                 
                 
            }  
            catch (Exception ex)  
            {  
                MessageBox.Show(ex.Message, Text);  
                return;  
            }  
              
        }  
  
        private void richTextBox1_TextChanged(object sender, EventArgs e)  
        {  
  
        }  

77771-skarmbild-41.png
Thank you!

Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,651 Reputation points
    2021-03-16T01:57:10.51+00:00

    Hi GhadaAlsayed-7509,
    A socket is an end point of a connection. It is identified by a 2-tuple (IP , Port). A connection is identified by a 4 tuple (client IP,client port, server IP, server port). If anyone of these is different, it will be a different connection.
    In this case, since clients are different, multiple clients can connect to the same server socket.
    So the server can receive any number of connections on its single listening port, as long as each client uses a different address/port combination.
    You can also create multiple threads to handle multiple connections.
    Here some similar threads you can refer to.
    How to make a TCP server handle multiple clients?
    [How do multiple clients connect simultaneously to one port, say 80, on a server? duplicate
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.