question

GhadaAlsayed-7509 avatar image
0 Votes"
GhadaAlsayed-7509 asked DanielZhang-MSFT commented

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

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!



dotnet-csharp
skarmbild-41.png (1.3 MiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT commented

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
Thank you for information, Links are in python, can you help me for C#, in my code What I should change to server can accept multiple connection, I get error for port number when I run a client how has another port number. list of client how dose it work?
Thank you!

0 Votes 0 ·

Hi
Sorry for python code, and the main idea is using a while loop to keep the server in listening mode. So that you can have multiple instance of client connected.

  while (true)
         {
             TcpClient client = listener.AcceptTcpClient();
             .....
           }

Here are some c# code examples in this thread.
Best Regards,
Daniel Zhang


0 Votes 0 ·