• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert Conventional IP Address to byte format

 
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to change the format of an IP address to a byte format:

IP Address 11.44.92.197
byte[] ipAddr = new byte[] {(byte) 11, (byte) 44, (byte) 92, (byte) 197};

This is my attempt but not successful.

 
Saloon Keeper
Posts: 15489
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a much simpler way:
 
Stephan van Hulst
Saloon Keeper
Posts: 15489
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seeing as you're going to operate on the InetAddress object, you don't even have to convert to the byte format. Just use InetAddress.getByName() and you're done.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Seeing as you're going to operate on the InetAddress object, you don't even have to convert to the byte format. Just use InetAddress.getByName() and you're done.



Thanks. Got this code working, however having issues.

Tested one three computers on the same network. 1 returned the computer name(SUSGAIDXXX). 2 returned the IP Address.
 
Stephan van Hulst
Saloon Keeper
Posts: 15489
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to give some more information. What are you trying to achieve? What code are you using to try to achieve it? What input are you using? What output are you getting?
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You need to give some more information. What are you trying to achieve? What code are you using to try to achieve it? What input are you using? What output are you getting?



machineip value# 1: 11.44.94.97
machineip value# 2: 11.44.92.85
machineip value# 3: 11.44.92.51



SystemOut     O MachinePrinterList: hostname: susgaid3xs1f42
SystemOut     O MachinePrinterList: hostname: 11.44.92.85
SystemOut     O MachinePrinterList: hostname: 11.44.92.51
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the first machine the one the code is running on?

From the docs:
"
If there is a security manager, this method first calls its checkConnect method with the hostname and -1 as its arguments to see if the calling code is allowed to know the hostname for this IP address, i.e., to connect to the host. If the operation is not allowed, it will return the textual representation of the IP address.
"
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Is the first machine the one the code is running on?



The code runs on a WebSphere Application Server on an iSeries(as400) computer.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:

"
If there is a security manager???
"



Is this security manager part of the application attempting the get the host name?

I really do not understand the part about security manager. I compile the application, deploy to WebSphere server, every client runs the app from that server.

Is the security manager somehow on the client machine apart from the application they are running?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've found the SO thread I remember reading this on from a couple of years ago:
https://stackoverflow.com/questions/34842698/inetaddress-getcanonicalhostname-returns-ip-instead-of-hostname

That might give you a pointer as to what it is about those two boxes that prevents InetAddress returning the names.
 
Stephan van Hulst
Saloon Keeper
Posts: 15489
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You forgot to mention what you're trying to achieve. Do you want the host name, or the IP address? If the latter, then why perform the conversion if you already start with the IP address?
 
Stephan van Hulst
Saloon Keeper
Posts: 15489
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that after the reverse DNS lookup is performed to get the host name, Java will then first perform a regular DNS lookup for the returned host name to verify that it indeed belongs to the IP address you supplied.

So, if the security manager allows you to perform the reverse lookup, it may still be the case that the DNS server's forward and reverse lookups don't match.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic