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.
No Connection Could be Made because the target machine actively refused it - TCP Client C#
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:
How to fix this?
txtHost.Text in both devices contains: "127.0.0.1"
txtPort.Text in both devices contains "8910"