Originating connections from multiple IP addresses on same server

C

cigusenet

Is there any way to specify which IP address to use when originating a
TCP/IP request?

I have a server with multiple IP addresses and I'd like to use specific
ones to create connections with URLConnection. I read that this may
not be possible without creating a custom URLStreamHandler, but I'm
hoping there may be an easier way.
 
G

Gordon Beaton

Is there any way to specify which IP address to use when originating
a TCP/IP request?

Sure, create an unconnected Socket and bind() it to the address of the
interface you want to use before calling connect(). If you only care
about the originating address (not the port number) specify port 0.
I have a server with multiple IP addresses and I'd like to use
specific ones to create connections with URLConnection. I read that
this may not be possible without creating a custom URLStreamHandler,
but I'm hoping there may be an easier way.

You could use a SocketImplFactory, but that would affect every Socket.
There is a similar factory for URLStreamHandlers, I think that's your
best bet.

/gordon
 
R

Roedy Green

Is there any way to specify which IP address to use when originating a
TCP/IP request?
just put in the ip in there where you would otherwise put a DNS. e.g.
"24.85.32.136"
or
"080:0:0:0:8:800:200C:41FA."
 
R

Roedy Green

Is there any way to specify which IP address to use when originating a
TCP/IP request?

I have a server with multiple IP addresses and I'd like to use specific
ones to create connections with URLConnection. I read that this may
not be possible without creating a custom URLStreamHandler, but I'm
hoping there may be an easier way.

Another way to do it would be to get the corresponding IP addresses
from the DNS name, pick one yourself and use it. See
http://mindprod.com/jgloss/ip.html
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top