我已经用java为我的应用程序编写了一个服务器,它需要唯一地标识每个连接。我目前通过 SocketChannel 的 Socket 端口来识别每个连接。
套接字的端口是 JVM 给定的数字吗?
如果是,这是否意味着平均最大连接数仅限于该类型连接可用的端口数量? (这个问题可能是对问题 1 答案的反驳,但安全总比后悔好!)
任何 Java 开发人员对于唯一标识每个连接有更好的技巧吗?
最佳答案
I currently identify each connection by the port of SocketChannel's Socket.
如果您指的是本地端口,那么您的代码已经不起作用。
Is the port of a socket a number given by JVM?
不,它是由 TCP 给出的,除非您自己在代码中指定。
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?
没有。从同一服务器套接字接受的所有套接字的本地端口与服务器套接字的本地端口相同。
Any Java developers that have any better tips for uniquely identify each connection?
您应该通过元组来识别它{本地IP地址,本地端口,远程IP地址,远程端口}。
关于Java套接字——套接字端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997097/