What IP adress sould I use to communicate over TCP

Hello. I am trying to make a simple one way chat over TCP. But the problem is that when I am listening on my ip:port no one can reach me. What address from the miriads of addresses that ipconfig -all shows me should I use? When I try to connect both the client and the server at one computer it works and I am using IPv4 (preffered). Thank you very much.

Quick tip: If you only pass a port number to net.Listen, it will listen on all available interfaces. (E.g. both LAN and WLAN.)

listener, err = net.Listen("tcp", ":6100")

(Shameless plug: I wrote an article about basic networking in Go some time ago - the code there uses this trick to avoid having to identify and select a particular network interface.)

3 Likes

Thank you very much @christophberger for the tip on the listening side :-), but what address should I connect to, when using conn, err := net.Dial("tcp", addressString) ?

If your machine has a network name that can be resolved via DNS, you can use this name in Dial:

Dial("tcp", "yourmachine.yourdomain.com:6100")

This should even work in a home network, as most routers assign local network names to each machine, like, “yourmachine.local” or similar.

In the unlikely case that this does not work:

I assume that your machine runs Windows (because ipconfig is typically seen on Windows only, Unixes use ifconfig). The network configuration panel (available somewhere in the Control Panel app) should provide further information about the active network interfaces, especially if one of them connects to the local LAN.

1 Like

Thank you very much. I’ll give it a try!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.