Thread View: comp.lang.java.programmer
3 messages
3 total messages
Started by Martin Ry��nek
Thu, 17 Sep 1998 00:00
InetAddress - no solution
Author: Martin Ry��nek
Date: Thu, 17 Sep 1998 00:00
Date: Thu, 17 Sep 1998 00:00
20 lines
1271 bytes
1271 bytes
Hello, I have just byte[] array of 4 bytes and need to create regular InetAddress class. I don't know host's name and so. I only know host's IP address. I can't create regular InetAddress class, because I have to pass the name of the host to constructor. InetAddress inet = new InetAddress(null, array); is NOT solution, because InetAddress is willing to create String with null pointer. If I pass "" string, I can not receive host name based on IP. How can I receive host name based on IP, if I have just IP as 4 byte array ? Martin
Re: InetAddress - no solution
Author: Martin Ry��nek
Date: Thu, 17 Sep 1998 00:00
Date: Thu, 17 Sep 1998 00:00
27 lines
1464 bytes
1464 bytes
>I have just byte[] array of 4 bytes and need to create regular InetAddress >class. > >I don't know host's name and so. I only know host's IP address. I can't >create regular >InetAddress class, because I have to pass the name of the host to >constructor. > >InetAddress inet = new InetAddress(null, array); >is NOT solution, because InetAddress is willing to create String with null >pointer. > >If I pass "" string, I can not receive host name based on IP. > >How can I receive host name based on IP, if I have just IP as 4 byte array ? The only solution is: convert your 4 byte array to string in dot convention first, then pass to InetAddress as string. That works fine. Sounds good ?!?!?!?! is not it. Martin
Re: InetAddress - no solution
Author: Paul Tomblin
Date: Thu, 17 Sep 1998 00:00
Date: Thu, 17 Sep 1998 00:00
27 lines
1633 bytes
1633 bytes
In a previous article, "Martin Ry��nek" <rysanek@telecom.cz> said:
>I have just byte[] array of 4 bytes and need to create regular InetAddress
>class.
Sorry, but you have to convert it to a string:
private static String IPToString(byte[] addr)
{
String dottedquad = "";
for (int i = 0; i < addr.length;)
{
short sh = (short)(addr[i] & 0xff);
dottedquad += Integer.toString(sh);
i++;
if (i < addr.length)
dottedquad += ".";
}
return dottedquad;
}
Note that while I call it dotted*quad*, it will work in that time in the
future when the byte array is longer than 4 bytes. (IPv6, whose time has come)
--
Paul Tomblin, ptomblin@xcski.com.
"Only a NAZI would try to invoke Godwin's [law] deliberately"
- Jeff Gostin in a.s.r
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads