• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

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.
  • 4
  • 4
  • 2
2 Solutions
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.  
try to take a look at this:

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Try calling http://java.sun.com/j2se/1.5.0/docs/api/java/net/InetAddress.html#getAllByName(java.lang.String) 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
This is an old question answer i found - you can construct a range of ip addresses using this approach:

import java.net.InetAddress;

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(

>> 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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now