• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • Himai Minh

getServerPort() vs getLocalPort()

Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody !

Could someone please explain me why requests are forwarded to different
ports on the server ?

I understand, that getServerPort() returns the original port number that
the request was made to and that getLocalPort() returns the current one
that is using but why would we need it ? Can't we handle multiple client's
connections on the same port ? And secondly, how is it achieved ?
I mean normally, a container gets request, creates or allocates a thread
which uses servlet's object to handle the request. So when the request is
made, it uses containers port number to communicate with it (so socket for
a container is created with an associated port number). Where does the new
port number fit in this scenario ? Client sends some new data to the new
port number and .... what happens on the server side ?

Thanks and Merry Christmas Everybody !!!

Adrian Sosialuk
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If (for example) you put Apache in front of Tomcat, the port that Apache serves content on may not be the same as the one that Tomcat listens on. Likewise, if you use port forwarding on a router you could have two different ports depending on whether you are talking about external or internal requests.
Adrian Sosialuk
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David !

Thanks for you prompt reply.

Well - what you have said makes sense for me, but I'm wondering
about another context. In HFSJ you can read: "The difference between
getLocalPort() and getServerPort() is more subtle - getServerPort()
says, 'to which port was the request originally sent' while getLocalPort()
says, 'on which port did the request end up ?' Yes, there is a difference,
because although the requests are sent to a single port (where the server is listening), the server turns around and finds a different local port for
each thread, so that the app can handle multiple clients at the same time."

I don't understand why the app wouldn't be able to handle multiple clients
at the same time without having different port numbers for each thread ...

And when it happens, does it mean that container creates socket for each
client and container then starts listening on those ports ? It would mean
that container needs some kind of connection manager so it knows what
client connects on which port and forwards to which threads. So I see it
this way (assuming there is only tomcat standalone) : client connects
to tomcat on port let's say 8080. Container creates/allocates a thread,
loads the servlet, creates another socket with port let's say 8081, passes
the request through the thread to servlet and it generates the output.
Next time the same client sends request, it uses port 8081 on which the
container is now listening and the story continues ... Am I right here ?? :/

Also, would it be done that way for efficiency ?


Adrian Sosialuk
    Bookmark Topic Watch Topic
  • New Topic