简体   繁体   English

Java套接字-套接字端口

[英]Java socket - the socket port

I have written a server in java for my application which needs to uniquely identify each connection. 我已经用Java为我的应用程序编写了一个服务器,该服务器需要唯一地标识每个连接。 I currently identify each connection by the port of SocketChannel's Socket. 我目前通过SocketChannel的Socket端口标识每个连接。

  1. Is the port of a socket a number given by JVM? 套接字的端口号是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!) (考虑到问题1的答案,该问题可能是一个反义词,但是比后悔更安全!)

  3. Any Java developers that have any better tips for uniquely identify each connection? 有没有更好的技巧来唯一标识每个连接的Java开发人员?

I currently identify each connection by the port of SocketChannel's Socket. 我目前通过SocketChannel的Socket端口标识每个连接。

If you mean the local port, your code already doesn't work. 如果您的意思是本地端口,则您的代码已经无法使用。

Is the port of a socket a number given by JVM? 套接字的端口号是JVM给定的数字吗?

No, it is given by TCP unless you specified it yourself in the code. 不,它由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? 如果是,这是否意味着平均最大连接数受限于该类型连接可用的端口数量?

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? 有没有更好的技巧来唯一标识每个连接的Java开发人员?

You should identify it by the tuple {local IP address, local port, remote IP address, remote port}. 您应该通过元组{本地IP地址,本地端口,远程IP地址,远程端口}进行标识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM