• Post Reply Bookmark Topic Watch Topic
  • New Topic

Internet address from String?

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

This time I'm working on a 100% Java FTP server, and since it will not be used over the internet, I'm trying to allow the user to input the IP address of the desired connection instead of Socket(Hostname, port#). I don't see that there is any way to parse a String into an internet address. Any ideas?

Thanks in advance!
 
Tarek Khojah
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand your problem.

InetAddress.getByName(String name) should work.

According to the 1.5 docs:

"Host name-to-IP address resolution is accomplished through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and Network Information Service(NIS). The particular naming services(s) being used is by default the local machine configured one. For any host name, its corresponding IP address is returned."

So if your local network is configured to work the way you want it, wouldn't it solve this problem?
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tarek Khojah:
I'm not sure I understand your problem.

InetAddress.getByName(String name) should work.

According to the 1.5 docs:

"Host name-to-IP address resolution is accomplished through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and Network Information Service(NIS). The particular naming services(s) being used is by default the local machine configured one. For any host name, its corresponding IP address is returned."

So if your local network is configured to work the way you want it, wouldn't it solve this problem?



Basically, I'm trying to let the user type an IP address in a text box field, then when the user clicks the 'connect' button, the FTP app will connect to another instance of itself (the other instance will be running as a server, I give the user the choice upon startup). Anyways, that's why I wanted to transform user input into an IP address, because they will be entering 192.168.1.2, for example, into a JTextField. So, with that said (hope I didn't confuse you),


will work, if I understand correctly? I'm not looking to resolve host names

Thanks a ton! It's been a while since I've messed with Java; we mainly did C++ at school, and I've gotten lazy lately by just doing VB.NET, so pardon the silly questions; I'm sure I will have more.
 
Tarek Khojah
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No silly questions, I'm quiet the beginner myself.

If there's still any confusion, getByName() works for both IP addresses and host names (or, err, domain names o machine names, not down with the lingo myself).

It's always best to have the API docs handy. So if you don't already have them, downloading them will make things so much easier for you.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a very similar problem, at the same skill bracket.

I have discovered already that Java does not want to use 4 byte ip addressing in the consructor - it wants a conventionial url and let it do the lookup on dns. Additionally, the InetAddress types are resolved into IPv4 / IPv6 by if(add.byte[].length ==4){;//do code} || if(add.byte[].length == 6 ){;//do code}, not by testing against instanceof as common sense suggests.

I have a copy of Java Network Programming ( O'Reilly ), very helpful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!