HTTPURLConnection端口号分配

时间:2016-04-15 08:11:18

标签: java sockets port httpurlconnection

我正在使用HTTPURLConnection并想知道端口问题是如何处理的?我的意思是该类使用引擎盖下的套接字,但如何在内部分配客户端套接字的端口号?有什么想法吗?

THX

2 个答案:

答案 0 :(得分:1)

不是。它完全取决于操作系统。

答案 1 :(得分:1)

When creating a socket you have to take into account whether you want to open a server-socket or a client-socket.

A server-socket is opend and sits there waiting for an incoming request to start a "conversation". So it needs a port-number that is known to the client. For this reason several services have "well-known ports" like HTTP is port 80 etc.

A client-socket is created an starts with contacting a server-socket. In this case the server does not need to know the clients port-number in advance - it will receive it with the first incoming request. So when opening a client-socket the operating system will just choose a random port from a list of unused ports (look up "ephemeral port") and often close it once the conversation is finished.

The situation is similar to ip-adresses: the client needs to know the servers ip-address to contact it but the server does not need to know each clients ip-address.