question

YRK-33345 avatar image
0 Votes"
YRK-33345 asked ·

How to make the IP to the computer's IP in the TCP Server?

How to make the IP to the computer's IP in the TCP Server?
I created a chat but every time you open the software the IP changes, so I want to make the IP the IP of the computer or the IP be fixed
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;
         string ChooseClient = "127.0.0.1:9000";
         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
             {
    
                 if (Encoding.UTF8.GetString(e.Data).Contains("127.0.0.1:"))
                 {
                     lstClientIP.SelectedItem = Encoding.UTF8.GetString(e.Data);
                 }
    
                 if (!Encoding.UTF8.GetString(e.Data).Contains("127.0.0.1:"))
                 {
                     txtInfo.Text += $"{e.IpPort}: {Encoding.UTF8.GetString(e.Data)} {Environment.NewLine}";
                     server.Send(lstClientIP.SelectedItem.ToString(), Encoding.UTF8.GetString(e.Data));
                 }
                    
                    
                    
                    
             });
         }
    
         private void Events_ClientDisconnected(object sender, ClientDisconnectedEventArgs e)
         {
             this.Invoke((MethodInvoker)delegate
             {
                 server.Send(lstClientIP.SelectedItem.ToString(), txtMessage.Text);
                 txtInfo.Text += $"{e.IpPort} disconnected.{Environment.NewLine}";
                 lstClientIP.Items.Remove(e.IpPort);
                 frindNum -= 1;
                    
                    
             });
                
         }
    
         private void Events_ClientConnected(object sender, ClientConnectedEventArgs e)
         {
                
             this.Invoke((MethodInvoker) delegate 
             {
                 frindNum += 1;
                 txtInfo.Text += $"{e.IpPort} connected.{Environment.NewLine}";
                 lstClientIP.Items.Add(e.IpPort);
    
                 server.Send(e.IpPort, e.IpPort);
    
             });
                
         }
    
         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)
         {
    
         }
    
         private void txtMessage_TextChanged(object sender, EventArgs e)
         {
    
         }
     }
 }

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;
         int locY;
    
         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 += $"fraind: {Encoding.UTF8.GetString(e.Data)} {Environment.NewLine}";
    
                 locY += 51;
                 Label tb = new Label();
                 tb.Location = new Point(label5.Location.X, locY);
                 tb.Text = $"fraind: {Encoding.UTF8.GetString(e.Data)} {Environment.NewLine}";
                 tb.Size = label5.Size;
                 tb.BackColor = label5.BackColor;
                 Panel pe = new Panel();
                 pe.Location = new Point(panel5.Location.X, locY - 8);
                 pe.Size = panel5.Size;
                 pe.BackColor = panel5.BackColor;
                 pe.BackgroundImageLayout = panel5.BackgroundImageLayout;
                 pe.BackgroundImage = panel5.BackgroundImage;
                 Controls.Add(tb);
                 Controls.Add(pe);
                 pe.Parent = panel4;
                 tb.Parent = panel4;
                 pe.BringToFront();
                 tb.BringToFront();
    
                 if (Encoding.UTF8.GetString(e.Data).Contains("127.0.0.1:"))
                 {
                     label3.Text = Encoding.UTF8.GetString(e.Data);
                        
                 }
             });
                
         }
    
         private void Events_ClientConnected(object sender, ClientConnectedEventArgs e)
         {
             this.Invoke((MethodInvoker)delegate
             {
                 txtInfo.Text += $"server conected.{Environment.NewLine}";
                 btnSend.Enabled = false;
                 btnConnect.Enabled = false;
                 label3.Text = "none";
                    
    
             });
    
                
                
         }
    
         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}";
    
                     locY += 51;
                     Label tb = new Label();
                     tb.Location = new Point(label4.Location.X, locY - 2);
                     tb.Text = $"me: {txtMessage.Text}{Environment.NewLine}";
                     tb.Size = label4.Size;
                     tb.BackColor = label4.BackColor;
                     Panel pe = new Panel();
                     pe.Location = new Point(panel3.Location.X, locY - 8);
                     pe.Size = panel3.Size;
                     pe.BackColor = panel3.BackColor;
                     pe.BackgroundImageLayout = panel3.BackgroundImageLayout;
                     pe.BackgroundImage = panel3.BackgroundImage;
                     Controls.Add(tb);
                     Controls.Add(pe);
                     pe.Parent = panel4;
                     tb.Parent = panel4;
                     pe.BringToFront();
                     tb.BringToFront();
    
                     panel4.Size += new Size(0, 25);
    
                     txtMessage.Text = string.Empty;
                 }
             }
         }
    
         private void lstClientIP_SelectedIndexChanged(object sender, EventArgs e)
         {
             client.Send(lstClients.SelectedItem.ToString());
         }
    
         private void label2_Click(object sender, EventArgs e)
         {
    
         }
    
         private void button1_Click(object sender, EventArgs e)
         {
             lstClients.Items.Add(textBox1.Text);
         }
    
         private void textBox1_TextChanged(object sender, EventArgs e)
         {
    
         }
    
         private void txtMessage_TextChanged(object sender, EventArgs e)
         {
             client.Send(lstClients.SelectedItem.ToString());
         }
    
         private void label3_Click(object sender, EventArgs e)
         {
    
         }
    
         private void button3_Click(object sender, EventArgs e)
         {
             try
             {
                 client.Disconnect();
                 btnSend.Enabled = false;
                 btnConnect.Enabled = false;
                    
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "messege", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }
    
         private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
         {
             panel4.Location = new Point(panel4.Location.X,-panel4.Location.Y + -vScrollBar1.Value);
         }
    
         private void textBox2_TextChanged(object sender, EventArgs e)
         {
    
         }
     }
 }





dotnet-csharpwindows-forms
· 1
10 |1000 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 @YRK-33345,
Based on your code, I noticed that you connect to server via "txtIP.Text".
And If you specify 127.0.0.1 as the listener IP address, it will only be able to accept connections from within the local host.
So which ip are you referring to?And if you want ip fixed, you can use static ip.
Best Regards,
Daniel Zhang

1 Vote 1 ·

0 Answers