I have a server program and a client program. While developing the program I run the server and the client on the same machine for convenience. The server starts listening to incoming connections using these lines:
var listener = new TcpListener(IPAddress.Any, 7070);
listener.Start();
The client connects to the server using these lines (simplified):
var client = new TcpClient(AddressFamily.InterNetwork);
client.Connect(IPAddress.Loopback, 7070);
I use IPAddress.Loopback
because I run the programs on the same machine. But, knowing that the server and the client won't be necessarily run on the same machine in the future, I changed it to my public IP from http://icanhazip.com (IPAddress.Parse(...)
). Because of that the client was unable to connect to the server on the same machine with the exception No connection could be made because the target machine actively refused it <my public ip:7070>
.
I tried disabling my firewall but it's still not working. Why is the server refusing the connection? Didn't I specifically tell it to listen to all interfaces with IPAddress.Any
?
Why does that happen and how do I fix it?