No Connection Could be Made because the target machine actively refused it - TCP Client C#

Hemanth B 886 Reputation points
2021-08-31T08:34:51.677+00:00

Hi, seeing this video I created an connection between two c# applications: https://www.youtube.com/watch?v=ve2LX1tOwIM.

Now in Form 1:
private void btnstartserver_Click(object sender, EventArgs e)
{
txtStatus.Text += "Server Starting....";
System.Net.IPAddress ip = System.Net.IPAddress.Parse(txtHost.Text);
server.Start(ip, Convert.ToInt32(txtPort.Text));
}
private void Form1_Load(object sender, EventArgs e)
{
server = new SimpleTcpServer();
server.Delimiter = 0x13;
server.StringEncoder = Encoding.UTF8;
server.DataReceived += Server_DataRec;
}
private void Server_DataRec(object sender, SimpleTCP.Message e)
{
txtStatus.Invoke((MethodInvoker)delegate ()
{
txtStatus.Text += e.MessageString;
e.ReplyLine(string.Format("You Said: {0}", e.MessageString));
});
}
In form 2:
private void btnconnect_Click(object sender, EventArgs e)
{

            client.Connect(txtHost.Text, Convert.ToInt32(txtPort.Text));  
             
        }  
 private void Form2_Load(object sender, EventArgs e)  
        {  
            client = new SimpleTcpClient();   
            client.StringEncoder = Encoding.UTF8;  
            client.DataReceived += Client_DataRec;  
        }  
private void Client_DataRec(object sender, SimpleTCP.Message e)  
        {  
            txtStatus.Invoke((MethodInvoker)delegate ()  
            {  
                txtStatus.Text += e.MessageString;  
                 
            });  
        }  
private void btnSendMessage_Click(object sender, EventArgs e)  
        {  
            
            client.WriteLineAndGetReply(txtMessage.Text, TimeSpan.FromSeconds(3));    
        }  

When I run both forms at once in the same device and click on start and in form 1 and connect in form 2, it connects perfectly and data is getting transferred. But if I run Form 1 in one Windows PC and form 2 in another, and click start and click connect it throws this exception:
127864-screenshot-2021-08-31-140258.png

How to fix this?
txtHost.Text in both devices contains: "127.0.0.1"
txtPort.Text in both devices contains "8910"

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,484 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 26,676 Reputation points
    2021-08-31T14:14:47.42+00:00

    127.0.0.1 is the loopback IP and refers to the current machine. You must use the IP of the remote machine and make sure firewalls and virus checkers do not block the IP:Port.

    0 comments No comments