Patrick;
Don't get trapped by the word 'socket'. A socket is an accounting and
management data structure. Don't think of it like an electrical socket that
is used up with a since instance.
Looking at Comer's or Stevens' basic introductions to networking define the
following. There is a data structure (socket) for each connection, as well
as the listening connections, and those in the shutdown process.
A connection is identified by:
local IP
local port
remote IP
remote port
protocol
By simply changing the remote port, the system distinquishes a different
connection (socket). Waiting connections have no remote data. When a
connection is accepted, a new socket structure is used that has the remote
IP and port filled in. Viola, new socket.. new connection.
There are also sockets for 'connection-less connections' such as UDP
that have slightly different rules.
As to the question about the ephemerial port during socket connection;
that is trial and error. (My context is 'C' and C++ system calls). By
placing the zero in the port number of the socket() you are asking for
'any' available port. But if you want all the connections originating from
this program/system to be in specific range you have to poll that.
As an example, all ephemeral ports are to be in the range 10100<->10500.
The simple approach is to request a socket using port 10100, if the
return is PortInUse (system/lang specific), try 10101...10102.. You can
make this more efficient by starting after the last port that worked
successfuly.
BUT! by restricting yourself to 401 ports (in this example) you leave
yourself open to a resource exhaustion attack that causes your app to
consume all the ports. Be prepared to handle the case where no ports are
available to be used!
Victor Probo
Patrick wrote:
> Is there a way to find an unused local port for when I create a client
> socket, or is trial and error?
>
> -- P
>
> "Patrick" <[EMAIL PROTECTED]> wrote in message
> [EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
>
>>Here's a general question and it came about when I went to create a
>>SSLServerSocket in JSS:
>>
>>How can I control what port is used by the socket returned by the accept
>>method? (As you all know, after the server accepts a client connection, it
>>creates a *brand new socket* which listens on *a brand new port*; this is
>>
> so
>
>>the server can continue listening to more clinet connections on the
>>
> original
>
>>socket...).
>>
>>Even in Sun's ServerSocket class, there seems to be no way...
>>
>>I know this is a fundamental socket programming issue, but I really never
>>paid much attention to this issue until recently when my NSS app had a
>>requirement for staying within a pre-determined range for dynamically
>>allocated ports when creating any new socket...
>>
>>-- P
>>
>>
>>
>
>