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.
Gordon Beaton - 21 Dec 2005 09:36 GMT
> 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

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Roedy Green - 21 Dec 2005 10:23 GMT
>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."

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 21 Dec 2005 10:25 GMT
>Is there any way to specify which IP address to use when originating a
>TCP/IP request?
[quoted text clipped - 3 lines]
>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

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.