Click here to Skip to main content
15,175,960 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a very simple C# client/server chat application with TCP/IP Protocol that it works on local network but I want to use it on internet network but it seem is not easily in C# and I must to do port forwarding on server router or use SSH Tunnel but I do not know How to do them?
Please help me by show me an example, a resource or a code that works....
Thank you.

What I have tried:

I just have a very simple C# client/server chat application with TCP/IP Protocol that it works on local network but I want to use it on internet network:

Server side Program:

C#
namespace TCPIPDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        SimpleTcpServer server;
        private void Form1_Load(object sender, EventArgs e)
        {
            server = new SimpleTcpServer();
            server.Delimiter = 0x13;//enter
            server.StringEncoder = Encoding.UTF8;
            server.DataReceived += Server_DataReceived;
        }
 
        private void Server_DataReceived(object sender, SimpleTCP.Message e)
        {
            //Update mesage to txtStatus
            txtStatus.Invoke((MethodInvoker)delegate ()
            {
                txtStatus.Text += e.MessageString;
                e.ReplyLine(string.Format("You said: {0}", e.MessageString));
            });
        }
 
        private void btnStart_Click(object sender, EventArgs e)
        {
            //Start server host
            txtStatus.Text += "Server starting...";
            System.Net.IPAddress ip = System.Net.IPAddress.Parse(txtHost.Text);
            server.Start(ip, Convert.ToInt32(txtPort.Text));
        }
 
        private void btnStop_Click(object sender, EventArgs e)
        {
            if (server.IsStarted)
                server.Stop();
        }
    }
}


Client side Program:

C#
namespace Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        SimpleTcpClient client;
 
        private void btnConnect_Click(object sender, EventArgs e)
        {
            btnConnect.Enabled = false;
            //Connect to server
            client.Connect(txtHost.Text, Convert.ToInt32(txtPort.Text));
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            client = new SimpleTcpClient();
            client.StringEncoder = Encoding.UTF8;
            client.DataReceived += Client_DataReceived;
        }
 
        private void Client_DataReceived(object sender, SimpleTCP.Message e)
        {
            //Update message to txtStatus
            txtStatus.Invoke((MethodInvoker)delegate ()
            {
                txtStatus.Text += e.MessageString;               
            });
        }
 
        private void btnSend_Click(object sender, EventArgs e)
        {
            client.WriteLineAndGetReply(txtMessage.Text, TimeSpan.FromSeconds(3));
        }
    }
}
Posted
Updated 4-Feb-21 5:46am

1 solution

Have a look here: SignalR - Simple Chat Application in C#[^] - it's a lot simpler than trying to use TCP/IP directly.
   
Comments
Mohammad Tavoosi 4-Feb-21 10:56am
   
I want to Create a desktop application, Can I use SignalR for it?
Does SignalR work on internet network?
Thanks for you...
OriginalGriff 4-Feb-21 11:16am
   
Did you look at the link I gave you, at all?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900