"bind"ing is (iirc) something quite different:
you wait for incoming connections with "bind"
No, you wait for incoming connections with listen() (though, as near as I
can tell, in Java this is done implicitly when you create a ServerSocket,
rather than there being an actual listen() method...I'm more familiar with
the lower-level sockets API than Java's wrapper around it). The bind()
method is used simply to assign an address to a socket.
Starting an outbound connection, even if a
local address is specified will not "accept"
any new connections, so I assumed that "bind"
wasn't relevant to the concept of a source-
address. (I may be wrong here)
Yes, you are. A socket must be bound before it's used. You can call
bind() explicitly, or it will be called on your behalf when you first try
to use the socket in a context that requires a bound address. But the
socket does get bound.
Your confusion may arise from the fact that most commonly a server socket
(i.e. one that's listening) is bound explicitly (usually because you want
a specific port), while a client socket (i.e. one that would connect to a
listening socket) often need not be bound explicitly.
The whole problem was, that to specify a local
port (which may happen occasionally), there
is no Socket-constructor that wouldn't also
require a source address, but the source
address is best picked by the system, depending
on the target.
That's not a problem at all. Typically, providing INADDR_ANY is the way
to do this. You provide an explicit port, and let the OS pick the IP
address. Assuming Java behaves like the underlying OS, a server socket
bound to INADDR_ANY will listen for and accept connections on any valid IP
address, while a client socket will connect to a server using some
specific IP address.
I admit, I haven't used the Java socket API specifically, but I would be
very surprised if it deviated that much from the original socket API on
which it's obviously based.
The machine might be a router
and be known to the inner net by a different ip,
than on the other net. e.g. the outer net might
not be able to reach the machine through some
10.x.y.z address, and the inner net might not
even know the possibly dynamic IP-address of the
ppp-dialup-link. So, if that router machines
opens a connection to the inner net, then it had
better pick the 10.x.y.z address, otherwise the
externally known ip-address.
If it uses INADDR_ANY, it should receive connection requests from either
network. The socket will not receive an actual assigned IP address until
it's been connected (for a client socket, after connect() has successfully
completed, for a server socket, it's the socket returned from accept()
that will have an assigned IP address, based on the connection made).
Pete