Finding DB2 servers running in LAN


I wanted to know if there is a way of finding the ip address of the DB2 servers that might be running in LAN. The need is to get all the ip addresses and show them in a list format. So the user can select to which ip address he wantes to connect to. Is there a way to achive this in Java. I'm currently working with Java and my database is DB2.

Thanks in advance.
LVL 18
Who is Participating?
CEHJConnect With a Mentor Commented:
This is an old question answer i found - you can construct a range of ip addresses using this approach:


public class InetRange {

      public static void main(String[] args) {
            long ip = InetRange.ipToLong(args[0]);

      public static String longToIp(long ipAddress) {
            StringBuffer sb = new StringBuffer(15);
            long octetMask = 0xFF;
            long octet1 = (ipAddress >> 24) & octetMask;
            long octet2 = (ipAddress >> 16) & octetMask;
            long octet3 = (ipAddress >> 8) & octetMask;
            long octet4 = (ipAddress >> 0) & octetMask;
            return sb.toString();

      public static long ipToLong(String ipAddress) {
            long result = 0;
            try {
                  byte[] bytes = InetAddress.getByName(ipAddress).getAddress();
                  long octet1 = bytes[0] & 0xFF;
                  octet1 <<= 24;
                  long octet2 = bytes[1] & 0xFF;
                  octet2 <<= 16;
                  long octet3 = bytes[2] & 0xFF;
                  octet3 <<= 8;
                  long octet4 = bytes[3] & 0xFF;
                  result = octet1 | octet2 | octet3 | octet4;
            } catch (Exception e) {
                  return 0;
            return result;

      public static String intToIp(int ipAddress) {
            int octet1 = (ipAddress & 0xFF000000) >> 24;
            int octet2 = (ipAddress & 0xFF0000) >> 16;
            int octet3 = (ipAddress & 0xFF00) >> 8;
            int octet4 = ipAddress & 0xFF;
            return new StringBuffer().append(octet1).append('.').append(octet2).append(

I think that you have to scan the network and check which active host have the 50000 (or whatever you set) port listening. It can take some time.
tusharashahAuthor Commented:
Can you give me more details on how can i scan the netowrk through a java program.  
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

try to take a look at this:
Try calling on the domain controller machine. Iterate each address, creating a socket connection to the port for db2
Forgive me. Maybe i have something to learn in here too :).

You told that it's possible to use getAllByName() to retrieve all the addresses in the local network.
But are you sure?
I always thought that it gives you all the accessible addresses of a single host, not of all the host of the network. How do you do as you say?

>>I always thought that it gives you all the accessible addresses of a single host

Yep - you're right. Thanks. I think i must have been thinking of a getAllX call in some other package that i've used
RomanRegaConnect With a Mentor Commented:
>> Yep - you're right. Thanks.

Thank for what. I was actually hoping that you were right. :)
I admit, this is a damn interesting question for me too.

I know how to ping a host, i know how to look if a port is listening, but i dont know a fast way to generate all the possible address in a network for the scanning.

I was kinda hoping there was a method ready.

Else the only way is to guess the netmask from your ip address.
Then generate all the possible ip and check any one of them.
It sounds damn slow.  
tusharashahAuthor Commented:
I was also under the same impression that there might be some method ready to scan. Because when generating IP range would be slow and inaccurate when we're sending out our application.

Well, thanks for your help and if you happen to find any better approach then let me know..


To make a range, you just need to take the output from the command ipconfig (or ifconfig on Unix) and apply the netmask. Since you're only 'pinging' one port it shouldn't be too lengthy a procedure
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.