2

A bunch of servers offer functionality on a particular port (the port listens on all interfaces). Servers must self-test their own functionality by connecting to themselves. What DNS name or IP should the test connection use?

Ideally, I'd use one of the local IPs and be done with it (e.g. '192.13.1.5'). However, obtaining the list of local IPs is difficult for the test code.

I can use 'localhost' (127.0.0.1), but this uses the loopback interface and bypasses network hardware. Which leads me to...

Question: Is there shorthand notation for "Any local IP this server uses"? (except loopback address).

Using '0.0.0.0' seems to work on Linux, but not on Windows.

7
  • 2
    How about the computer's own name? Aug 4, 2015 at 2:23
  • 0.0.0.0 does mean "all IPv4 addresses belonging to this machine", so you've got it right with Linux. I am unsure, but at a guess, knowing Windows there is probably a flag not set on a socket that should be, or something ridiculous like that.
    – Shane
    Aug 4, 2015 at 3:11
  • Michael: I was hoping for the notation-based solution Shane pointed out. I ended up writing code to resolve.
    – Happyblue
    Aug 4, 2015 at 5:19
  • @Shane: 0.0.0.0 indeed means that, but only in contexts where it makes sense to talk about that collection. In particular, that makes sense when accepting new TCP connections or listening for UDP packets. Not for sending data.
    – MSalters
    Aug 4, 2015 at 9:41
  • 1
    @Shane: That's pretty much the same. "Connecting" starts by sending a SYN packet.
    – MSalters
    Aug 5, 2015 at 8:30

2 Answers 2

7

To add to Michael Hampton's suggestion, Windows should recognize %computername% as an environment variable, which you can ping, etc.

It looks to me like the PowerShell cmdlet Test-NetConnection along with the windows environment variable might do what you want. You can specify a port, but since you didn't mention one I just used

Test-NetConnection -ComputerName $env:COMPUTERNAME

and that worked fine on my laptop. The port syntax for checking port 135 (as a random example) would be

Test-NetConnection -Port 135 -ComputerName $env:COMPUTERNAME
1
  • 1
    Thanks Katherine. I was hoping for a notation-based, code-free solution (my test application needs code to access environment variables). I ended up biting the bullet and writing code.
    – Happyblue
    Aug 4, 2015 at 5:16
0

Thanks folks - deeply appreciate your help here. Yes, hostname environment variables would work but require code to access. I'm with Shane - 0.0.0.0 should work, but there's something tricky about it.

I figured I'd need to create code (which had to be Java) so I wrote this to obtain three sets of IPs:

    List<String> IPAddress = new ArrayList<String>();
    List<String> NonLoopbackIPv4Address = new ArrayList<String>();
    List<String> NonLoopbackIPv6Address = new ArrayList<String>();

    try {
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
        while(en.hasMoreElements()){
            NetworkInterface ni=(NetworkInterface) en.nextElement();
            Enumeration<InetAddress> ea = ni.getInetAddresses();
            while(ea.hasMoreElements()) {
                InetAddress ia= (InetAddress) ea.nextElement();

                //Add entries to the overall address array 
                IPAddress.add(ia.getHostAddress());

                //Add entries to non-loopback IPv4 address array
                if (!ia.isLoopbackAddress() && (ia.getClass() == Inet4Address.class))  {
                    NonLoopbackIPv4Address.add(ia.getHostAddress());
                }

                //Add entries to non-loopback IPv6 address array
                if (!ia.isLoopbackAddress() && (ia.getClass() == Inet6Address.class))  {
                    NonLoopbackIPv6Address.add(ia.getHostAddress());
                }
            }
         }
    } catch (Exception e) {
        //catch and process necessary exceptions, etc...
        throw new Exception (e);
    }

...where:

  • IPAddress - list of all IP addresses for this host (both v4 and v6), including those bound to the loopback interface
  • NonLoopbackIPv4Address - list of all IPv4 addresses for this host, excluding those bound to the loopback interface
  • NonLoopbackIPv6Address - list of all IPv6 addresses for this host, excluding those bound to the loopback interface
3
  • Was this meant to be an answer to the question? It looks like Java code. Are you on the right site? We generally don't write Java code.. Aug 4, 2015 at 4:48
  • Yes, its my answer to my own question. I have to write code in Java instead of shell and so decided to share it. Feel free to delete if it improves this site.
    – Happyblue
    Aug 4, 2015 at 5:03
  • I think this question may have been more appropriate on Stack Overflow rather than Server Fault, just fyi :)
    – Shane
    Aug 6, 2015 at 1:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .