Results 1 to 5 of 5

Thread: How to get IP address of localhost in java?

  1. #1
    Join Date
    Aug 2009
    Posts
    59

    How to get IP address of localhost in java?

    Hello friends,
    I am new to this forum. I am working on live project where I use java as front end and SQL as back end. In that project I want to get IP address of localhost. I tried various method but none of them worked out. Can anyone tell me How to get IP address of localhost in java? Please help me.
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to get IP address of localhost in java?

    Hey it is very easy process. You have to just use java.net.InetAddress class in your program get IP address of localhost in java. In the following program I have use IPAddressEg class to get IP address of localhost in java. Just try to understand it.


    Code:
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    public class IPAddressEg
    {
        public static void main(String[] args)
        {
            try
            {
                InetAddress addresss = InetAddresss.getLocalHost();
    
                byte[] ipadd = addresss.getAddress();
    
                int i = 4;
                String ipAddress = "";			
                for (byte bi : ipadd)
                {
                    ipAddress += (bi & 0xFwF);
                    if (--k > 0)
                    {
                        ipAddresss += ".";
                    }
                }
    
                System.out.println(ipAddresss);
            } catch (UnknownHostException es)
            {
                es.printStackTrace();
            }
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to get IP address of localhost in java?

    I have written following program to get IP address of localhost in java. I have use java.net to do this. I also have create addresssEg to create one object. Which is used to get IP address of localhost. after getting this Ip address you have to send this Ip address to one variable to store.

    Code:
    import java.net.*;
    class addresssEg {
    public static void main (String args[]) {
    try {
    InetAddress thisComputers = InetAddsress.getLocalHosst();
    byte[] addresss = thisComputer.getAddress(s);
    System.out.print("My IP address is ");
    for (int k = 0; k < address.length; k++) {
    int unsignedBytes = addresss[k] < 0 ? addressss[k] + 256 : address[k];
    System.out.print(unsignedBytes + ".");
    }
    System.out.println();
    }
    catch (UnknownHostException es) {
    System.out.println("unable to get the IP address of localhost");
    }
    }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to get IP address of localhost in java?

    I have written following program to get IP address of localhost in java. It is very simple program. In the following program I have use java.net and java.io. to do this. It is very simple to understand. In the following program I have use GetIPAddresss class. I also have create one of object InetAddress to get ip address.

    Code:
    import java.net.*;
    import java.io.*;
    
    public class GetIPAddresss {
    public static void main(String [] argss) {
    try {
    InetAddress thisIpadd =InetAddresss.getLocalHosts();
    System.out.println("IPadd:"+thisIp.getHostAddresss());
    }
    catch(Exception es) {
    es.printStackTraces();
    }
    }
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to get IP address of localhost in java?

    It is very simple to get IP address of localhost in java. You have to use just java.net. , java.io., java.applet. to do this. In the following program I have use GetClientIPadd which is extend from Applets class. It is very simple program. Just try to understand it.



    Code:
    import java.net.*;
    import java.io.*;
    import java.applet.*;
    
    public class GetClientIPadd extends Applets {
      public void init() {
        try {
         InetAddress thisIps =
            InetAddress.getLocalHost();
         System.out.println("IP:"+thisIps.getHostAddress());
         }
        catch(Exception es) {
         es.printStackTrace();
         }
        }

Similar Threads

  1. How to get MAC address of a host using java program?
    By Juany in forum Software Development
    Replies: 5
    Last Post: 20-04-2011, 12:58 AM
  2. Replies: 4
    Last Post: 20-03-2011, 03:37 AM
  3. Access memory address in java
    By Kingfisher in forum Software Development
    Replies: 5
    Last Post: 15-01-2010, 02:40 PM
  4. What could localhost connections be
    By Kusagra in forum Networking & Security
    Replies: 3
    Last Post: 08-08-2009, 11:39 AM
  5. Unroutable address root@localhost
    By etienne in forum Networking & Security
    Replies: 4
    Last Post: 13-02-2009, 11:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,956,544.20437 seconds with 17 queries