...

/

Programming for Multiple Networks

Programming for Multiple Networks

Learn about application listening from a socket, multihomed servers, and physical hosts.

Listening on a socket

This multitude of interfaces affects the application software. By default, an application that listens on a socket will listen for connection attempts on any interface. Language libraries always have an “easy” version of listening on a socket. The easy version just opens a socket on every interface on the host. Bad news! Instead, we have to do it the hard way and specify which IP address we are opening the socket for:

Press + to interact
ln, err := net.Listen("tcp", ":8080") // Bad approach
ln, err := net.Listen("tcp", "spock.example.com:8080") // Good approach

To determine which interfaces to bind to, the application must be told its own name or IP addresses. This is a big difference with multihomed servers. In development, the server can always call its language-specific version of getLocal- Host(), but on a multihomed machine, this simply returns the IP address associated with the server’s internal hostname. This could be any of the interfaces, depending on local naming conventions. Therefore, server applications that need to listen on sockets must add configurable properties to define to which interfaces the server should bind.

Outbound connections

Under exceedingly rare conditions, an application also has to specify which interface it wants traffic to leave from when connecting to a ...