stju
stju

Reputation: 11

Java socket - the socket port

I have written a server in java for my application which needs to uniquely identify each connection. I currently identify each connection by the port of SocketChannel's Socket.

  1. Is the port of a socket a number given by JVM?

  2. If it is, does this mean the mean max number of connections is limited to the amount of ports to available for that type of connection? (this question might be a retorical given the answer from question 1, but better safe than sorry!)

  3. Any Java developers that have any better tips for uniquely identify each connection?

Upvotes: 1

Views: 114

Answers (1)

user207421
user207421

Reputation: 311054

I currently identify each connection by the port of SocketChannel's Socket.

If you mean the local port, your code already doesn't work.

Is the port of a socket a number given by JVM?

No, it is given by TCP unless you specified it yourself in the code.

If it is, does this mean the mean max number of connections is limited to the amount of ports to available for that type of connection?

No. The local port for all sockets accepted from the same server socket is the same as the server socket's.

Any Java developers that have any better tips for uniquely identify each connection?

You should identify it by the tuple {local IP address, local port, remote IP address, remote port}.

Upvotes: 2

Related Questions