question

YRK-33345 avatar image
0 Votes"
YRK-33345 asked DanielZhang-MSFT edited

how to send a message between clients

I did a project of a server that can choose from a list with which client to talk to and there the client can only talk to the server, and I want to make that the client will also have the option to choose from a list if which client to talk to,

This is the server code:

 using SimpleTcp;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
    
 namespace TCPServer
 {
     public partial class Form1 : Form
     {
         int frindNum;
         public Form1()
         {
             InitializeComponent();
         }

     public SimpleTcpServer server;

     private void btnStart_Click(object sender, EventArgs e)
     {
         server.Start();
         btnSend.Enabled = false;
         txtInfo.Text += $"starting...{Environment.NewLine}";
         btnStart.Enabled = false;
         btnSend.Enabled = true;
     }

     private void Form1_Load(object sender, EventArgs e)
     {
         btnSend.Enabled = false;
         server = new SimpleTcpServer(txtIP.Text);
         server.Events.ClientConnected += Events_ClientConnected;
         server.Events.ClientDisconnected += Events_ClientDisconnected;
         server.Events.DataReceived += Events_DataReceived;
     }

     private void Events_DataReceived(object sender, DataReceivedEventArgs e)
     {
         this.Invoke((MethodInvoker)delegate
         {
             txtInfo.Text += $"{"frind" + frindNum}: {Encoding.UTF8.GetString(e.Data)} {Environment.NewLine}";
         });
     }

     private void Events_ClientDisconnected(object sender, ClientDisconnectedEventArgs e)
     {
         this.Invoke((MethodInvoker)delegate
         {
             server.Send(lstClientIP.SelectedItem.ToString(), txtMessage.Text);
             txtInfo.Text += $"{"frind" + frindNum} disconnected.{Environment.NewLine}";
             lstClientIP.Items.Remove("frind" + frindNum);
             frindNum -= 1;
         });
            
     }

     private void Events_ClientConnected(object sender, ClientConnectedEventArgs e)
     {
         this.Invoke((MethodInvoker) delegate 
         {
             frindNum += 1;
             txtInfo.Text += $"{"frind" + frindNum} connected.{Environment.NewLine}";
             lstClientIP.Items.Add("frind" + frindNum);
         });
            
     }

     private void btnSend_Click(object sender, EventArgs e)
     {
         if (server.IsListening)
         {
             if(!string.IsNullOrEmpty(txtMessage.Text) && lstClientIP.SelectedItem != null)
             {
                    
                 server.Send(lstClientIP.SelectedItem.ToString(), txtMessage.Text);
                    
                 txtInfo.Text += $"server: {txtMessage.Text}{Environment.NewLine}";
             }
         }
     }

     private void lstClientIP_SelectedIndexChanged(object sender, EventArgs e)
     {

     }
 }

}


This is the client code:

 using SimpleTcp;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
    
 namespace TCP_Client
 {
     public partial class Form1 : Form
     {
            
         public Form1()
         {
             InitializeComponent();
         }

     SimpleTcpClient client;
        

     private void btnConnect_Click(object sender, EventArgs e)
     {
         try
         {
             client.Connect();
             btnSend.Enabled = true;
             btnConnect.Enabled = false;
         }
         catch(Exception ex)
         {
             MessageBox.Show(ex.Message, "messege", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }

     private void Form1_Load(object sender, EventArgs e)
     {
         client = new(txtIP.Text);
         client.Events.Connected += Events_ClientConnected;
         client.Events.DataReceived += Events_DataReceived;
         client.Events.Disconnected += Events_ClientDisconnected;
         btnSend.Enabled = false;

     }

     private void Events_ClientDisconnected(object sender, ClientDisconnectedEventArgs e)
     {
         this.Invoke((MethodInvoker)delegate
         {
             txtInfo.Text += $"server disconected.{Environment.NewLine}";
         });
     }

     private void Events_DataReceived(object sender, DataReceivedEventArgs e)
     {
         this.Invoke((MethodInvoker)delegate
         {
             txtInfo.Text += $"server: {Encoding.UTF8.GetString(e.Data)} {Environment.NewLine}";
         });
            
     }

     private void Events_ClientConnected(object sender, ClientConnectedEventArgs e)
     {
         this.Invoke((MethodInvoker)delegate
         {
             txtInfo.Text += $"server conected.{Environment.NewLine}";
         });
            
     }

     private void btnSend_Click(object sender, EventArgs e)
     {
         if (client.IsConnected)
         {
             if (!string.IsNullOrEmpty(txtMessage.Text))
             {
                    
                 client.Send(txtMessage.Text);
                 txtInfo.Text += $"me: {txtMessage.Text}{Environment.NewLine}";
                 txtMessage.Text = string.Empty;
             }
         }
     }

     private void lstClientIP_SelectedIndexChanged(object sender, EventArgs e)
     {

     }
 }

}


dotnet-csharp
· 1
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.

thank you😁

0 Votes 0 ·

1 Answer

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

Hi @YRK-33345 ,
Maybe you can try the BradleyDotNET's method that holding the incoming client objects in memory.
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.


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.