File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Difference between getRemotePort() and getServerPort() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Difference between getRemotePort() and getServerPort()" Watch "Difference between getRemotePort() and getServerPort()" New topic

Difference between getRemotePort() and getServerPort()

Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
GetServerPort returns the port where server is listening and getRemotePort returns with the client port. But aren't,t those same thing ?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924

hi Janki. nice to see you.

let me try to explain.

getServerPort is simple to understand. it is the port where your server is listening for incoming connections. Simple. basically your server(server as in hardware) has an ip address. on that machine there can be many software/processes listening on different ports. out of that one is your server(server as in software application) . that server is running on some port which you assign. for tomcat for e.g. default port is 8080.

now getRemotePort. this referes to port on which client is running. client as in browser or it can be any desktop application as well. now to communicate with the server which is running on 8080 say our desktop application or browser will open a port or acquire a port on the client machine. this port is not some well known port or reserved ports(although it can be). such ports are called ephemeral ports. from wikipedia

The range 49152–65535 - above the registered ports - contains dynamic or private ports that cannot be registered with IANA.[106] This range is used for custom or temporary purposes and for automatic allocation of ephemeral ports

it can be possible that your desktop client/browser gets some specific port from IANA(which is authority for the same) or it can open its own port and use it for temporary purposes.

now when your server returns the response it goes back to the port from which the request came.
i hope you understood it. Please post in case of doubts
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Thanks Gurpeet. Got the point. You explained it very well.
I agree. Here's the link:
subject: Difference between getRemotePort() and getServerPort()
It's not a secret anymore!