
Signature
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
>> How about in the java code itself? let's say, same java app executed
>> twice,
[quoted text clipped - 3 lines]
>>
> That's a somewhat different question from the one you originally asked.
Yup, but it was obvious he was going here, so I gave the real answer
right off the bat. :D
> All types of socket provide a getLocalPort() method to get their own
> port number and the Socket class, used by clients, can use getPort() to
> get the remote port. What your app does with the information is up to
> you. For instance, it might write it to a file, store it in a database
> table or send it to a server with a known host name and port that other
> apps can query.
I don't know if this is automatic in TCP/IP or not, but there's a common
idiom where a server listens on a well-known port (ie., one you publish
in /etc/services) and when it receives a request from a client, grabs a
new port (randomly) and responds to the client with that new port #. It
keeps the main port clear for new connections.
> You seem to be talking about a set of servers which each listens on an
> unused port chosen randomly from a range of ports. I must say that this
[quoted text clipped - 3 lines]
> connection) and passed to a thread that handles the connection until it
> is closed.
Ditto. The new port stuff I mention above would be handled by the new
thread in the example quoted above.
> The listener port number can then be published and documented in
> /etc/services. If there could be a clash with another server, its
> trivial to make the listener port number configurable via a config file,
> read from /etc/services or passed in as a command line argument.
Ditto, and good advice.
Final advice: get some basic networking books and learn how this works.
It ain't Java, it's TCP/IP. Java just implements an API for you.
Gordon Beaton - 23 Jul 2006 09:43 GMT
> I don't know if this is automatic in TCP/IP or not, but there's a common
> idiom where a server listens on a well-known port (ie., one you publish
> in /etc/services) and when it receives a request from a client, grabs a
> new port (randomly) and responds to the client with that new port #. It
> keeps the main port clear for new connections.
TCP does no such thing automatically or otherwise. Some poorly written
applications may do so, but it's hardly a common idiom.
The original listening port does not need to be "kept clear". Incoming
connections can and do share the same port number as the original
listening socket. There is no conflict, it creates no extra work for
the server, and each connection uses a completely distinct socket.
/gordon

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
EJP - 24 Jul 2006 02:22 GMT
> I don't know if this is automatic in TCP/IP or not, but there's a common
> idiom where a server listens on a well-known port (ie., one you publish
> in /etc/services) and when it receives a request from a client, grabs a
> new port (randomly) and responds to the client with that new port #. It
> keeps the main port clear for new connections.
Surely you mean 'thread' not 'port' throughout in the above? There is no
need for a new port and no such idiom I am aware of. Unless you are
talking about FTP which is another kettle of fish entirely.