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. 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
Joined: Nov 23, 2011
Thanks Gurpeet. Got the point. You explained it very well.