Search code examples
c#network-programmingtcpwifi

C# TcpListener and TcpClient on separate PCs in one network


I am trying to understand how Tcp communication is done in C#. I have a simple program, which works fine if the client and listener are on one PC (using localhost).

Then I tried starting the two programs from separate PCs (in the same WiFi network) changing localhost to the local IP address (192.168.0.x) for both programs. But that does not work (I get an exception that the address is not known).

I suppose I am missing something very basic since I do not have a good knowledge on networks. Is there a simple way to solve this? If not, do you have any hints where I could start studying?

I have been looking quite a lot but all examples you find are with localhost.

Thanks for the help.


Solution

  • For the PC that runs the listener you have to allow incoming connections on whatever port you picked (presumably in Windows Firewall) and also put the IP the client uses to connect to it in the TCPListener constructor. Then the server will only accept connections from that network interface that has the IP specified. On the client you just put the same IP and Port.