Sending an IP address to a ServerSocket without name resolution?

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

I am writing a simulation which needs to maintain a table that maps
host names to IP addresses. Because I will be connecting to simulated
hosts, the hostnames do not exist in DNS. I will, however, be
connecting to real hosts. I need a way to create a ServerSocket that
connects to my choice of IP address. But ServerSocket only takes
InetAddress objects, and those don't provide a way to set the IP
address. Is there a way to do this?

Thanks,
Michael
 
T

Thomas Weidenfeller

I am writing a simulation which needs to maintain a table that maps
host names to IP addresses. Because I will be connecting to simulated
hosts, the hostnames do not exist in DNS. I will, however, be
connecting to real hosts. I need a way to create a ServerSocket that
connects to my choice of IP address. But ServerSocket only takes
InetAddress objects, and those don't provide a way to set the IP
address.

Wrong. Please see the API documentation of InetAddress.getByAddress(byte[]).

/Thomas
 
N

nooneinparticular314159

Thanks! I didn't see that. So do I need to create a string:

a.b.c.d

including the "."s, and convert that into a Byte Array? Or does it
expect the IP without the .'s?

Thanks,
Michael
 
G

Gordon Beaton

Thanks! I didn't see that. So do I need to create a string:

a.b.c.d

including the "."s, and convert that into a Byte Array? Or does it
expect the IP without the .'s?

Just create a string containing the IP address:

Socket s = new Socket("192.168.141.29", 80);

/gordon
 
N

nooneinparticular314159

Hi, Gordon. I tried a version of this earlier. It didn't work - I
think it tried to use the IP address in quotes as a hostname, and
failed to look up the hostname, of course.
 
G

Gordon Beaton

Hi, Gordon. I tried a version of this earlier. It didn't work - I
think it tried to use the IP address in quotes as a hostname, and
failed to look up the hostname, of course.

The Socket constructor passes the host String to
InetAddress.getByName(), which does not attempt to look up literal IP
addresses. It simply checks that the format is correct before creating
an InetAddress() from the String.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top