Results 1 to 9 of 9

Thread: Constructing an InetAddress from a byte[]

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Constructing an InetAddress from a byte[]

    After reading a question on this topic over at jguru i tried to figure out how an InetAddress object could be constructed using an ip address in the form of a byte[].

    Figuring the ip address is most likely a String(say if a host is resolved to an IpAddress) i tried to construct a byte array to make another InetAddress object but the code simply fails because the byte array is too long.
    Code:
    InetAddress inet = InetAddress.getByName("www.sun.com");
    String ip = inet.getHostAddress();
    System.out.println(ip);
    InetAddress inet2 = InetAddress.getByAddress(ip.getBytes());
    System.out.println(inet2.getHostAddress());
    Even hardcoding the values of the ip address into a byte[] wouldn't work since java dosen't have unsigned primative types.

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Constructing an InetAddress from a byte[]

    use getAddress to get the bytes:
    Code:
    import java.net.*;
    
    class Testing
    {
    	public static void main(String[] args) throws Exception
    	{
    		InetAddress inet = InetAddress.getByName("www.sun.com");	
    		byte[] ip = inet.getAddress();
    		InetAddress inet2 = InetAddress.getByAddress(ip);
    		System.out.println(inet2.getHostAddress()); 
    	}
    }

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Constructing an InetAddress from a byte[]

    Works. Now getAddress() returns a byte[] but that's only from a InetAddress object that has been created already. So what would we do without it?
    Code:
     InetAddress inet = InetAddress.getByName("www.sun.com");
     System.out.println(inet.getHostName()); 
     System.out.println(inet.getHostAddress()); 
     InetAddress inet2 = InetAddress.getByAddress(inet.getAddress());
     System.out.println(inet2.getHostName()); 
     System.out.println(inet2.getHostAddress());

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Constructing an InetAddress from a byte[]

    So your wanting to get the IP Address without an InetAddress object to read the bytes from? If so, I don't think it's possible. I've looked into the URL class, but it doesn't have anything, even though it does support the ipaddress as an argument. The only other way I can think of is getting some external process:

    Code:
    Process p = Runtime.Runtime().exec("ipconfig");
    Maybe somehow you could get it using net commands.

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Constructing an InetAddress from a byte[]

    Posted by System_Error

    Maybe somehow you could get it using net commands.
    Perhaps. Ill have to look into it. Thanks for the help.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Constructing an InetAddress from a byte[]

    What exactly is wrong with using an InetAddress?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Constructing an InetAddress from a byte[]

    Why do you need to construct an InetAddress from a byte[] that comes from who-knows-where?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Constructing an InetAddress from a byte[]

    It was a question that was asked by someone. Why they would want to i have no idea.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width